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

上一篇: Struts2教程1:第一個Struts2程序

   在不少 Web 應用中,爲了完成不一樣的工做,一個 HTML form 標籤中可能有兩個或多個 submit 按鈕,以下面的代碼所示:

< html  action =""   method ="post" >
 
< input  type ="submit"  value ="保存"   />
< input  type ="submit"  value ="打印"   />
</ html >

因爲在<form>中的多個提交按鈕都向一個action提交,使用Struts2 Actionexecute方法就沒法判斷用戶點擊了哪個提交按鈕。若是你們使用過Struts1.x就會知道在Struts1.2.9以前的版本須要使用一個LookupDispatchAction動做來處理含有多個submitform。但使用LookupDispatchAction動做須要訪問屬性文件,還須要映射,比較麻煩。從Struts1.2.9開始,加入了一個EventDispatchAction動做。這個類能夠經過java反射來調用經過request參數指定的動做(實際上只是判斷某個請求參數是不存在,若是存在,就調用在action類中和這個參數同名的方法)。使用EventDispatchAction必須將submitname屬性指定不一樣的值以區分每一個submit。而在Struts2中將更容易實現這個功能。 php

固然,咱們也能夠模擬EventDispatchAction的方法經過request得到和處理參數信息。但這樣比較麻煩。在Struts2中提供了另一種方法,使得無須要配置能夠在同一個action類中執行不一樣的方法(默認執行的是execute方法)。使用這種方式也須要經過請求參來來指定要執行的動做。請求參數名的格式爲 html

action!method.action java

注:因爲Struts2只須要參數名,所以,參數值是什麼均可以。 apache

下面我就給出一個實例程序來演示如何處理有多個submitform jsp

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


<% @ page language = " java "  import = " java.util.* "  pageEncoding = " GBK " %>
<% @ taglib prefix = " s "  uri = " /struts-tags "   %>
< html >
   < head >
     < title >My JSP 'hello.jsp' starting page </ title >
   </ head >
  
   < body >
     < s:form  action ="submit.action"   >
         < s:textfield  name ="msg"  label ="輸入內容" />  
         < s:submit  name ="save"  value ="保存"  align ="left"  method ="save" />
         < s:submit  name ="print"  value ="打印"  align ="left"  method ="print"   />      
     </ s:form >
   </ body >
</ html >

more_submit.jsp中有兩個submit:保存和打印。其中分別經過method屬性指定了要調用的方法:saveprint。所以,在Action類中必需要有saveprint方法。 測試

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


package action;

import javax.servlet.http.*;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.interceptor.*;

public  class MoreSubmitAction  extends ActionSupport  implements ServletRequestAware
{
     private String msg;
     private javax.servlet.http.HttpServletRequest request;
     //  得到HttpServletRequest對象
     public  void setServletRequest(HttpServletRequest request)
    {
         this.request = request;
    }
     //  處理save submit按鈕的動做
     public String save()  throws Exception
    {
        request.setAttribute("result", "成功保存[" + msg + "]");
         return "save";
    }

     //  處理print submit按鈕的動做
     public String print()  throws Exception
    {
        request.setAttribute("result", "成功打印[" + msg + "]");
         return "print";
    }
     public String getMsg()
    {
         return msg;
    }

     public  void setMsg(String msg)
    {
         this.msg = msg;
    }
}

上面的代碼須要注意以下兩點: spa

saveprint方法必須存在,不然會拋出java.lang.NoSuchMethodException異常。 .net

Struts2 Action動做中的方法和Struts1.x Actionexecute不一樣,只使用Struts2 Action動做的execute方法沒法訪問request對象,所以,Struts2 Action類須要實現一個Struts2自帶的攔截器來得到request對象,攔截器以下:

org.apache.struts2.interceptor. ServletRequestAware

【第3步】配置Struts2 Action

struts.xml 的代碼以下:

<? xml version="1.0" encoding="UTF-8"  ?>
<! DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd"
>
< struts >    
     < package  name ="demo"  extends ="struts-default"   >
         < action  name ="submit"   class ="action.MoreSubmitAction" >
             < result  name ="save"   >
                /result.jsp
             </ result >
             < result  name ="print" >
                /result.jsp
             </ result >
         </ action >    
     </ package >    
</ struts >

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


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

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

啓動Tomcat後,在IE中執行以下的URL來測試程序:

    http://localhost:8080/moresubmit/more_submit.jsp

你們也能夠直接使用以下的URL來調用saveprint方法:

調用save方法:http://localhost:8080/moresubmit/submit!save.action

調用print方法:http://localhost:8080/moresubmit/submit!print.action

源代碼:http://www.itpub.net/attachment.php?aid=520773


下一篇:Struts2教程3:struts.xml經常使用配置解析

相關文章
相關標籤/搜索