我正在使用Thymeleaf作为我的Spring mvc的一部分。但是我在浏览器发出请求时无法获取视图页面。
请在下面找到Thymeleaf配置:
<bean id="templateResolver"
class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">
<property name="prefix" value="/WEB-INF/templates/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
</bean>
<bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
</bean>
<bean id="thymeleafViewResolver" class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
<property name="order" value="1" />
<property name="viewNames" value="*.html" />
</bean>
我的Controller类定义如下:
@Controller
public class PersonController {
@RequestMapping(value = "/home.html", method = RequestMethod.GET)
public String navigateToPerson() {
return "home";
}
}
我的观看页面home.html
位于:\WEB-INF\templates\home.html
我已经在weblogic控制台中检查了url的映射:
INFO: Mapped "{[/home.html],methods=[GET]}" onto public java.lang.String com.userapp.web.PersonController.navigateToPerson()
我的web.xml如下:
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/applicationConfig/applicationConfig.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
现在,当我向浏览器发出如下请求时: http://localhost:7040/UserApplication/home.html
我在weblogic控制台中收到以下错误:
Root cause of ServletException.
javax.servlet.ServletException: Could not resolve view with name 'home' in
servlet with name 'DispatcherServlet'
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1266)
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1041)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:984)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
Truncated. see log file for complete stacktrace
答案 0 :(得分:0)
我最近也遇到了同样的问题,我删除了 viewNames
根据您的xml,您拥有
<bean id="thymeleafViewResolver" class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
<property name="order" value="1" />
<property name="viewNames" value="*.html" />
</bean>
请删除* viewNames **属性
<bean id="thymeleafViewResolver" class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
<property name="order" value="1" />
</bean>
那应该解决它。
依赖性和版本
spring-webmvc ----> 5.2.1
thymeleaf-spring5 ----> 3.0.11
javax.servlet-api ----> 4.0.1
注意:同样的情况适用于基于Java的配置,只需删除 setViewNames config(实现此修复程序的代码库,使用基于Java的配置)...我刚从Thymeleaf开始,以及为什么它可以解决它,我真的不知道(任何知道原因的人都应该发表评论)
希望这对您有帮助