我是 Thymeleaf 的新手。我正在 View 部分中使用 thymeleaf 创建一个 spring-boot 应用程序。
请在下面找到我的 pom.xml 文件[仅依赖项部分]:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
我在以下位置创建了一个 application.properties 文件:src/main/resources
server.port = 8086
spring.mvc.static-path-pattern=/resources/**
spring.thymeleaf.prefix=/WEB-INF/views/
spring.thymeleaf.suffix=.html
以下是我的启动应用程序主类:
@SpringBootApplication
@ComponentScan(basePackages="<common package structure>")
public class AdvMain extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(AdvMain.class);
}
public static void main(String[] args) {
SpringApplication.run(AdvMain.class, args);
}
}
我有一个 Controller 类,如下所示:
@Controller
public class HomeController {
@RequestMapping(path = "/info", method = { RequestMethod.GET, RequestMethod.POST })
public String info(Model model) {
model.addAttribute("message", "Thymeleaf");
return "info";
}
@RequestMapping(path = "/home", method = RequestMethod.GET)
public String index() {
return "index";
}
}
当我调用以下网址时:
http://localhost:8086/AddViewer/home
我收到页面未找到错误。
请在下面找到我的 tomcat 控制台日志:
Mapped "{[/home],methods=[GET]}" onto public java.lang.String <package_Structure>.HomeController.index()
Mapped "{[/info],methods=[GET || POST]}" onto public java.lang.String <package_Structure>.HomeController.info(org.springframework.ui.Model)
Mapped URL path [/resources/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
Tomcat started on port(s): 8086 (http)
FrameworkServlet 'dispatcherServlet': initialization completed in 29 ms
No mapping found for HTTP request with URI [/AddViewer/home] in DispatcherServlet with name 'dispatcherServlet'
谁能提供任何合适的解决方案吗???
最佳答案
这不是 thymeleaf 的问题。
在控制台消息中,您有两个映射:/home
和 /info
。
Mapped "{[/home],methods=[GET]}" onto public java.lang.String <package_Structure>.HomeController.index()
Mapped "{[/info],methods=[GET || POST]}" onto public java.lang.String <package_Structure>.HomeController.info(org.springframework.ui.Model)
但是您发出了请求/AddViewer/home
。因此,它与请求映射不匹配。
这样请求。
http://localhost:8086/home
关于java - 无法使用 Thymeleaf 解析 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49072235/