Thymeleaf在模板中看不到页面HTML。

huangapple go评论104阅读模式
英文:

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方法:

  1. @SpringBootApplication
  2. public class ChatSbAppApplication {
  3. public static void main(String[] args) {
  4. SpringApplication.run(ChatSbAppApplication.class, args);
  5. }
  6. }

错误控制器(正常工作):

  1. @Controller
  2. public class ErrorController implements org.springframework.boot.web.servlet.error.ErrorController {
  3. @RequestMapping("/error")
  4. public String handleError() {
  5. return "error";
  6. }
  7. @Override
  8. public String getErrorPath() {
  9. return "/error";
  10. }
  11. }

标准控制器:

  1. @Controller
  2. public class UserAccountController {
  3. private final UserAccountService userAccountService;
  4. @Autowired
  5. public UserAccountController(UserAccountService userAccountService) {
  6. this.userAccountService = userAccountService;
  7. }
  8. ///注册
  9. @GetMapping("/register")
  10. public String showRegistrationForm() {
  11. return "register";
  12. }
  13. @PostMapping("/register")
  14. public String registerUser(@RequestBody UserAccount userAccount) {
  15. userAccountService.saveUserAccount(userAccount);
  16. return "redirect:/login";
  17. }
  18. ///授权
  19. @GetMapping("/login")
  20. public String loginUser() {
  21. return "login";
  22. }
  23. @PostMapping("/login")
  24. public String processLoginForm() {
  25. return "redirect:/chats";
  26. }
  27. }

application.properties:

  1. spring.datasource.url=jdbc:postgresql://localhost:5432/ChatDB
  2. spring.datasource.username=postgres
  3. spring.datasource.password=1234
  4. spring.datasource.driver-class-name=org.postgresql.Driver
  5. server.error.whitelabel.enabled = false
  6. spring.jpa.open-in-view=false

我还尝试更改配置,但也不起作用。

  1. @Configuration
  2. public class WebConfig implements WebMvcConfigurer {
  3. @Bean
  4. public ViewResolver viewResolver() {
  5. ThymeleafViewResolver resolver = new ThymeleafViewResolver();
  6. resolver.setTemplateEngine(templateEngine());
  7. resolver.setCharacterEncoding("UTF-8");
  8. return resolver;
  9. }
  10. @Bean
  11. public TemplateEngine templateEngine() {
  12. SpringTemplateEngine engine = new SpringTemplateEngine();
  13. engine.setEnableSpringELCompiler(true);
  14. engine.setTemplateResolver(templateResolver());
  15. return engine;
  16. }
  17. @Bean
  18. public ITemplateResolver templateResolver() {
  19. SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
  20. resolver.setPrefix("classpath:/templates/");
  21. resolver.setSuffix(".html");
  22. resolver.setTemplateMode(TemplateMode.HTML);
  23. resolver.setCharacterEncoding("UTF-8");
  24. return resolver;
  25. }
  26. @Override
  27. public void addInterceptors(InterceptorRegistry registry) {
  28. registry.addInterceptor(new LoggingInterceptor());
  29. }
  30. }

HTML示例如下:error.html可以工作,但相同的login.html无法看到。另外,Thymeleaf可以很好地看到/static中的.css.js...

  1. <!DOCTYPE html>
  2. <html xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <title>Error Page</title>
  5. <link rel="stylesheet" type="text/css" th:href="@{index.css}">
  6. </head>
  7. <body>
  8. <div class="navbar">
  9. <div class="navbar-logo">
  10. <button onclick="changeColor()" class="color-button">
  11. </button>
  12. </div>
  13. <div style="display:flex; margin-right: 40px;">
  14. <button class="auth-button" onclick="window.location.href='login.html'">Login</button>
  15. </div>
  16. </div>
  17. <script src="index.js"></script>
  18. </body>
  19. </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:

  1. @SpringBootApplication
  2. public class ChatSbAppApplication {
  3. public static void main(String[] args) {
  4. SpringApplication.run(ChatSbAppApplication.class, args);
  5. }
  6. }

Error controller (works fine):

  1. @Controller
  2. public class ErrorController implements org.springframework.boot.web.servlet.error.ErrorController {
  3. @RequestMapping(&quot;/error&quot;)
  4. public String handleError() {
  5. return &quot;error&quot;;
  6. }
  7. @Override
  8. public String getErrorPath() {
  9. return &quot;/error&quot;;
  10. }
  11. }

Standard controllers:

  1. @Controller
  2. public class UserAccountController {
  3. private final UserAccountService userAccountService;
  4. @Autowired
  5. public UserAccountController(UserAccountService userAccountService) {
  6. this.userAccountService = userAccountService;
  7. }
  8. ///registration
  9. @GetMapping(&quot;/register&quot;)
  10. public String showRegistrationForm() {
  11. return &quot;register&quot;;
  12. }
  13. @PostMapping(&quot;/register&quot;)
  14. public String registerUser(@RequestBody UserAccount userAccount) {
  15. userAccountService.saveUserAccount(userAccount);
  16. return &quot;redirect:/login&quot;;
  17. }
  18. ///authorisation
  19. @GetMapping(&quot;/login&quot;)
  20. public String loginUser() {
  21. return &quot;login&quot;;
  22. }
  23. @PostMapping(&quot;/login&quot;)
  24. public String processLoginForm() {
  25. return &quot;redirect:/chats&quot;;
  26. }
  27. }

application.properties:

  1. spring.datasource.url=jdbc:postgresql://localhost:5432/ChatDB
  2. spring.datasource.username=postgres
  3. spring.datasource.password=1234
  4. spring.datasource.driver-class-name=org.postgresql.Driver
  5. server.error.whitelabel.enabled = false
  6. spring.jpa.open-in-view=false

I also tried changing the config, but it didn't work either.

  1. @Configuration
  2. public class WebConfig implements WebMvcConfigurer {
  3. @Bean
  4. public ViewResolver viewResolver() {
  5. ThymeleafViewResolver resolver = new ThymeleafViewResolver();
  6. resolver.setTemplateEngine(templateEngine());
  7. resolver.setCharacterEncoding(&quot;UTF-8&quot;);
  8. return resolver;
  9. }
  10. @Bean
  11. public TemplateEngine templateEngine() {
  12. SpringTemplateEngine engine = new SpringTemplateEngine();
  13. engine.setEnableSpringELCompiler(true);
  14. engine.setTemplateResolver(templateResolver());
  15. return engine;
  16. }
  17. @Bean
  18. public ITemplateResolver templateResolver() {
  19. SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
  20. resolver.setPrefix(&quot;classpath:/templates/&quot;);
  21. resolver.setSuffix(&quot;.html&quot;);
  22. resolver.setTemplateMode(TemplateMode.HTML);
  23. resolver.setCharacterEncoding(&quot;UTF-8&quot;);
  24. return resolver;
  25. }
  26. @Override
  27. public void addInterceptors(InterceptorRegistry registry) {
  28. registry.addInterceptor(new LoggingInterceptor());
  29. }
  30. }

HTML example below: error.html works, but same login.html does not see. Also thymeleaf saw fine .css/.js in /static...

  1. &lt;!DOCTYPE html&gt;
  2. &lt;html xmlns:th=&quot;http://www.thymeleaf.org&quot;&gt;
  3. &lt;head&gt;
  4. &lt;title&gt;Error Page&lt;/title&gt;
  5. &lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; th:href=&quot;@{index.css}&quot;&gt;
  6. &lt;/head&gt;
  7. &lt;body&gt;
  8. &lt;div class=&quot;navbar&quot;&gt;
  9. &lt;div class=&quot;navbar-logo&quot;&gt;
  10. &lt;button onclick=&quot;changeColor()&quot; class=&quot;color-button&quot;&gt;
  11. &lt;/button&gt;
  12. &lt;/div&gt;
  13. &lt;div style=&quot;display:flex; margin-right: 40px;&quot;&gt;
  14. &lt;button class=&quot;auth-button&quot; onclick=&quot;window.location.href=&#39;login.html&#39;&quot;&gt;Login&lt;/button&gt;
  15. &lt;/div&gt;
  16. &lt;/div&gt;
  17. &lt;script src=&quot;index.js&quot;&gt;&lt;/script&gt;
  18. &lt;/body&gt;
  19. &lt;/html&gt;

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 Thymeleaf在模板中看不到页面HTML。

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 打开链接需要设置完整路径。

尝试替换:

  1. <button class="auth-button" onclick="window.location.href='login.html'">Login</button>

为:

  1. <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

  1. &lt;button class=&quot;auth-button&quot; onclick=&quot;window.location.href=&#39;login.html&#39;&quot;&gt;Login&lt;/button&gt;

With

  1. &lt;button class=&quot;auth-button&quot; onclick=&quot;window.location.href=&#39;http://localhost:8080/login&#39;&quot;&gt;Login&lt;/button&gt;

Also note you should not include .html extension since controller takes care of this when parsing templates

huangapple
  • 本文由 发表于 2023年7月7日 02:05:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76631478.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定