Spring Boot Thymeleaf模板解析错误求助:chatloginpage模板无法找到
解决Thymeleaf模板解析错误:找不到chatloginpage模板
我们在Spring Boot项目中使用Thymeleaf进行POC验证,访问localhost:8080/chatstart时触发如下错误:
Error resolving template [chatloginpage], template might not exist or might not be accessible by any of the configured Template Resolvers.
已查阅相关解答但问题未解决,按Spring Boot默认规则,HTML页面直接放在templates目录下即可,无需额外配置或WEB-INF目录,怀疑该错误是否与同时使用REST服务有关。
相关配置信息
pom.xml依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.6</version> <relativePath/> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web-services</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-rest</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies>
非REST部分主控制器代码
@Controller @EnableWebMvc public class StartChatController { @RequestMapping( path = "/chatstart", method = RequestMethod.GET) public String index(HttpServletRequest request, Model model) { String username = (String) request.getSession().getAttribute("username"); if (username == null || username.isEmpty()) { return "redirect:/chatlogin"; } model.addAttribute("username", username); return "chatting"; } @RequestMapping(path = "/chatlogin", method = RequestMethod.GET) public String showLoginPage() { return "chatloginpage"; } @RequestMapping(path = "/chatlogin", method = RequestMethod.POST) public String doLogin(HttpServletRequest request, @RequestParam(defaultValue = "") String username) { username = username.trim(); if (username.isEmpty()) { return "chatloginpage"; } request.getSession().setAttribute("username", username); return "redirect:/chatstart"; } @RequestMapping(path = "/chatlogout", method = RequestMethod.POST) public String logout(HttpServletRequest request) { request.getSession(true).invalidate(); return "redirect:/chatlogin"; } }
资源目录结构
网页资源均位于resources目录下:
- main.css(内容省略)
- main.js(内容省略)
- templates/chatloginpage.html
- templates/chatting.html
resources/templates/chatloginpage.html内容
<!DOCTYPE html> <html> <head> <title>Login</title> <link rel="stylesheet" href="../main.css"/> </head> <body> <div id="login-container"> <h1 class="title">Enter your username</h1> <form id="loginForm" name="loginForm" method="POST"> <input type="text" name="username"/> <button action="chatlogin" type="submit">Login</button> </form> </div> </body> </html>
resources/templates/chatting.html内容
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Spring Boot WebSocket</title> <link rel="stylesheet" href="../main.css" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/sockjs-client/1.1.4/sockjs.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/stomp.js/2.3.3/stomp.min.js"></script> </head> <body> <div id="chat-container"> <div class="chat-header"> <div class="user-container"> <span id="username" th:utext="${username}"></span> <a th:href="@{/chatlogout}">Logout</a> </div> <h3>Spring WebSocket Chat Demo</h3> </div> <hr/> <div id="connecting">Connecting...</div> <ul id="messageArea"> </ul> <form id="messageForm" name="messageForm"> <div class="input-message"> <input type="text" id="message" autocomplete="off" placeholder="Type a message..."/> <button type="submit">Send</button> </div> </form> </div> <script href="../main.js"></script> </body> </html>
请帮忙排查并解决这个模板解析错误。
内容的提问来源于stack exchange,提问作者tm1701
相关产品推荐
相关产品推荐

