日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

struts练习-表单提交

發布時間:2025/3/16 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 struts练习-表单提交 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

防止表單重復提交練習:

做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,將錯誤信息以中文形式提示客戶!



打包下載,希望對您有用!

總結

以上是生活随笔為你收集整理的struts练习-表单提交的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。