form 多个submit php,Struts2 处理一个form多个submit

由于在中的多个提交按钮都向一个action提交,使用Struts2 Action的execute方法就无法判断用户点击了哪一个提交按钮。

请求参数名的格式为

action!method.action

【第1步】实现主页面(more_submit.jsp)

My JSP 'hello.jsp' starting page

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

【第2步】实现Action类(MoreSubmitAction)

packageaction;

importjavax.servlet.http.*;

importcom.opensymphony.xwork2.ActionSupport;

importorg.apache.struts2.interceptor.*;

publicclassMoreSubmitActionextendsActionSupportimplementsServletRequestAware

{

privateString msg;

privatejavax.servlet.http.HttpServletRequest request;

// 获得HttpServletRequest对象

publicvoidsetServletRequest(HttpServletRequest request)

{

this.request = request;

}

// 处理save submit按钮的动作

publicString save()throwsException

{

request.setAttribute("result","成功保存["+ msg +"]");

return"save";

}

// 处理print submit按钮的动作

publicString print()throwsException

{

request.setAttribute("result","成功打印["+ msg +"]");

return"print";

}

publicString getMsg()

{

returnmsg;

}

publicvoidsetMsg(String msg)

{

this.msg = msg;

}

}

【第3步】配置Struts2 Action,struts.xml如下:

xmlversion="1.0"encoding="UTF-8"?>

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">

/result.jsp

result>

/result.jsp

result>

action>

package>

struts>

【第4步】编写结果页(result.jsp)

提交结果title>

head>

${result}h1>

body>

html>

在result.jsp中将在save和print方法中写到request属性中的执行结果信息取出来,并输出到客户端。

启动Tomcat后,在IE中执行如下的URL来测试程序:

大家也可以直接使用如下的URL来调用save和print方法:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值