防止表单反复提交练习:

做struts练习之前,首先有一些准备工作要做,那就是建立一个webproject ,另外就是导入jar包和配置web.xml

我一般都是将下面jar包一次性导入( 。可能一个知识点的练习不须要那么多)

struts练习-表单提交_css

 ​

web.xml中须要加入过滤器:

配置例如以下内容:

struts练习-表单提交_html_02

完毕以上配置之后,在src下建立struts.xml(当然临时能够不用)!

以下就能够进行你要做的工作了!

以下的样例是我的防止表单反复提交的练习:

1、发送请求的页面:

<%@ 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>请求界面</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>

<s:form action="token" namespace="/" methos="post" theme="simple">
<!-- 通过s:token生成隐藏域(令牌号) -->
<s:token />
<input type="submit" value="提交" />
</s:form>

</body>
</html>


2、提交成功页面:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>

<body>
<h2>表单提交成功</h2>
</body>
</html>


3、反复提交。提示错误页面:

<%@ 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 'token_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>
<h2>表单已提交,请不要反复提交!</h2>

<!-- 表单反复提交有一个默认的错误信息 -->
<!-- 打印出该默认信息 -->
<s:actionerror />
</body>
</html>


4、Action代码:

package cn.itcast.action;

import com.opensymphony.xwork2.ActionSupport;


public class TokenAction extends ActionSupport{

@Override
public String execute() throws Exception {
System.out.println("用户注冊...");
return SUCCESS;
}
}


5、struts配置信息:

<?xml version="1.0" encoding="UTF-8"?
>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<!-- 配置struts -->
<struts>
<!-- 常量配置 -->
<!-- 配置web应用的默认编码集 -->
<constant name="struts.i18n.encoding" value="UTF-8"></constant>

<!-- 设置value为true时。当struts文件改变后,系统会自己主动又一次载入该文件 -->
<constant name="struts.configuration.xml.reload" value="true"></constant>

<!-- 应用struts2的开发模式。value为true时。能够打印更具体的错误信息 -->
<constant name="struts.devMode" value="true"></constant>

<!-- 指定struts所须要的国际化资源文件 -->
<constant name="struts.custom.i18n.resources" value="tokenerror"></constant>

<!-- 包的配置 -->
<!-- 包名为default,继承struts-default -->
<!--
struts2框架使用包来管理Action和拦截器。
每一个包就是多个Action、多个拦截器、多个拦截器引用的集合。
使用package能够将逻辑相关的一组Action、Result、Interceptor等组件分为一组。package有点
像对象,能够继承其它的package,也能够被其它package继承,甚至能够定义抽象的package
-->
<package name="default" extends="struts-default">

<!-- 加入action:表单反复提交 -->
<action name="token" class="cn.itcast.action.TokenAction">
<!-- 配置结果页面。省略了name="success" -->
<result>/index.jsp</result>
<result name="invalid.token">/token_error.jsp</result>

<!-- 重定义拦截器 -->
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="token"></interceptor-ref>
</action>

</package>
</struts>


6、web.xml配置信息:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
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_3_0.xsd">
<display-name></display-name>

<!-- 设置过滤器 -->
<filter>
<!-- 过滤器的名称 -->
<filter-name>struts</filter-name>
<!-- 过滤器的实现类。负责详细的过滤事务 -->
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<!-- 设置过滤器的映射 -->
<filter-mapping>
<!-- 过滤器的名称 -->
<filter-name>struts</filter-name>
<!-- 过滤器负责过滤的URL -->
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- 设置该web网站欢迎文件列表 -->
<welcome-file-list>
<!-- 指定欢迎文件名 -->
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>


7、tokenerror.properties文件:

单击Add,将错误信息以中文形式提示客户!

struts练习-表单提交_struts_03