为什么“重定向”(即“返回”重定向:/ myPage“`)不能用于我的Tiles页面定义?

时间:2013-08-02 18:41:38

标签: java jsp tiles

为什么我的“重定向”(即return "redirect:/myPage")无效?

注意:“myPage”是Tiles页面定义。

(FWIW,我所遇到的“双重提交”问题的解决方案是使用重定向到“myPage”。但我无法让“重定向”工作。)

到目前为止,我已经尝试了以下内容。

return new ModelAndView("redirect:/myPage"))
return new ModelAndView("redirect:/myPage.jsp"))

return new ModelAndView("redirect:/views/myPage"))
return new ModelAndView("redirect:/views/myPage.jsp"))

return new ModelAndView(new RedirectView("myPage"))
return new ModelAndView(new RedirectView("myPage.jsp"))

return new ModelAndView(new RedirectView("/views/myPage"))
return new ModelAndView(new RedirectView("/views/myPage.jsp"))

return new ModelAndView(new RedirectView("/WEB-INF/views/myPage"))
return new ModelAndView(new RedirectView("/WEB-INF/views/myPage.jsp"))

但是,无济于事。以上网址都不会重定向工作。也就是说,我得到HTTP状态404 - [描述,请求的资源不可用]。

(注意:主要布局模板和构成“myPage”页面定义的JSP文件位于“WEB-INF / views”文件夹中。)

以下是一些相关的配置信息。

什么可能阻止我重定向到Tiles页面定义:“myPage”?


的applicationContext.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans:beans xmlns="http://www.springframework.org/schema/mvc"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:beans="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:p="http://www.springframework.org/schema/p"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

        <context:component-scan base-package="aaa.bbb.ccc" />
        <context:annotation-config />
        <mvc:annotation-driven/>
        <resources mapping="/resources/**" location="/resources/" />

        <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <beans:property name="prefix" value="/WEB-INF/views/" />
            <beans:property name="suffix" value=".jsp" />
        </beans:bean>

        <beans:bean id="tilesViewResolver1"
                    class="org.springframework.web.servlet.view.tiles2.TilesViewResolver"
                    p:order="0"/>

        <beans:bean id="tilesViewResolver2" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
            <beans:property name="viewClass">
                <beans:value>org.springframework.web.servlet.view.tiles2.TilesView</beans:value>
            </beans:property>
        </beans:bean>

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

web.xml(摘录)

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5"
             xmlns="http://java.sun.com/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    -
    -
    -
        <servlet>
            <servlet-name>appServlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>/WEB-INF/servlet-context.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>

        <servlet-mapping>
            <servlet-name>appServlet</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    -
    -
    -
    </web-app>

tiles.xml

    <tiles-definitions>
        <definition name="mainpagedef" template="/WEB-INF/views/layout01.jsp">
            <put-attribute name="title" value="My Page" type="string"/>
            <put-attribute name="cssPage1" value="/WEB-INF/views/stylesheet.jsp" />
            <put-attribute name="jsPage1" value="" />
            <put-attribute name="jsPage2" value="" />
            <put-attribute name="body" value="" />
            <put-attribute name="footer" value="/WEB-INF/views/footer.jsp" />
        </definition>
        -
        -
        -
        <definition name="myPage" extends="mainpagedef">
            <put-attribute name="jsPage1" value="/WEB-INF/views/javascript.jsp" />
            <put-attribute name="body">
                <definition template="/WEB-INF/views/subpage01.jsp">
                    <put-attribute name="header" value="/WEB-INF/views/header.jsp" />
                    <put-attribute name="main" value="/WEB-INF/views/main.jsp" />
                </definition>
            </put-attribute>
        </definition>
        -
        -
        -
    </tiles-definitions>

layout01.jsp

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <%@taglib prefix="spring" uri="http://www.springframework.org/tags"%>
    <%@taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>

    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8; pageEncoding=UTF-8">
            <title><tiles:getAsString name="title" /></title>
            <tiles:insertAttribute name="cssPage1" />
            <tiles:insertAttribute name="jsPage1" />
            <tiles:insertAttribute name="jsPage2" />
            <script type="text/javascript" src="${pageContext.request.contextPath}/resources/js/main.js"></script>
        </head>
        <body>
            <div class="page">
                <div class="container">
                    <tiles:insertAttribute name="body" />
                    <tiles:insertAttribute name="footer" />
                </div>
            </div>
        </body>
    </html>

subpage01.jsp

    <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
    <%@taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
    <div class="page">
        <div class="container">
            <div>
                <tiles:insertAttribute name="header" />
            </div>
            <div>
                <tiles:insertAttribute name="main" />
            </div>
        </div>
    </div>

一些环境信息:

  • Spring v3.2.x
  • Tiles v2.2
  • Java v1.6x
  • Internet Explorer 8浏览器。

1 个答案:

答案 0 :(得分:5)

感谢Sotirios的评论留下的暗示,我发现我无法“重定向”到“myPage”与Tiles无关。

相反,在更难看之后,我意识到这是与“myPage”相关联的控制器的配置。

我不得不向Controller添加以下“public ModelAndView myPage(HttpSession session)”方法(参见下面的代码片段),以便执行成功的重定向(即,从另一个控制器)...即,使用< / p>

return new ModelAndView("redirect:/myPage");

问题是“myPage”控制器包含“公共ModelAndView myPage(HttpSession会话)”方法......(再次感谢Sotirius)。

下面是“public ModelAndView myPage(HttpSession session)”方法 - 代码片段

    @Controller
    @Scope("session")
    @SessionAttributes(
        {
        "sharedList"
    })    
    public class MyPageController implements Serializable
    {

        @ModelAttribute("sharedList")
        public List<Pojo1> createSharedList()
        {
            return new ArrayList<Pojo1>();
        }


        @RequestMapping(value = "/myPage", method = RequestMethod.GET)
        @ResponseBody
        public ModelAndView myPage(HttpSession session)
        {
            createSharedList();
            return new ModelAndView("myPage");
        }
    -
    -
    -