老鐘

          并不是每一次雨后,都會有彩虹
          posts - 14, comments - 4, trackbacks - 0, articles - 1

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

          Posted on 2012-02-20 22:20 LaozhonG 閱讀(313) 評論(1)  編輯  收藏 所屬分類: 技術
          在很多Web應用中,為了完成不同的工作,一個HTML form標簽中可能有兩個或多個submit按鈕,如下面的代碼所示:

          1 <form action=""  method="post">
          2  .............................................
          3 <input type="submit" value="保存" />
          4 <input type="submit" value="打印" />
          5 </form>
          由于在<form>中的多個提交按鈕都向一個action提交,使用Struts2 Actionexecute方法就無法判斷用戶點擊了哪一個提交按鈕。如果大家使用過Struts1.x就會知道在Struts1.2.9之前的版本需要使用一個LookupDispatchAction動作來處理含有多個submitform。但使用LookupDispatchAction動作需要訪問屬性文件,還需要映射,比較麻煩。從Struts1.2.9開始,加入了一個EventDispatchAction動作。這個類可以通過java反射來調用通過request參數指定的動作(實際上只是判斷某個請求參數是不存在,如果存在,就調用在action類中和這個參數同名的方法)。使用EventDispatchAction必須將submitname屬性指定不同的值以區分每個submit。而在Struts2中將更容易實現這個功能。 
             
          當然,我們也可以模擬EventDispatchAction的方法通過request獲得和處理參數信息。但這
          樣比較麻煩。在Struts2中提供了另外一種方法,使得無需要配置可以在同一個action類中執
          行不同的方法(默認執行的是execute方法)。使用這種方式也需要通過請求參來來指定要執
          行的動作。請求參數名的格式為
          action!method.action
          注:由于Struts2只需要參數名,因此,參數值是什么都可以。
          下面我就給出一個實例程序來演示如何處理有多個submit的form:
          【第1步】實現主頁面(more_submit.jsp)

           1 <%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
           2 <%@ taglib prefix="s" uri="/struts-tags" %>
           3 <html>
           4 <head>
           5 <title>My JSP 'hello.jsp' starting page</title>
           6 </head>
           7 <body>
           8 <s:form action="submit.action" >
           9 <s:textfield name="msg" label="輸入內容"/>
          10 <s:submit name="save" value="保存" align="left" method="save"/>
          11 <s:submit name="print" value="打印" align="left" method="print" />
          12 </s:form>
          13 </body>
          14 </html>
          在more_submit.jsp中有兩個submit:保存和打印。其中分別通過method屬性指定了要調用
          的方法:save和print。因此,在Action類中必須要有save和print方法。
          【第2步】實現Action類(MoreSubmitAction)
           1 package action;
           2 
           3 import javax.servlet.http.*;
           4 import com.opensymphony.xwork2.ActionSupport;
           5 import org.apache.struts2.interceptor.*;
           6 public class MoreSubmitAction extends ActionSupport implements ServletRequestAware
           7 {
           8 private String msg;
           9 private javax.servlet.http.HttpServletRequest request;
          10 // 獲得HttpServletRequest對象
          11 public void setServletRequest(HttpServletRequest request)
          12 {
          13 this.request = request;
          14 }
          15 // 處理save submit按鈕的動作
          16 public String save() throws Exception
          17 {
          18 request.setAttribute("result", "成功保存[" + msg + "]");
          19 return "save";
          20 }
          21 // 處理print submit按鈕的動作
          22 public String print() throws Exception
          23 {
          24 request.setAttribute("result", "成功打印[" + msg + "]");
          25 return "print";
          26 }
          27 public String getMsg()
          28 {
          29 return msg;
          30 }
          31 public void setMsg(String msg)
          32 {
          33 this.msg = msg;
          34 }
          35 }
          上面的代碼需要注意如下兩點:
          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的代碼如下:
           1 <?xml version="1.0" encoding="UTF-8" ?>
           2 <!DOCTYPE struts PUBLIC
           3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
           4 "http://struts.apache.org/dtds/struts-2.0.dtd">
           5 <struts>
           6 <package name="demo" extends="struts-default" >
           7 <action name="submit" class="action.MoreSubmitAction">
           8 <result name="save" >
           9 /result.jsp
          10 </result>
          11 <result name="print">
          12 /result.jsp
          13 </result>
          14 </action>
          15 </package>
          16 </struts>
          【第4步】編寫結果頁(result.jsp)
          1 <%@ page pageEncoding="GBK"%>
          2 <html>
          3 <head>
          4 <title>提交結果</title>
          5 </head>
          6 <body>
          7 <h1>${result}</h1>
          8 </body>
          9 </html>  
           
          在result.jsp中將在save和print方法中寫到request屬性中的執行結果信息取出來,并輸出到
          客戶端。
          啟動Tomcat后,在IE中執行如下的URL來測試程序:
          http://localhost:8080/moresubmit/more_submit.jsp
          大家也可以直接使用如下的URL來調用save和print方法:
          調用save方法:http://localhost:8080/moresubmit/submit!save.action
          調用print方法:http://localhost:8080/moresubmit/submit!print.action

          以前為了寫這個想了很長時間,才知道原來name這個屬性作用還真的蠻大。
          其實再非struts標簽中,即純粹的html標簽中也可以使用,寫法為:
          <input type="submit" name="method:save" value="save"/>
          <input type="submit" name="method:print" value="print"/>
          上文出自nokiaguy的個人博客,如果想查看原文請點擊《 Struts2教程2:處理一個form多個submit


          ------------------------------------------------------
          About Me's
                          ------一個喜歡在一塊青石階上獨立行走的人

          Feedback

          # re: Struts2教程2:處理一個form多個submit  回復  更多評論   

          2012-02-21 00:03 by LaozhonG
          其實struts1中還有多種方法,可以達到這個效果,但是都要比這個復雜!可以參考nokiaguy的,EventDispatchAction,LookupDispatchAction,這兩個是通過配置不同的訪問路徑來實現的。如同MappingDispatchAction類調用不同的Action方法,DispatchAction調用不同的Action方法般。
          主站蜘蛛池模板: 论坛| 城步| 衡阳县| 巨鹿县| 闽侯县| 乐清市| 武乡县| 健康| 万安县| 长子县| 炉霍县| 福海县| 瑞昌市| 平谷区| 浦江县| 北碚区| 娄烦县| 福海县| 海原县| 陇南市| 普兰县| 张北县| 车险| 石渠县| 桐乡市| 沙湾县| 福贡县| 阳东县| 肥城市| 二连浩特市| 五常市| 忻州市| 上栗县| 塔河县| 微博| 西青区| 明水县| 杭州市| 洱源县| 河曲县| 南昌县|