首页 > 代码库 > Struts2_三种传参数方法
Struts2_三种传参数方法
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;
}
}
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="http://www.mamicode.com/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;
// }
}
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;
}
}
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;
}
}
//数据传输对象
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="http://www.mamicode.com/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;
}
}
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="http://www.mamicode.com/user/userAdd.action?name=Don&age=26">添加用户</a>
<a href="http://www.mamicode.com/user/userAdd.action?name=Don&age=26">添加用户</a>
Struts2_三种传参数方法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。