2

プロジェクトのSpring MVCとThymeleafから始めます-しかし、インデックスページの読み込みの問題に直面しています-

私のコントローラーは次のとおりです-

@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ファイルは

<?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

ただし、次の例外が発生します-

javax.servlet.ServletException: Could not resolve view with name 'index1' in servlet with name「スプリングMVC」

ファイルを間違った場所に配置したり、コントローラーに何か不足していますか?

4

1 に答える 1

6

それを解決しました.viewResolverに.htmlで終わる名前のみを解決するように指示していたので、拡張子を付けてビュー名を返さなければなりませんでした..このスレッドは、答えhttp://forum.thymeleaf.org/Issue-with -my-Thymeleaf-Spring-configuration-td4024996.html

于 2012-10-28T00:21:54.620 に答える