英文:
Thymeleaf doesn't see page html in templates
问题
我是Spring Boot应用程序开发的新手。我使用Eclipse IDE,Spring Boot版本为3.1.1,Maven项目。
问题是我的.html
页面位于模板中(src/main/resources/templates
),需要通过它们的链接显示(例如localhost:8080/page),为它们创建了控制器。我还尝试在错误情况下显示页面(/error),该页面位于相同的文件夹中 - 并且它显示出来了。
此外,我将提供主要方法、控制器的代码以及HTML页面的一小部分代码(它们相当大...)
.java
文件中的void main
方法:
@SpringBootApplication
public class ChatSbAppApplication {
public static void main(String[] args) {
SpringApplication.run(ChatSbAppApplication.class, args);
}
}
错误控制器(正常工作):
@Controller
public class ErrorController implements org.springframework.boot.web.servlet.error.ErrorController {
@RequestMapping("/error")
public String handleError() {
return "error";
}
@Override
public String getErrorPath() {
return "/error";
}
}
标准控制器:
@Controller
public class UserAccountController {
private final UserAccountService userAccountService;
@Autowired
public UserAccountController(UserAccountService userAccountService) {
this.userAccountService = userAccountService;
}
///注册
@GetMapping("/register")
public String showRegistrationForm() {
return "register";
}
@PostMapping("/register")
public String registerUser(@RequestBody UserAccount userAccount) {
userAccountService.saveUserAccount(userAccount);
return "redirect:/login";
}
///授权
@GetMapping("/login")
public String loginUser() {
return "login";
}
@PostMapping("/login")
public String processLoginForm() {
return "redirect:/chats";
}
}
application.properties:
spring.datasource.url=jdbc:postgresql://localhost:5432/ChatDB
spring.datasource.username=postgres
spring.datasource.password=1234
spring.datasource.driver-class-name=org.postgresql.Driver
server.error.whitelabel.enabled = false
spring.jpa.open-in-view=false
我还尝试更改配置,但也不起作用。
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Bean
public ViewResolver viewResolver() {
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setTemplateEngine(templateEngine());
resolver.setCharacterEncoding("UTF-8");
return resolver;
}
@Bean
public TemplateEngine templateEngine() {
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.setEnableSpringELCompiler(true);
engine.setTemplateResolver(templateResolver());
return engine;
}
@Bean
public ITemplateResolver templateResolver() {
SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
resolver.setPrefix("classpath:/templates/");
resolver.setSuffix(".html");
resolver.setTemplateMode(TemplateMode.HTML);
resolver.setCharacterEncoding("UTF-8");
return resolver;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoggingInterceptor());
}
}
HTML示例如下:error.html
可以工作,但相同的login.html
无法看到。另外,Thymeleaf可以很好地看到/static
中的.css
和.js
...
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Error Page</title>
<link rel="stylesheet" type="text/css" th:href="@{index.css}">
</head>
<body>
<div class="navbar">
<div class="navbar-logo">
<button onclick="changeColor()" class="color-button">
</button>
</div>
<div style="display:flex; margin-right: 40px;">
<button class="auth-button" onclick="window.location.href='login.html'">Login</button>
</div>
</div>
<script src="index.js"></script>
</body>
</html>
我也可以显示日志,但其中没有什么令人惊讶的地方 - 没有错误或警告,也没有提到创建的映射(我看到,我的观点应该有一条消息,类似“映射已创建在路径[x.html]”之类的消息,但我没有看到这样的消息)。pom.xml
中也包含了Thymeleaf依赖项。
英文:
I am new to Spring boot application development. I use eclipse IDE, spring boot v.3.1.1, maven project.
The problem is that my .html
pages are in templates (src/main/resources/templates
), which need to be displayed by their link (like a localhost:8080/page), controllers were created for them. I also tried to display the page in case of an error (/error), which is located in the same folder - and it is displayed.
In addition, I will give the code of the main method, controllers, and a small code of the html pages (they are quite large ...)
.java
file with void main:
@SpringBootApplication
public class ChatSbAppApplication {
public static void main(String[] args) {
SpringApplication.run(ChatSbAppApplication.class, args);
}
}
Error controller (works fine):
@Controller
public class ErrorController implements org.springframework.boot.web.servlet.error.ErrorController {
@RequestMapping("/error")
public String handleError() {
return "error";
}
@Override
public String getErrorPath() {
return "/error";
}
}
Standard controllers:
@Controller
public class UserAccountController {
private final UserAccountService userAccountService;
@Autowired
public UserAccountController(UserAccountService userAccountService) {
this.userAccountService = userAccountService;
}
///registration
@GetMapping("/register")
public String showRegistrationForm() {
return "register";
}
@PostMapping("/register")
public String registerUser(@RequestBody UserAccount userAccount) {
userAccountService.saveUserAccount(userAccount);
return "redirect:/login";
}
///authorisation
@GetMapping("/login")
public String loginUser() {
return "login";
}
@PostMapping("/login")
public String processLoginForm() {
return "redirect:/chats";
}
}
application.properties:
spring.datasource.url=jdbc:postgresql://localhost:5432/ChatDB
spring.datasource.username=postgres
spring.datasource.password=1234
spring.datasource.driver-class-name=org.postgresql.Driver
server.error.whitelabel.enabled = false
spring.jpa.open-in-view=false
I also tried changing the config, but it didn't work either.
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Bean
public ViewResolver viewResolver() {
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setTemplateEngine(templateEngine());
resolver.setCharacterEncoding("UTF-8");
return resolver;
}
@Bean
public TemplateEngine templateEngine() {
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.setEnableSpringELCompiler(true);
engine.setTemplateResolver(templateResolver());
return engine;
}
@Bean
public ITemplateResolver templateResolver() {
SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
resolver.setPrefix("classpath:/templates/");
resolver.setSuffix(".html");
resolver.setTemplateMode(TemplateMode.HTML);
resolver.setCharacterEncoding("UTF-8");
return resolver;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoggingInterceptor());
}
}
HTML example below: error.html works, but same login.html does not see. Also thymeleaf saw fine .css/.js in /static...
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Error Page</title>
<link rel="stylesheet" type="text/css" th:href="@{index.css}">
</head>
<body>
<div class="navbar">
<div class="navbar-logo">
<button onclick="changeColor()" class="color-button">
</button>
</div>
<div style="display:flex; margin-right: 40px;">
<button class="auth-button" onclick="window.location.href='login.html'">Login</button>
</div>
</div>
<script src="index.js"></script>
</body>
</html>
I can also show the logs, but there is nothing surprising in them - not 1 error or warning, but not 1 mention of the created mapping (I read, in my opinion there should be a message, like "mapping was created along the path [x.html]" or something like that, I didn't have such a message). The thymeleaf dependency is also in pom.xml
As I described above, I tried to change the thymeleaf config, but it didn't work. Also I read some other people's problems and tried to "Project->Update Maven Project" (Alt+F5) but it didn't help too
答案1
得分: 0
使用 window.location
打开链接需要设置完整路径。
尝试替换:
<button class="auth-button" onclick="window.location.href='login.html'">Login</button>
为:
<button class="auth-button" onclick="window.location.href='http://localhost:8080/login'">Login</button>
还请注意,不应包含 .html
扩展名,因为控制器在解析模板时会处理此部分。
英文:
Opening link using window.location will require you to set complete path.
Try to replace
<button class="auth-button" onclick="window.location.href='login.html'">Login</button>
With
<button class="auth-button" onclick="window.location.href='http://localhost:8080/login'">Login</button>
Also note you should not include .html extension since controller takes care of this when parsing templates
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论