Struts2教程2:处理一个form多个submit

来源:互联网 发布:医库软件下载 编辑:程序博客网 时间:2024/04/30 23:49
在本系列教程中我们将学习到Struts2的各种技术。在本教程中使用的工具和程序库的版本如下:

开发工具:MyEclipse10

Web服务器:Tomcat6

Struts版本:Struts2.0.14

JDK版本:JDK1.7.0_03

J2EE版本:Java EE5.0

在很多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中将更容易实现这个功能。

当然,我们也可以模拟EventDispatchAction的方法通过request获得和处理参数信息。但这样比较麻烦。在Struts2中提供了另外一种方法,使得无需要配置可以在同一个action类中执行不同的方法(默认执行的是execute方法)。使用这种方式也需要通过请求参来来指定要执行的动作。请求参数名的格式为

action!method.action

注:由于Struts2只需要参数名,因此,参数值是什么都可以。

下面我就给出一个实例程序来演示如何处理有多个submitform

【第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></html>

more_submit.jsp中有两个submit:保存和打印。其中分别通过method属性指定了要调用的方法:saveprint。因此,在Action类中必须要有saveprint方法。

【第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;    }}

上面的代码需要注意如下两点:

saveprint方法必须存在,否则会抛出java.lang.NoSuchMethodException异常。

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>


【第5步】

配置Struts2 Action

web.xml的代码如下:

<?xml version="1.0" encoding="UTF-8"?>  <web-app version="2.5"       xmlns="http://java.sun.com/xml/ns/javaee"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee       http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">    <display-name></display-name>     <welcome-file-list>      <welcome-file>more_submit.jsp</welcome-file>    </welcome-file-list> <filter>    <filter-name>struts2</filter-name>    <filter-class>        org.apache.struts2.dispatcher.FilterDispatcher                </filter-class></filter><filter-mapping>    <filter-name>struts2</filter-name>    <url-pattern>/*</url-pattern></filter-mapping></web-app>


     还有应该注意的是要加载struts2的六个类库,分别为:commons-logging-1.0.4.jar , freemarker-2.3.8.jar , ognl-2.6.11.jar , struts2-core-2.0.14.jar , struts-core-1.3.5.jar , xwork-2.0.7.jar

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.blogjava.net/nokiaguy/archive/2008/04/16/193287.html