问题描述
从 Spring MVC 和 Thymeleaf 开始用于一个项目 - 但我面临加载索引页面的问题 -
Starting out with Spring MVC and Thymeleaf for a project - But I am facing the issue of loading the index page -
我的控制器如下 -
@Controller
public class HomeController {
@RequestMapping(method=RequestMethod.GET, value="/")
public ModelAndView index(){
ModelAndView mav = new ModelAndView();
mav.setViewName("index");
mav.addObject("user", new UserCredential());
return mav;
}
而我的 servlet.xml 文件是
and my servlet.xml file is
<?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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<context:component-scan base-package="web.controller" />
<!-- Enabling Spring MVC configuration through annotations -->
<mvc:annotation-driven />
<!-- Mapping Static Resources -->
<mvc:resources mapping="/resources/**" location="/resources/" />
<bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
</bean>
<bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
</bean>
<bean class="org.thymeleaf.spring3.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
<property name="order" value="1" />
<property name="viewNames" value="*.html"/>
</bean>
</beans>
我的文件结构是——
WebContent
-- WEB_INF
-- views
-- index.html
但是我得到以下异常 -
However I get the following exception -
javax.servlet.ServletException: 无法解析名称为index1"的 servlet 中的视图
'springMVC'
javax.servlet.ServletException: Could not resolve view with name 'index1' in servlet with name
'springMVC'
我是否将文件放在了错误的位置或我的控制器中遗漏了什么?
Am I placing the file in the wrong place or missing something in my controller?
推荐答案
解决了它,我不得不返回带有扩展名的视图名称,因为我告诉我的 viewResolver 只解析以 .html 结尾的名称..该线程将我指向了答案 http://forum.thymeleaf.org/Issue-with-my-Thymeleaf-Spring-configuration-td4024996.html
Solved it, I had to return the view name with the extension since I was telling my viewResolver to only resolve to names ending in .html ..This thread pointed me to the answer http://forum.thymeleaf.org/Issue-with-my-Thymeleaf-Spring-configuration-td4024996.html
这篇关于Spring 与 Thymeleaf 视图集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!