首页 > 应用开发 > Java > struts2防止表单重复提交
|
1
2
3
4
5
6
7
8
9
10
|
<action name="student_*" class="com.rupeng.action.StudentAction" method="{1}">
<interceptor-ref name="defaultStack"/>
<!-- 增加token拦截器 -->
<interceptor-ref name="token">
<!-- 定义被token拦截器拦截的方法 -->
<param name=“includeMethods">add</param>
</interceptor-ref>
<!-- 当表单重复提交时转向的页面 -->
<result name="invalid.token">/WEB-INF/page/resubmitError.jsp</result>
</action>
|
|
1
2
3
4
5
|
<s:form action="student_add" method="post" namespace="/test">
<s:textfield name="name/><br>
<s:textfield name="degree/><br>
<s:token/><s:submit/>
</s:form>
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
package com.rk.strut.h_token;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class StudentAction extends ActionSupport
{
private String username;
private String password;
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public String add()
{
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("user", username);
request.setAttribute("pwd", password);
return "success";
}
}
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="rk_token" namespace="/rk/token" extends="struts-default">
<action name="add" class="com.rk.strut.h_token.StudentAction" method="add">
<interceptor-ref name="token"></interceptor-ref>
<result name="success">/token/success.jsp</result>
<result name="invalid.token">/token/invalid.jsp</result>
</action>
</package>
</struts>
|
|
1
|
<include file="com/rk/strut/h_token/struts-token.xml"></include>
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>添加学生</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/rk/token/add" method="post">
用户名:<input type="text" name="username"/><br/>
密码:<input type="password" name="password"/><br/>
<input type="submit" value="提交"/>
<s:token></s:token>
</form>
</body>
</html>
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>token无效</title>
</head>
<body>
<s:actionerror/>
</body>
</html>
|
|
1
|
<interceptor name="token" class="org.apache.struts2.interceptor.TokenInterceptor"/>
|
|
1
2
3
4
5
|
<action name="someAction" class="com.examples.SomeAction">
<interceptor-ref name="token"/>
<interceptor-ref name="basicStack"/>
<result name="success">good_result.ftl</result>
</action>
|
|
1
2
3
4
5
6
7
8
9
|
<-- In this case, myMethod of the action class will not
get checked for invalidity of token -->
<action name="someAction" class="com.examples.SomeAction">
<interceptor-ref name="token">
<param name="excludeMethods">myMethod</param>
</interceptor-ref name="token"/>
<interceptor-ref name="basicStack"/>
<result name="success">good_result.ftl</result>
</action>
|
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29917475/viewspace-2121024/ ,如需转载,请注明出处,否则将追究法律责任。