struts2与struts很大的不同点在于,struts的execute方法提供了HttpServletRequest和HttpServletResponse方法在获取客户端提交的数据信息的时候需要使用HttpServletRequest的getParameter()方法,并且还需要进行必要的数据类型转换。如何客户端提交的数据量大的时候,我们则需要写很多的getParameter方法。这样代码量就相应的增加不少。但是struts2为我们提供了属性驱动和模型驱动,它不需要我们写很多的获取值的方法。而只需要我们在Action中定义相应的getter方法,在界面上以Action中的变量名作为表单元素的name属性值即可。
package org.viancent.action; import java.util.Date; import com.opensymphony.xwork2.ActionSupport; public class GetFieldAction extends ActionSupport { private String pname; private String address; private int age; private Date date; public String getPname() { return pname; } public void setPname(String pname) { this.pname = pname; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } @Override public String execute() throws Exception { return SUCCESS; } }
<div> <form action="/struts2/register.action" method="post"> <table width="80%" align="center"> <tr> <td>UserName:</td> <td><input type="text" name="pname" id="pname"/> </td> </tr> <tr> <td>PassWord:</td> <td><input type="text" name="address" id="address"/></td> </tr> <tr> <td>Age:</td> <td><input type="text" name="age" id="age"/></td> </tr> <tr> <td>Date</td> <td><input type="text" name="date" id="date"/></td> </tr> <tr> <td colspan="2"><input type="submit" value="submit"/></td> </tr> </table> </form> </div> //其他配置信息,相同
package org.viancent.domain; import java.util.Date; public class Person { private String pname; private int page; private String address; private Date date; public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } public String getPname() { return pname; } public void setPname(String pname) { this.pname = pname; } public int getPage() { return page; } public void setPage(int page) { this.page = page; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }Action:(RegisterAction)
package org.viancent.action; import org.viancent.domain.Person; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; public class RegisterAction extends ActionSupport implements ModelDriven<Person/*这里需要填写Person对象*/> { private Person person = new Person(); //定义Person对象用于承载getModel()方法返回的数据信息 //同时这里必须实例化对象,否则person的对象会为空 public Person getPerson() { //实现person的getter方法,用于当请求转发后,其他界面数据访问 return person; } @Override public Person getModel() { //重写ModleDriven的方法,返回定义的person对象 return person; } @Override public String execute() throws Exception { //实现跳转 System.out.println(person.getPage()+""+person.getDate()); return SUCCESS; } }struts.xml:
<action name="register"class="org.viancent.action.RegisterAction"> <result name="success">/result.jsp</result> <result name="input">/show.jsp</result> <interceptor-ref name="modelDriven"></interceptor-ref> //实现模型驱动的拦截器 <interceptor-ref name="defaultStack"></interceptor-ref> </action>JSP(register.jsp)
原文:http://blog.csdn.net/zhanglianhai555/article/details/44352913