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>
相关推荐
比格杰森 2020-01-30
niepxiya 2019-09-05
冬冬阳光 2010-07-03
xasdfg 2016-04-19
dcbeyond 2019-06-27
如果我们把input的type设置为submit,是的,提交表单经常需要submit按钮,就像下面这样:。<inputtype="submit"class="button"value="登录"
陈浩 2014-10-08
小小大人物 2014-09-28
uvyoaa 2014-05-19
liuxiaohua 2019-06-25
andyhu00 2013-08-23
努力的zhiyi 2013-07-23
ajaxhe 2013-03-23
jibkfv 2019-06-21
mofgeng 2008-04-26
lpch0 2012-05-29
lovehuayud 2012-05-23