欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

struts2中一个表单中提交多个请求的例子(多个提交按钮)

程序员文章站 2022-06-29 13:22:59
使用这种方式也需要通过请求参来来指定要执行的动作。请求参数名的格式为  action!method.action  注:由于struts2只需要参数名,因此,参数值是什么都...

使用这种方式也需要通过请求参来来指定要执行的动作。请求参数名的格式为
  action!method.action
  注:由于struts2只需要参数名,因此,参数值是什么都可以。

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

主页面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>

moresubmitaction.java

复制代码 代码如下:

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

structs.xml

复制代码 代码如下:

<?xml version="1.0" encoding="utf-8" ?>
<!doctype struts public "-//apache software foundation//dtd struts configuration 2.1//en" "http://struts.apache.org/dtds/struts-2.1.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>

result.jsp

复制代码 代码如下:

<%@ page language="java" contenttype="text/html; charset=gbk"
    pageencoding="gbk"%>
<!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=iso-8859-1">
<title>提交结果</title>
</head>
<body>
<h1>${result }</h1>
</body>
</html>