一尘不染

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

jsp

为什么我的“重定向”(即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"))

但是,无济于事。以上所有URL均无法重定向。即,我得到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
  • Java v1.6x
  • Internet Explorer 8浏览器。

阅读 211

收藏
2020-06-10

共1个答案

一尘不染

感谢Sotirios的评论所留下的提示,我发现无法“重定向”到“ myPage”与Tiles没有任何关系。

相反,经过一番努力,我意识到这是与“ myPage”关联的控制器的配置。

我必须将以下“ public ModelAndViewmyPage(HttpSession会话)”方法添加到Controller中(请参见下面的代码段),以便执行成功的重定向(即从另一个控制器进行重定向)…即,使用

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

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

下面是“公共ModelAndView myPage(HttpSession会话)”方法-代码段)

    @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");
        }
    -
    -
    -
2020-06-10