歡迎您光臨本站 註冊首頁

Struts2教程2:處理一個form多個submit

←手機掃碼閱讀     火星人 @ 2014-03-10 , reply:0

在很多Web應用中,為了完成不同的工作,一個HTML form標籤中可能有兩個或多個submit按鈕,如下面的代碼所示:

<!--[if !supportLineBreakNewLine]-->
<htmlaction="…" method="post">
……
<inputtype="submit"value="保存"/>
<inputtype="submit"value="列印"/>
</html>

由於在<form>中的多個提交按鈕都向一個action提交,使用Struts2 Action的execute方法就無法判斷用戶點擊了哪一個提交按鈕.如果大家使用過Struts1.x就會知道在Struts1.2.9之前的版本需要使用一個LookupDispatchAction動作來處理含有多個submit的form.但使用LookupDispatchAction動作需要訪問屬性文件,還需要映射,比較麻煩.從Struts1.2.9開始,加入了一個EventDispatchAction動作.這個類可以通過java反射來調用通過request參數指定的動作(實際上只是判斷某個請求參數是不存在,如果存在,就調用在action類中和這個參數同名的方法).使用EventDispatchAction必須將submit的name屬性指定不同的值以區分每個submit.而在Struts2中將更容易實現這個功能.

當然,我們也可以模擬EventDispatchAction的方法通過request獲得和處理參數信息.但這樣比較麻煩.在Struts2中提供了另外一種方法,是的無需要配置可以在同一個action類中執行不同的方法(默認執行的是execute方法).使用這種方式也需要通過請求參來來指定要執行的動作.請求參數名的格式為

action!method.action

註:由於Struts2隻需要參數名,因此,參數值是什麼都可以.

下面我就給出一個實常式序來演示如何處理有多個submit的form:

【第1步】實現主頁面(more_submit.jsp)

<%@pagelanguage="java"import="java.util.*"pageEncoding="GBK"%>
<%@taglibprefix="s"uri="/struts-tags"%>
<html>
 <head>
<title>MyJSP'hello.jsp'startingpage</title>
 </head>
 
 <body>
<s:formaction="submit.action">
<s:textfieldname="msg"label="輸入內容"/> 
<s:submitname="save"value="保存"align="left"method="save"/>
<s:submitname="print"value="列印"align="left"method="print"/> 
</s:form>
 </body>
</html>

在more_submit.jsp中有兩個submit:保存和列印.其中分別通過method屬性指定了要調用的方法:save和print.因此,在Action類中必須要有save和print方法.

【第2步】實現Action類(MoreSubmitAction)

packageaction;

importjavax.servlet.http.*;   importcom.opensymphony.xwork2.ActionSupport;   importorg.apache.struts2.interceptor.*;   publicclassMoreSubmitActionextendsActionSupportimplementsServletRequestAware   {   privateStringmsg;   privatejavax.servlet.http.HttpServletRequestrequest;   //獲得HttpServletRequest對象   publicvoidsetServletRequest(HttpServletRequestrequest)   {   this.request=request;   }   //處理savesubmit按鈕的動作   publicStringsave()throwsException   {   request.setAttribute("result","成功保存[" msg "]");   return"save";   }   //處理printsubmit按鈕的動作   publicStringprint()throwsException   {   request.setAttribute("result","成功列印[" msg "]");   return"print";   }   publicStringgetMsg()   {   returnmsg;   }   publicvoidsetMsg(Stringmsg)   {   this.msg=msg;   }   }   

上面的代碼需要注意如下兩點:

save和print方法必須存在,否則會拋出java.lang.NoSuchMethodException異常.

Struts2 Action動作中的方法和Struts1.x Action的execute不同,只使用Struts2 Action動作的execute方法無法訪問request對象,因此,Struts2 Action類需要實現一個Struts2自帶的攔截器來獲得request對象,攔截器如下:

org.apache.struts2.interceptor. ServletRequestAware

【第3步】配置Struts2 Action

struts.xml的代碼如下:

<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEstrutsPUBLIC
"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<packagename="demo"extends="struts-default">
<actionname="submit" class="action.MoreSubmitAction">
<resultname="save">
/result.jsp
</result>
<resultname="print">
/result.jsp
</result>
</action>
</package>
</struts>

【第4步】編寫結果頁(result.jsp)

<%@pagepageEncoding="GBK"%>
<html>
 <head>
<title>提交結果</title>
 </head>
 <body>
<h1>${result}</h1>
 </body>
</html>

在result.jsp中將在save和print方法中寫到request屬性中的執行結果信息取出來,並輸出到客戶端.


[火星人 ] Struts2教程2:處理一個form多個submit已經有1040次圍觀

http://coctec.com/docs/java/show-post-61861.html