strut2_令牌防止重复提交

index.jsp:

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%@ taglib prefix="s" uri="/struts-tags"%>
 3 <%
 4 String path = request.getContextPath();
 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 6 %>
 7 
 8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 9 <html>
10   <head>
11     <base href="<%=basePath%>">
12     
13     <title>My JSP 'index.jsp' starting page</title>
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">    
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="styles.css">
21     -->
22   </head>
23   
24   <body>
25        <s:form action="token.action" theme="simple" >
26            用户名:<s:textfield name="username" size="20"/><br>
27            密码:<s:textfield name="password" size="20"/><br>
28            
29            <s:token></s:token>
30            
31            <s:submit value="提交"/>
32        </s:form>
33   </body>
34 </html>

 welcome.jsp:

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@ taglib prefix="s" uri="/struts-tags"%>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6     <head>
 7         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8         <title>欢迎光临</title>
 9     </head>
10     <body>
11         <s:property value="username"/>,欢迎光临<br/>
12     </body>
13 </html>

alert.jsp:

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5     <head>
 6         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7         <title>Insert title here</title>
 8     </head>
 9     <body>
10         不能重复提交表单
11     </body>
12 </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.devMode" value="true"></constant>
 8     <package name="struts" extends="struts-default">
 9         <action name="token" class="com.sunflower.action.Action">
10             <result name="success">/welcome.jsp</result>
11             <result name="invalid.token">/alert.jsp</result>
12 
13             <interceptor-ref name="token"></interceptor-ref>
14             <interceptor-ref name="defaultStack"></interceptor-ref>
15         </action>
16     </package>
17 </struts>    

关键在struts.xml和表单中设置:

原文地址:https://www.cnblogs.com/hanyuan/p/2537060.html