struts练习-表单提交

来源:互联网 发布:阿里云 余额不足通知 编辑:程序博客网 时间:2024/05/16 02:23

防止表单重复提交练习:

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

我一般都是将以下jar包一次性导入(大笑,可能一个知识点的练习不需要那么多)

以上jar下载

web.xml中需要添加过滤器:

配置如下内容:

完成以上配置之后,在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{@Overridepublic 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,将错误信息以中文形式提示客户!



打包下载,希望对您有用!

0 0
原创粉丝点击