教程1中,一个form表单只能完成一个submit工作,在实际工作中很有可能出现一个form多个submit的情况,例如下面这个代码:

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

下面演示如何处理多个submit的form:

主页面more_submit.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>More-Sumbit</title>
</head>
<body>
    <s:form action="submit" namespace="/mystruts">
        <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,value为保存和打印,并分别通过method属性指定了要调用的方法分别是save和print,因此在Action类中必须有这两个方法的实现。

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对象
    @Override
    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";
    }

    // msg的get和set方法
    public String getMsg() {
        return msg;
    }

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

`(1)save和print方法必须存在,否则会抛出java.lang.NoSuchMethodException异常

(2)只使用Struts2 Action动作的execute方法无法访问request对象,因此,Struts2 Action类需要实现一个Struts2自带的拦截器ServletRequestAware来获得request对象。`

配置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 ="struts2" namespace="/mystruts" extends= "struts-default">
             <action name ="submit" class="action.MoreSubmitAction" >
                   <result name ="save">/save.jsp</ result>         
                   <result name ="print">/print.jsp</ result>
             </action >
      </package >
</struts>

结果页面

只为演示,这里save.jsp和print.jsp使用一样的代码

<%@ page language ="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
<html>
<head>
<meta http-equiv= "Content-Type" content ="text/html; charset=UTF-8">
<title> 一个form多个submit的结果页面 </title >
</head>
<body>
      ${result }
</body>
</html>

save.jsp中${result }将save方法中request属性的值取出来,并输出到客户端。
同样的,print.jsp中${result }将print方法中request属性的值取出来,并输出到客户端。

启动Tomcat,访问http://localhost:8080/Struts2Demo/more_submit.jsp

当然也可以直接使用如下URL调用save和print方法
调用save方法:http://localhost:8080/Struts2Demo/mystruts2/submit!save.action
调用print方法:http://localhost:8080/Struts2Demo/mystruts2/submit!print.action

标签: Java, Struts2

添加新评论