这是一个在Tomcat上使用Thymeleaf、idea、maven的springMVC程序,Tomcat可以正常运行,程序可以进入Controller,但是一旦Controller返回Thymeleaf的html文件,Tomcat就会返回500 web,看来springMVC未能示例化ThymeleafView,我不知道为什么会发生这种情况,这是我的springMVC.xml web.xml和错误报告
springMVC.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="helloworld.Controller"></context:component-scan>
<bean id="viewResolver"
class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
<property name="order" value="1"/>
<property name="characterEncoding" value="UTF-8"/>
<property name="templateEngine">
<bean class="org.thymeleaf.spring5.SpringTemplateEngine">
<property name="templateResolver">
<bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
<!-- 视图前缀 -->
<property name="prefix" value="/WEB-INF/templates/"/>
<!-- 视图后缀 -->
<property name="suffix" value=".html"/>
<property name="templateMode" value="HTML"/>
<property name="characterEncoding" value="UTF-8"/>
</bean>
</property>
</bean>
</property>
</bean>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
错误报告
类型 异常报告
消息 Request processing failed: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.thymeleaf.spring5.view.ThymeleafView]: Unresolvable class definition
描述 服务器遇到一个意外的情况,阻止它完成请求。
例外情况
jakarta.servlet.ServletException: Request processing failed: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.thymeleaf.spring5.view.ThymeleafView]: Unresolvable class definition
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1019)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:903)
jakarta.servlet.http.HttpServlet.service(HttpServlet.java:683)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:885)
jakarta.servlet.http.HttpServlet.service(HttpServlet.java:792)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
根本原因。
org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.thymeleaf.spring5.view.ThymeleafView]: Unresolvable class definition
org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:150)
org.thymeleaf.spring5.view.ThymeleafViewResolver.loadView(ThymeleafViewResolver.java:837)
org.thymeleaf.spring5.view.ThymeleafViewResolver.createView(ThymeleafViewResolver.java:796)
org.springframework.web.servlet.view.AbstractCachingViewResolver.resolveViewName(AbstractCachingViewResolver.java:184)
org.springframework.web.servlet.DispatcherServlet.resolveViewName(DispatcherServlet.java:1455)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1390)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1158)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1097)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:973)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1011)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:903)
jakarta.servlet.http.HttpServlet.service(HttpServlet.java:683)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:885)
jakarta.servlet.http.HttpServlet.service(HttpServlet.java:792)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
根本原因。
java.lang.NoClassDefFoundError: javax/servlet/ServletException
java.base/java.lang.Class.getDeclaredConstructors0(Native Method)
java.base/java.lang.Class.privateGetDeclaredConstructors(Class.java:3373)
java.base/java.lang.Class.getConstructor0(Class.java:3578)
java.base/java.lang.Class.getDeclaredConstructor(Class.java:2754)
org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:141)
org.thymeleaf.spring5.view.ThymeleafViewResolver.loadView(ThymeleafViewResolver.java:837)
org.thymeleaf.spring5.view.ThymeleafViewResolver.createView(ThymeleafViewResolver.java:796)
org.springframework.web.servlet.view.AbstractCachingViewResolver.resolveViewName(AbstractCachingViewResolver.java:184)
org.springframework.web.servlet.DispatcherServlet.resolveViewName(DispatcherServlet.java:1455)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1390)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1158)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1097)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:973)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1011)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:903)
jakarta.servlet.http.HttpServlet.service(HttpServlet.java:683)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:885)
jakarta.servlet.http.HttpServlet.service(HttpServlet.java:792)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
根本原因。
java.lang.ClassNotFoundException: javax.servlet.ServletException
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1449)
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1257)
java.base/java.lang.Class.getDeclaredConstructors0(Native Method)
java.base/java.lang.Class.privateGetDeclaredConstructors(Class.java:3373)
java.base/java.lang.Class.getConstructor0(Class.java:3578)
java.base/java.lang.Class.getDeclaredConstructor(Class.java:2754)
org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:141)
org.thymeleaf.spring5.view.ThymeleafViewResolver.loadView(ThymeleafViewResolver.java:837)
org.thymeleaf.spring5.view.ThymeleafViewResolver.createView(ThymeleafViewResolver.java:796)
org.springframework.web.servlet.view.AbstractCachingViewResolver.resolveViewName(AbstractCachingViewResolver.java:184)
org.springframework.web.servlet.DispatcherServlet.resolveViewName(DispatcherServlet.java:1455)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1390)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1158)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1097)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:973)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1011)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:903)
jakarta.servlet.http.HttpServlet.service(HttpServlet.java:683)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:885)
jakarta.servlet.http.HttpServlet.service(HttpServlet.java:792)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
根据错误报告,我尝试在maven中添加以下依赖项
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
并且ServletDispather似乎无法部署,因为tomcat错误报告更改为404
1条答案
按热度按时间o8x7eapl1#
感谢M.代努姆的评论,这正是问题的原因,我的tomcat版本是10,我的springMVC版本是6,但我使用的是Thymleaf-spring 5,问题在我将其改为Thymleaf-spring 6后得到解决,我之前没有考虑版本。