标签:
Struts2三种传值方法:
Model方法传值(最简单)
DomainModel(域模型)方法传值(工程中最常用)
ModelDriven(模型驱动)传值(不推荐)
HTML表单
1 <form method="post" action="Student_reg!reg"> 2 <input type="text" name="username"><br> 3 <input type="text" name="password"><br> 4 <input type="submit"><input type="reset"> 5 </form>
Struts.xml配置
1 <struts> 2 <constant name="struts.devMode" value="true"></constant> 3 <package name="default" namespace="/" extends="struts-default"> 4 <action name="Student_*" class="cn.potato.action.StudentAction"> 5 <result name="{1}"> 6 /Student_{1}_success.jsp 7 </result> 8 </action> 9 </package> 10 </struts>
Model方法传值(最简单)
1 public class StudentAction{ 2 private String username; 3 private String password; 4 public String reg(){ 5 System.out.println(username); 6 System.out.println(password); 7 return "reg"; 8 } 9 public String getUsername() { 10 return username; 11 } 12 public void setUsername(String username) { 13 this.username = username; 14 } 15 public String getPassword() { 16 return password; 17 } 18 public void setPassword(String password) { 19 this.password = password; 20 } 21 }
DomainModel(域模型)方法传值(工程中最常用)
1 /**通过getUser方法将A对象传给Struts2,Struts2调用setter方法进行赋值。 2 *域模型 3 */ 4 public class A { 5 private String username; 6 private String password; 7 public String getUsername() { 8 return username; 9 } 10 public void setUsername(String username) { 11 this.username = username; 12 } 13 public String getPassword() { 14 return password; 15 } 16 public void setPassword(String password) { 17 this.password = password; 18 } 19 } 20 21 //Action类 22 public class StudentAction{ 23 private A user; 24 public String reg(){ 25 System.out.println(user.getUsername()); 26 System.out.println(user.getPassword()); 27 return "reg"; 28 } 29 public A getUser() { 30 return user; 31 } 32 public void setUser(A user) { 33 this.user = user; 34 } 35 }
ModelDriven(模型驱动)传值
1 /**Action类中自己new出A对象,然后利用getModel()把它传给Struts2,Struts2调用它的setter方法。 2 * Action类 3 */ 4 5 public class StudentAction extends ActionSupport implements ModelDriven<A>{ 6 private A user=new A(); 7 public String reg(){ 8 System.out.println(user.getUsername()); 9 System.out.println(user.getPassword()); 10 return "reg"; 11 } 12 public A getModel() { 13 return user; 14 } 15 }
标签:
原文地址:http://www.cnblogs.com/angleBlue/p/4835285.html