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>
<!-- 默认的视图主题 -->
<constant name="struts.ui.theme" value="simple" />
<!-- struts2在防止表单重复提交的拦截中有2个,分别是:token,tokensession。tokensession继承token而来。
通常情况下,使用tokensession客户端感觉会比较友好。 -->
<!-- 如果重复提交,会跳转到error.jsp教程页面 -->
<package name="person" namespace="/test" extends="struts-default">
<action name="token" class="com.ljq.action.personaction">
<interceptor-ref name="defaultstack" />
<interceptor-ref name="token" />
<!-- 如果重复提交,跳转到error.jsp页面 -->
<result name="invalid.token">/web-inf/page/error.jsp</result>
<result>/web-inf/page/message.jsp</result>
</action>
<action name="tokensession" class="com.ljq.action.personaction">
<interceptor-ref name="defaultstack" />
<interceptor-ref name="tokensession" />
<!-- 如果重复提交,不会跳转到error.jsp页面 -->
<result name="invalid.token">/web-inf/page/error.jsp</result>
<result>/web-inf/page/message.jsp</result>
</action>
</package>
</struts>
personaction类
package com.ljq.action;
import java.util.arraylist;
import java.util.list;
public class personaction {
private string name;
@suppresswarnings("unchecked")
//观看控制台
//如果token生效则不会在控制台输出name的值,而会输出如下警告: 2011-3-14 20:45:32 com.opensymphony.xwork2.util.logging.commons.commonslogger
//warn 警告: form token edz4s96rndn5vd8b1cqtk6fthijupc66 does not match the session token null.
public string execute() {
list ls = new arraylist();
ls.add(name);
for (int i = 0; i < ls.size(); i++) {
system.out.println(ls.get(i));
}
return "success";
}
public string getname() {
return name;
}
public void setname(string name) {
this.name = name;
}
}
index.jsp表单页面
<%@ page language="java" import="java.util.*" pageencoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
<head>
<title>防止表单重复提交</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
<!-- 防止表单重复提交,记得在form表单里填上<s:token></s:token> -->
<!-- action="token"、action="tokensession" -->
<s:form action="token" namespace="/test" method="post">
姓名:<s:textfield name="name"/><s:token></s:token>
<input type="submit" value="发送"/>
</s:form>
</body>
</html>
message.jsp返回成功页面
<%@ page language="java" import="java.util.*" pageencoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
<head>
<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">
</head>
<body>
<s:property value="name"/><br/>
<%=new date() %>
</body>
</html>
error.jsp表单重复提交提示页面
<%@ page language="java" import="java.util.*" pageencoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
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 '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">
<!--
<link rel="stylesheet" type="text/css教程" href="styles.css">
-->
</head>
<body>
您已经提交了表单,请不要重复提交。
</body>
</html>
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索string
配置
struts2表单重复提交、struts表单重复提交、struts防止重复提交、struts2 防止重复提交、防止表单重复提交,以便于您获取更多的相关知识。