1、通过属性传参数(Attr) UserAction.java
代码语言:javascript复制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;
}
}
调用方法:
代码语言:javascript复制<a href="user/userAdd.action?name=Don&age=26">添加用户</a>
2、通过域模型传参数(Domain Model) UserAction.java
代码语言:javascript复制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());
return SUCCESS;
}
}
User.java
代码语言:javascript复制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;
}
}
调用方法:
代码语言:javascript复制<a href="user/userAdd.action?user.name=Don&user.age=26">添加用户</a>
3、通过模型驱动传参数(Model Driven) UserAction.java
代码语言:javascript复制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;
}
}
代码语言:javascript复制<p>使用ModelDriven传参数</p>
Struts2中传递参数和方法 - happyfish - happyfish的博客<a href="user/userAdd.action?name=Don&age=26">添加用户</a>