Struts2教程:處理一個(gè)form多個(gè)submit
在很多Web應(yīng)用中,為了完成不同的工作,一個(gè)HTML form標(biāo)簽中可能有兩個(gè)或多個(gè)submit按鈕,如下面的代碼所示:
- < !--[if !supportLineBreakNewLine]-->
- < html action="" method="post">
- < input type="submit" value="保存" />
- < input type="submit" value="打印" />
- < /html>
由于在< form>中的多個(gè)提交按鈕都向一個(gè)action提交,使用Struts2 Action的execute方法就無法判斷用戶點(diǎn)擊了哪一個(gè)提交按鈕。如果大家使用過Struts1.x就會(huì)知道在Struts1.2.9之前的版本需要使用一個(gè)LookupDispatchAction動(dòng)作來處理含有多個(gè)submit的form。但使用LookupDispatchAction動(dòng)作需要訪問屬性文件,還需要映射,比較麻煩。從Struts1.2.9開始,加入了一個(gè)EventDispatchAction動(dòng)作。這個(gè)類可以通過java反射來調(diào)用通過request參數(shù)指定的動(dòng)作(實(shí)際上只是判斷某個(gè)請(qǐng)求參數(shù)是不存在,如果存在,就調(diào)用在action類中和這個(gè)參數(shù)同名的方法)。使用EventDispatchAction必須將submit的name屬性指定不同的值以區(qū)分每個(gè)submit。而在Struts2中將更容易實(shí)現(xiàn)這個(gè)功能。
當(dāng)然,我們也可以模擬EventDispatchAction的方法通過request獲得和處理參數(shù)信息。但這樣比較麻煩。在Struts2中提供了另外一種方法,使得無需要配置可以在同一個(gè)action類中執(zhí)行不同的方法(默認(rèn)執(zhí)行的是execute方法)。使用這種方式也需要通過請(qǐng)求參來來指定要執(zhí)行的動(dòng)作。請(qǐng)求參數(shù)名的格式為
action!method.action
注:由于Struts2只需要參數(shù)名,因此,參數(shù)值是什么都可以。
下面我就給出一個(gè)實(shí)例程序來演示如何處理有多個(gè)submit的form:
【第1步】實(shí)現(xiàn)主頁面(more_submit.jsp)
- < %@ 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="輸入內(nèi)容"/>
- < 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中有兩個(gè)submit:保存和打印。其中分別通過method屬性指定了要調(diào)用的方法:save和print。因此,在Action類中必須要有save和print方法。
【第2步】實(shí)現(xiàn)Action類(MoreSubmitAction)
- 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對(duì)象
- public void setServletRequest(HttpServletRequest request)
- {
- this.request = request;
- }
- // 處理save submit按鈕的動(dòng)作
- public String save() throws Exception
- {
- request.setAttribute("result", "成功保存[" + msg + "]");
- return "save";
- }
- // 處理print submit按鈕的動(dòng)作
- public String print() throws Exception
- {
- request.setAttribute("result", "成功打印[" + msg + "]");
- return "print";
- }
- public String getMsg()
- {
- return msg;
- }
- public void setMsg(String msg)
- {
- this.msg = msg;
- }
- }
上面的代碼需要注意如下兩點(diǎn):
save和print方法必須存在,否則會(huì)拋出java.lang.NoSuchMethodException異常。
Struts2 Action動(dòng)作中的方法和Struts1.x Action的execute不同,只使用Struts2 Action動(dòng)作的execute方法無法訪問request對(duì)象,因此,Struts2 Action類需要實(shí)現(xiàn)一個(gè)Struts2自帶的攔截器來獲得request對(duì)象,攔截器如下:
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步】編寫結(jié)果頁(result.jsp)
- < %@ page pageEncoding="GBK"%>
- < html>
- < head>
- < title>提交結(jié)果< /title>
- < /head>
- < body>
- < h1>${result}< /h1>
- < /body>
- < /html>
在result.jsp中將在save和print方法中寫到request屬性中的執(zhí)行結(jié)果信息取出來,并輸出到客戶端。
啟動(dòng)Tomcat后,在IE中執(zhí)行如下的URL來測試程序:
http://localhost:8080/moresubmit/more_submit.jsp
大家也可以直接使用如下的URL來調(diào)用save和print方法:
調(diào)用save方法:http://localhost:8080/moresubmit/submit!save.action
調(diào)用print方法:http://localhost:8080/moresubmit/submit!print.action
【編輯推薦】