spring 3.0 + tiles 2.1重定向问题

时间:2010-12-06 10:13:25

标签: spring-mvc tiles

我在尝试解决问题时遇到了问题。在index.jsp(应用程序启动的地方)我有...

<jsp:forward page="hello.do"></jsp:forward>

然后,在web.xml中我定义了servlet调度程序:

<!-- Servlet para levantar el dispatcher servlet de spring MVC -->
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

因此,以.do结尾的所有内容都应由控制器执行。这样可行 。定义的控制器是:

@Controller
@RequestMapping("/hello")
public class HelloWorldController {
    @RequestMapping(method=RequestMethod.GET)
    public String procesar(HttpServletRequest request,ModelMap modelo){
        System.out.println("todo jodidamente correcto");
        String mensaje = "Hola, perras";

        modelo.addAttribute("message",mensaje);

        return "hello";
    }
}

我可以看到消息,但是当它到达时它会失败 - 返回“你好” - ,我仍然有相同的例外:

javax.servlet.ServletException: Could not resolve view with name 'hello' in servlet with name 'dispatcher'
org.springframework.web.servlet.DispatcherServlet. render(DispatcherServlet.java:1013)
org.springframework.web.servlet.DispatcherServlet. doDispatch(DispatcherServlet.java:815)
org.springframework.web.servlet.DispatcherServlet. doService(DispatcherServlet.java:717)
org.springframework.web.servlet.FrameworkServlet.p rocessRequest(FrameworkServlet.java:644)
org.springframework.web.servlet.FrameworkServlet.d oGet(FrameworkServlet.java:549)
javax.servlet.http.HttpServlet.service(HttpServlet .java:617)
javax.servlet.http.HttpServlet.service(HttpServlet .java:717)

我的瓷砖定义是:

<tiles-definitions>
    <definition name="topfisio.layout" template="/layouts/three-layer.jsp">
        <put-attribute name="title" value="Top fisio" />
        <put-attribute name="header" value="/jsp/header.jsp" />
        <put-attribute name="footer" value="/jsp/footer.jsp" />
    </definition>


    <definition name="*" extends="topfisio.layout">
        <put-attribute name="title" value="{1}" />
        <put-attribute name="content" value="/jsp/{1}.jsp"/>
    </definition>
</tiles-definitions>

我的文件结构是:

index.jsp

WEB-INF
WEB-INF -- dispatcher-servlet.xml
WEB-INF -- tiles-def.xml
WEB-INF -- web.xml
layouts
layouts -- three-layer.jsp
jsp
jsp -- footer.jsp
jsp -- header.jsp
jsp -- hello.jsp

我的瓷砖定义可能有问题,但我仍然无法找到它

1 个答案:

答案 0 :(得分:1)

你必须在dispatcher-servlet.xml中配置tile的解析器,所以当你在控制器中返回一个字符串时,Spring会遍历你所有的解析器,试图寻找合适的解析器:

<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
    <property name="definitions">
        <list>
            <value>/WEB-INF/tiles-def.xml</value>
        </list>
    </property>
</bean>

<bean id="tilesResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
     <property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView"/>
     <!-- In case you have more than one resolver you can set the order here -->
     <!-- The order 0 will be the first one in the order -->
     <property name="order" value="1"/>              

更多信息here