<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>使用s:token防止重复提交</title>
</head>
<body>
<h3>使用s:token防止重复提交</h3>
<s:form action="pro">
<!-- 普通表单域 -->
<s:textfield name="book" key="book"/>
<!-- 用于防刷新的token -->
<s:token/>
<s:submit value="提交"/>
</s:form>
</body>
</html>
package cn.hnpi;
import com.opensymphony.xwork2.ActionSupport;
public class ProAction extends ActionSupport {
private String book;
public String getBook() {
return book;
}
public void setBook(String book) {
this.book = book;
}
}
在struts.Xml中的配置文件
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<constant name="struts.custom.i18n.resources" value="mess"/>
<constant name="struts.i18n.encoding" value="GBK"/>
<package name="lee" extends="struts-default">
<!-- 定义名为pro的Action,其实现类为lee.ProAction -->
<action name="pro" class="cn.hnpi.ProAction">
<!-- 使用系统默认的拦截器栈 -->
<interceptor-ref name="defaultStack"/>
<!-- 使用防刷新的token拦截器 -->
<interceptor-ref name="token"/>
<!-- 定义重复提交转向的视图,该逻辑视图名必须是invalid.token -->
<result name="invalid.token">/refresh.jsp</result>
<!-- 如果处理结果返回success,对应/show.jsp视图资源 -->
<result name="success">/show.jsp</result>
</action>
<action name="">
<result>.</result>
</action>
</package>
</struts>
<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>防刷新测试</title>
</head>
<body>
用户正常提交,提交的书名为:${requestScope.book}
</body>
</html>
<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>防刷新测试</title>
</head>
<body>
您的请求已被处理!请不要刷新页面
</body>
</html>
如果表单页面没有使用<s:token/>标签,则千万不要使用token拦截器,否则会导致无法提交表单
419

被折叠的 条评论
为什么被折叠?



