1、通过属性传参数(Attr)
UserAction.java
package com.bebig.struts2.user.action;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
private String name;
private int age;
public String add() {
System.out.println("name:"+name);
System.out.println("age:"+age);
return SUCCESS;
}
public int getAge() {
return age;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public void setName(String name) {
this.name = name;
}
}
调用方法:
<a href="user/userAdd.action?name=Don&age=26">添加用户</a>
2、通过域模型传参数(Domain Model)
UserAction.java
package com.bebig.struts2.user.action;
import com.bebig.struts2.user.dto.UserDTO;
import com.bebig.struts2.user.model.User;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
// 通过域模型传参数
private User user;
// 通过数据传输对象传参数
// private UserDTO userDTO;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String add() {
System.out.println("name:" + user.getName());
System.out.println("age:" + user.getAge());
// System.out.println("name:"+userDTO.getName());
// System.out.println("age:"+userDTO.getAge());
return SUCCESS;
}
// public void setUserDTO(UserDTO userDTO) {
// this.userDTO = userDTO;
// }
//
// public UserDTO getUserDTO() {
// return userDTO;
// }
}
User.java
package com.bebig.struts2.user.model;
public class User {
private String name;
private int age;
public int getAge() {
return age;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public void setName(String name) {
this.name = name;
}
}
UserDTO.java
package com.bebig.struts2.user.dto;
//数据传输对象
public class UserDTO {
private String name;
private String password;
private String comfirmPassword;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getComfirmPassword() {
return comfirmPassword;
}
public void setComfirmPassword(String comfirmPassword) {
this.comfirmPassword = comfirmPassword;
}
private int age;
public int getAge() {
return age;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public void setName(String name) {
this.name = name;
}
}
调用方法:
<a href="user/userAdd.action?user.name=Don&user.age=26">添加用户</a>
3、通过模型驱动传参数(Model Driven)
UserAction.java
package com.bebig.struts2.user.action;
import com.bebig.struts2.user.model.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class UserAction extends ActionSupport implements ModelDriven<User> {
private User user = new User();
public String add() {
System.out.println("name:"+user.getName());
System.out.println("age:"+user.getAge());
return SUCCESS;
}
@Override
public User getModel()
{
return user;
}
}
User.java
(源代码同上)
调用方法:
<p>使用ModelDriven传参数</p>
<a href="user/userAdd.action?name=Don&age=26">添加用户</a>