Struts2_使用token拦截器控制重复提交(很少用)

Posted 流年如水~烟雨随风

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Struts2_使用token拦截器控制重复提交(很少用)相关的知识,希望对你有一定的参考价值。

控制重复提交的方式:1、表单提交后页面重定向;2、Struts2.x token拦截器

大致流程:

例子:

index.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%
 4 String path = request.getContextPath();
 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 6 %>
 7 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 8 <html>
 9 <head>
10 <base href="<%=basePath %>"/>
11 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
12 <title>Hello Struts2</title>
13 </head>
14 <body>
15     <a href="input.action">输入用户信息</a>
16 </body>
17 </html>

input.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%
 4 String path = request.getContextPath();
 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 6 %>
 7 <%@ taglib uri="/struts-tags" prefix="s" %>
 8 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 9 <html>
10 <head>
11 <base href="<%=basePath %>"/>
12 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
13 <title>Hello Struts2</title>
14 </head>
15 <body>
16     <form action="user.action" method="post">
17         name:<input type="text" name="name"/>
18         age:<input type="text" name="age"/>
19         <input type="submit" value="提 交"/>
20         <s:token></s:token>
21     </form>
22 </body>
23 </html>

addOK.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%
 4 String path = request.getContextPath();
 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 6 %>
 7 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 8 <html>
 9 <head>
10 <base href="<%=basePath %>"/>
11 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
12 <title>Hello Struts2</title>
13 </head>
14 <body>
15     addOK.
16 </body>
17 </html>

error.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%
 4 String path = request.getContextPath();
 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 6 %>
 7 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 8 <html>
 9 <head>
10 <base href="<%=basePath %>"/>
11 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
12 <title>Hello Struts2</title>
13 </head>
14 <body>
15     严禁做重复的事!
16 </body>
17 </html>

struts.xml

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">
 5 
 6 <struts>
 7     <constant name="struts.enable.DynamicMethodInvocation" value="true" />
 8     <constant name="struts.configuration.xml.reload" value="true"/>
 9     
10     <package name="test" namespace="/" extends="struts-default">
11     
12         <action name="input" class="com.bjsxt.action.InputAction">
13             <result>/input.jsp</result>
14         </action>
15     
16         <action name="user" class="com.bjsxt.action.UserAction">
17             <result>/addOK.jsp</result>
18             <interceptor-ref name="defaultStack"></interceptor-ref>
19             <interceptor-ref name="token"></interceptor-ref>
20             <result name="invalid.token">/error.jsp</result>
21         </action>
22         
23     </package>
24     
25 </struts>

InputAction

 1 package com.bjsxt.action;
 2 
 3 import com.opensymphony.xwork2.ActionSupport;
 4 
 5 public class InputAction extends ActionSupport {
 6 
 7     private static final long serialVersionUID = -8003780600877800393L;
 8     
 9     public String execute(){
10         return SUCCESS;
11     }
12     
13 }

UserAction

 1 package com.bjsxt.action;
 2 
 3 import com.opensymphony.xwork2.ActionSupport;
 4 
 5 public class UserAction extends ActionSupport {
 6 
 7     private static final long serialVersionUID = -8003780600877800393L;
 8     
 9     private String name;
10     
11     private int age;
12     
13     public String execute(){
14         System.out.println("a user added!");
15         return SUCCESS;
16     }
17     
18     public String getName() {
19         return name;
20     }
21 
22     public void setName(String name) {
23         this.name = name;
24     }
25 
26     public int getAge() {
27         return age;
28     }
29 
30     public void setAge(int age) {
31         this.age = age;
32     }
33     
34 }

链接: http://pan.baidu.com/s/1mi1BnoW 密码: rpee

以上是关于Struts2_使用token拦截器控制重复提交(很少用)的主要内容,如果未能解决你的问题,请参考以下文章

Struts2系列:(13)防表单重复提交(token + 拦截器)

Struts2--二次提交

struts2框架之重复提交问题

防重复提交利器--struts2令牌

Struts2 06--系统拦截器防止数据重复提交

struts2 防止表单的重复提交