struts練習-表單提交,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,將錯誤資訊以中文形式提示客戶!
打包下載,希望對您有用!
struts2 表單提交
<form action="login.action" method="post">
姓名:<input type="text" name="username"><br>
密碼:<input type="text" name="password"><br>
<input type="submit" value="點擊登入">
</form>
表單寫錯了 是form不是from 點擊按鈕沒反應 不是submit寫錯 就是 form寫錯了 再就死瀏覽器的關係 後者可能性不大
struts2中表單提交問題
你點擊匯出後js修改了form的action,如果頁面沒有重新整理的話,form的action是不會自動重設為sendSingleQuery.action的。
給你說個簡單的方法,就是在doReport()方法的最後加上一行js代碼:
form.action="sendSingleQuery.action";
在匯出後重設為預設的action,代碼修改量最小,請採納。