Struct2 获取参数的几种方式小结
#1.获取静态参数:
<action name="demo" class="actions.Demo" method="add">
<param name="name">gsc</param>
<param name="age">18</param>
</action>
package actions;
//1.静态获取参数
public class Demo {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String add() {
System.out.println("age:"+getAge()+"name"+getName());
return null;
}
}
2,动态获取参数
#1,第一种数据模型和动作类在一起
package actions;
import com.opensymphony.xwork2.ActionSupport;
//1.动态获取参数,数据模型和动作类在一起注意提交的参数必须一致
public class Demo1 extends ActionSupport{
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String AddUser() {
System.out.println("age:"+getAge()+"name:"+getName());
return null;
}
}
##2,数据模型和动作类分离:
package actions;
import com.opensymphony.xwork2.ActionSupport;
import bean.User;
//2.动态获取参数,数据模型和动作类分离注意 提交参数带对象.属性如
//user.name 这种
public class Demo2 extends ActionSupport{
private User user=new User();
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String AddUser() {
System.out.println("age:"+user.getAge()+"name:"+user.getName());
return null;
}
}
##3,第三种数据模型和动作类分离*(推荐的一种)
package actions;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import bean.User;
//3.动态获取参数:模型和数据驱动 条件数据模型和动作类分开
//直接提交参数
public class Demo4 extends ActionSupport implements ModelDriven<User>{
/**
*
*/
private User user=new User();//必须自己实例化数据模型
private static final long serialVersionUID = 1L;
@Override
public User getModel() {
// TODO Auto-generated method stub
return user;
}
public String addUser() {
System.out.println("u "+user.toString());
return null;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}