天天看點

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

本文為原創,如需轉載,請注明作者和出處,謝謝!

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

<html action="

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

"  method="post">

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

<input type="submit" value="儲存" />

<input type="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)

<%@ 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>

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

【第2步】實作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對象

    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;

}

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

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的代碼如下:

<?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">

        </action>    

    </package>    

</struts>

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

<%@ page pageEncoding="GBK"%>

    <title>送出結果</title>

    <h1>${result}</h1>

在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