struts2: Form表单有多个submit的提交有关问题
页面中传的是Buser对象,BuserBean的属性为:
private String URealname;
private String USex;
private String UTel;
private String UQq;
添加get、set方法。
1.register.jsp
<!--
s:submit中的method属性和struts.xml中action标签中的method属性一致(method属性指定了要调用的方法)。
在s:submit中可以为一个action设置多个method,而在struts.xml中action标签中只能设置一个method。
当然,在struts.xml中可以为一个action指定多个actionName(action别名)。
总结:一个Action类,可以对应多个action别名,每个action别名对应多个method属性(通过s:submit标签设置)。
-->
4.view.jsp <s:form action="userAction">
<table>
<tr>
<td>用户注册信息</td>
</tr>
<tr>
<td>真实姓名</td>
<td><input name="buser.URealname" type="text" /></td>
</tr>
<tr>
<td>性别</td>
<td><input type="radio" name="buser.USex" value="男"checked="checked"/>男
<input type="radio" name="buser.USex" value="女" class="radio" />
女</td>
</tr>
<tr>
<td>手机号码</td>
<td><input name="buser.UTel" type="text"/></td>
</tr>
<tr>
<td>QQ号</td>
<td><input name="buser.UQq" type="text"/></td>
</tr>
</table>
<s:submit value="提交" method="ensure" />
<s:submit value="预览" method="view" />
</s:form>
2.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>
<!-- 加载struts2框架默认的配置文件 -->
<include file="struts-default.xml" />
<!-- extends表示要加载struts2框架默认的package -->
<package name="struts2" extends="struts-default">
<!-- action与action之间的跳转 -->
<action name="userAction" class="com.dgy.action.UserAction">
<result name="ensure" >test.jsp</result>
<result name="view" >view.jsp</result>
<result name="input">register.jsp</result>
</action>
</package>
</struts>
3.com.dgy.action.UserActionpackage com.dgy.action;
import com.dgy.javabean.BuserBean;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport{
BuserBean buser ;
public String ensure(){
System.out.println("running ensure()!");
return "ensure";
}
public String view(){
System.out.println("running view()!");
this.setBuser(this.getBuser());
return "view";
}
public BuserBean getBuser() {
return buser;
}
public void setBuser(BuserBean buser) {
this.buser = buser;
}
}
<s:iterator value="buser"> <table> <tr> <td>用户注册信息</td> </tr> <tr> <td>真实姓名</td> <td ><s:property value="URealname"></s:property></td> </tr> <tr> <td>性别</td> <td ><s:property value="USex"></s:property></td> </tr> <tr> <td>手机号码</td> <td ><s:property value="UTel"></s:property></td> </tr> <tr> <td>QQ号</td> <td><s:property value="UQq"></s:property></td> </tr> </table> </s:iterator> <s:submit value="提交" method="ensure" /> </s:form>