`

使用redirect避免重复提交 ,struts1.x struts2.x

 
阅读更多

 

 

 

避免重复提交这个问题在Web应用开发领域应该是一个老生常谈的问题了,主流的一些Web应用框架也提供了方便使用的功能来实现这个需求,比如Struts的Token。但是有些时候,可能用户确实需要刷新提交之后所转到的页面,比如假设如下的需求:用户注册了一个账号,但是这个账号需要管理员批准才可以进行工作,于是,用户在注册页面填写了详细信息之后,提交,转到详细信息页面,然后刷新这个页面来查看自己的状态。此时,我觉得使用redirect是非常方便的。

以下是redirect在Struts1和Struts2中的实现:

 

Struts1

在Struts1中,有一个org.apache.struts.action.ActionRedirect的类,这个类是ActionForward类的子类,是专门用来做redirect跳转的。使用起来非常的简单,只需在你的Action类的方法中,返回一个ActionRedirect类的实例即可。

----------------------------------------------------

ActionRedirect redirect = new ActionRedirect(mapping.findForward("detailAction")); // 这里是在struts-config.xml文件中定义的<forward>节点的name属性
        redirect.addParameter("id", user.getId()); //这里是要在url后面附加的参数名称及其值

return redirect;

----------------------------------------------------

 

Struts2:

在Struts2中,情况有所不同。但是Struts2里面的方式,更加的合理,因为不需要在Action代码中编码来完成,完全是通过配置完成的。在配置文件struts.xml中,

----------------------------------------------------

        <action name="userCreate" method="create" class="lab.action.UserAction">
            <result name="detailAction" type="redirect-action">
                <param name="actionName">userDetail</param>
                <param name="namespace">/</param>
                <param name="parse">true</param>
                <param name="user.id">${user.id}</param>
            </result>
        </action>
        <action name="userDetail" method="detail" class="lab.action.UserAction">
            <result name="detailPage">userDetail.jsp</result>
        </action>

----------------------------------------------------

 

这里,userCreate是创建用户的Action,userDetail是查看用户详细信息的Action,都是由类lab.action.UserAction来提供工作。如果从一个action redirect到另外一个action,那么redirect的result的type为redirect-action。

其中,参数actionName为要跳转到的action的name,无需.action后缀,struts框架会根据你配置的属性来确定后缀是什么。namespace为搜索action时使用的命名空间。parse是表示要对参数进行解析。后面的几个参数就是根据实际需要来定义参数名称和参数的值,这里会由struts框架将这些参数附加到URL后面。例如在上面的例子中,user.id为参数名,${user.id}为参数值,该值通过解析userCreate对应的类中的user属性的id属性来得到,和在jsp页面使用方式相同,其实都是来自于Value Stack。

 

如果是要redirect到一个jsp页面,并且带有参数(这种情况应该很少),按照struts2的文档描述:

----------------------------------------------------

<result name="success" type="redirect">
  <param name="location">foo.jsp</param>
  <param name="parse">false</param>
</result>
<package name="passingRequestParameters" extends="struts-default" namespace="/passingRequestParameters">
   <-- Pass parameters (reportType, width and height) -->
   <!--
   The redirect-action url generated will be :
   /genReport/generateReport.jsp?reportType=pie&width=100&height=100
   -->
   <action name="gatherReportInfo" class="...">
      <result name="showReportResult" type="redirect">
         <param name="location">generateReport.jsp</param>
         <param name="namespace">/genReport</param>
         <param name="reportType">pie</param>
         <param name="width">100</param>
         <param name="height">100</param>
      </result>
   </action>
</package>

----------------------------------------------------
另外,对于比如查询统计这样的功能,用户刷新页面的几率是非常大的,这种情况下,建议使用get方式来提交form,以避免IE那个总是跳出来的对话框。

相比于forward方式的跳转而言,redirect的跳转会在浏览器地址栏暴露更多的信息,可能会被使用者恶意篡改,所以在使用redirect的时候,要对数据在后台进行更加严格和全面的校验。

 

以下是实验代码的链接,在压缩包中,去掉了struts相关的lib包,如果要在本地运行这个实验应用,只需把对应struts版本的struts<version>-blanck-<version>.war里面的lib目录下的jar文件取出则可。

实验在Windows XP SP2, resin-3.2.1, Sun JDK 1.5, struts 1.2.9/struts 2.0.11.1下运行通过。如果是使用struts1,需要把应用编译成Java 1.4格式的(如果是使用Eclipse,只需修改应用的compile level即可)。

 

转载: http://www.blogjava.net/yoda/archive/2009/01/12/250931.html

分享到:
评论

相关推荐

    Struts2入门教程(全新完整版)

    10.为什么要使用struts2代替struts1.x 7 二、struts.xml配置及例程 7 1.配置文件的优先级 7 2.配置形式 8 3.package配置相关 8 4.分工合作include:指定多个配置文件 10 5.tomcat认证访问 10 6.初识拦截器 11 7....

    Struts2防止表单重复提交

    造成重复提交主要的两个原因: ...当然你可以选择redirect方式跳转页面,这样就不会出现重复提交的问题;但有时为了达到某种效果或式。者出于网站安全的目的需要隐藏网页跳转,而不得不采用forward跳转方

    struts2漏洞.rar

    在2013年6月底发布的Struts 2.3.15版本被曝出存在重要的安全漏洞 [1] ,主要问题如下: 可远程执行服务器脚本代码 [2] 用户可以构造http://host/struts2-blank/example/X.action?action:%25{(new+java.lang....

    深入浅出Struts 2 .pdf(原书扫描版) part 1

    第15章 防止重复提交 252 15.1 标记管理 252 15.2 使用Token拦截器 253 15.3 使用Token Session拦截器 256 15.4 小结 257 第16章 调试与性能分析 258 16.1 debug标签 258 16.2 Debugging拦截器 259 16.3 性能分析 ...

    struts2实例 学生信息管理系统

    struts2实现的学生信息管理系统 &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" ...

    struts-2.3.15.1

    漏洞根源在于,DefaultActionMapper类支持以"action:"、"redirect:"、"redirectAction:"作为导航或是重定向前缀,但是这些前缀后面同时可以跟OGNL表达式,由于struts2没有对这些前缀做过滤,导致利用OGNL表达式调用...

    Struts2 in action中文版

    15.3 使用令牌防止表单重复提交 313 15.3.1 使用s:token/表单标签 313 15.3.2 令牌拦截器规则的例外 314 15.4 自动显示等待页面 316 15.5 完成CRUD操作的一个动作 317 15.5.1 CRUD 317 15.5.2 拦截器和接口 318 ...

    struts2中result类型之redirect

    struts2中result类型之redirect,重定向

    Struts2 2.3.16_doc

    This is better than the ServletRedirectResult because it does not require you to encode the URL patterns processed by the ActionMapper in to your struts.xml configuration files. This means you can ...

    深入浅出Struts2(附源码)

    第15章防止重复提交 252 15.1 标记管理 252 15.2 使用Token拦截器 253 15.3 使用Token Session拦截器 256 15.4 小结 257 第16章调试与性能分析 258 16.1 debug标签 258 16.2 Debugging拦截器 259 16.3 性能...

    Struts2_result返回类型

    在struts2的返回结果配置中,我们大部分情况使用默认的或者chain或者redirect,其实struts2还有很多其他类型的,今天我们就来看一下都有哪些类型。 打开struts2的源码中struts-default.xml文件,我们能看到如下配置

    struts2中result类型之redirectAction

    struts2中result类型之redirectAction

    Struts2+Spring3+MyBatis3完整实例

    - Parsing configuration file [struts.xml] - Initializing Struts-Spring integration... - Setting autowire strategy to name - ... initialized Struts-Spring integration successfully - Initializing Spring...

    struts_actionforward.rar_ActionForward_redirect

    ActionForward的使用 1、理解全局和局部ActionForward的概念 2、redirect的使用 3、struts-config.xml文件不允许动态修改 4、理解动态ActionForward,动态的ActionForward是可以运行期修改的

    struts2Demo

    struts2演示 &lt;br&gt;1./helloworld - helloworld 2./spring - 与spring整合 3./coc - 惯例优先配置,零配置文件 - codebehind不支持redirect,chain等操作,需要自己写jsp跳转 4./crud - CRUD,Create Read...

    struts2.1宝典

    9.将struts.xml分开 4 10全局结果result 4 11模型驱动 4 12.创建拦截器 5 13表单提交中文乱码问题 5 14.Jsp不能识别EL 5 15.自定义标签中访问Dao 获取dao 6 16.标签获取参数 6 17.Action之间传递错误验证信息 6 18第...

    struts2重定向实例源码

    struts result类型中redirect与redirectAction的使用 包括参数传递。

Global site tag (gtag.js) - Google Analytics