为什么是“重定向”? (也就是说,`return" redirect:/ myPage"`)不能用于我的Tiles页面定义? [英] Why is a "redirect" (that is, `return "redirect:/myPage"`) not working for my Tiles page definition?

查看:142
本文介绍了为什么是“重定向”? (也就是说,`return" redirect:/ myPage"`)不能用于我的Tiles页面定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我的重定向(即返回redirect:/ myPage)无效?

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

注意:myPage是 Tiles 页面定义。

Note: "myPage" is a Tiles page definition.

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

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

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

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].

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

(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.

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

What might be preventing me from redirecting to the Tiles page definition: "myPage"?

    <?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(摘录)



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



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



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浏览器。

  • Some environment information:

    • Spring v3.2.x
    • Tiles v2.2
    • Java v1.6x
    • Internet Explorer 8 browser.
    • 推荐答案

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

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

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

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

      我必须将以下公共ModelAndView myPage(HttpSession会话)方法添加到Controller(请参阅下面的代码段),以便执行成功的重定向(即,从另一个控制器)...即,使用

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

      问题是myPage控制器包含 public ModelAndView myPage(HttpSession session)方法......(再次感谢Sotirius)。

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

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

      这篇关于为什么是“重定向”? (也就是说,`return&quot; redirect:/ myPage&quot;`)不能用于我的Tiles页面定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆