Struts2防止表单重复提交(源代码)

本文介绍如何使用Struts2的token拦截器防止表单重复提交,并通过具体示例展示了配置方法及前后端实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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/>
    密&nbsp;&nbsp;&nbsp;码:<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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值