Struts2防止表單重複提交(原始碼)
struts配置檔案:struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- 自定義攔截器,一般用於全域性 -->
<package name="default" namespace="/" extends="struts-default">
<interceptors>
<interceptor-stack name="myInterceptor">
<interceptor-ref name="defaultStack" />
<interceptor-ref name="tokenSession" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="myInterceptor"></default-interceptor-ref>
</package>
<!-- 測試 -->
<package name="user" namespace="/" extends="default">
<action name="userLogin" class="com.lun.action.UserAction" method="login">
<result name="success">/login_success.jsp</result>
<result name="invalid.token">/error.jsp</result>
</action>
</package>
</struts>
後臺action處理類:UserAction.java
package com.lun.action;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.opensymphony.xwork2.ActionSupport;
/**
* @Desc:使用struts的token,解決表單重複提交的問題
* @Author:張輪
* @Date:2014-2-24下午03:44:01
*/
@SuppressWarnings("serial")
public class UserAction extends ActionSupport{
private String userName;
private String password;
public String login(){
try {
System.out.println("---開始---"+new SimpleDateFormat("yyyy-MM-dd hh:mm:ss:SSS").format(new Date()));
//延遲3秒鐘,留足夠的時間,在前臺多點幾次提交按鈕,看控制檯輸出效果
Thread.sleep(3000);
System.out.println("userName:"+userName+" , password:"+password);
System.out.println("---結束---"+new SimpleDateFormat("yyyy-MM-dd hh:mm:ss:SSS").format(new Date()));
} catch (InterruptedException e) {
e.printStackTrace();
}
return SUCCESS;
}
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;
}
}
jsp前臺帶表單頁面:index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<form action="userLogin.action" method="post">
<!-- 必須在form里加入下邊這行,struts會自動生成一個唯一標識的令牌值,儲存session,在後臺進行匹配 -->
<s:token></s:token>
使用者名稱:<input name="userName" /><br/>
密 碼:<input name="password" /><br/>
<input type="submit" value="登入" style="width: 100px;height: 60px;"/>
</form>
</body>
</html>
login_success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'login_success.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<h1>登入成功!</h1>
<h2>使用者名稱:${userName}</h2>
<h2>密碼:${password}</h2>
</body>
</html>
error.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'error.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<h1>你已經提交過一次了!</h1>
</body>
</html>
web.xml
<?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">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 配置struts2 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>