Why is a “redirect” (that is, `return “redirect:/myPage”`) not working for my Tiles page definition?

試著忘記壹切 提交于 2019-12-23 03:29:51

问题


Why is my "redirect" (that is, return "redirect:/myPage") not working?

Note: "myPage" is a Tiles page definition.

(FWIW, the solution to a "double-submit" issue I am having is use a redirect to "myPage". But I am unable to get the "redirect" to work.)

So far, I've tried the following.

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"))

But, to no avail. None of the above URLs redirects work. That is, I get HTTP Status 404 - [description, the requested resource is not available].

(Note: the main layout template, and the JSP files that comprise the "myPage" page definition are located in a "WEB-INF/views" folder.)

Below is some relevant configuration information.

What might be preventing me from redirecting to the Tiles page definition: "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 (snippet)

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

Some environment information:

  • Spring v3.2.x
  • Tiles v2.2
  • Java v1.6x
  • Internet Explorer 8 browser.

回答1:


Thanks to hints left by Sotirios' comments, I discovered that my inability to "redirect" to "myPage" had nothing whatever to do with Tiles.

Rather, after a harder look, I realized that it was the configuration of the controller associated with "myPage".

I had to add the following "public ModelAndView myPage(HttpSession session)" method to the Controller (see snippet, below), in order to perform a successful redirect (i.e., from another controller)...i.e., using

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

The problem was that the "myPage" controller did not contain an "public ModelAndView myPage(HttpSession session)" method...(thanks again, Sotirius).

Below, is the "public ModelAndView myPage(HttpSession session)" method - code snippet)

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


来源:https://stackoverflow.com/questions/18023965/why-is-a-redirect-that-is-return-redirect-mypage-not-working-for-my-ti

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!