本文主要是介绍Structs2参数传递,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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 ="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
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;
// }
}
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
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;
}
}
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 ="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
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;
}
}
(源代码同上)
调用方法:
< p > 使用ModelDriven传参数 </ p >
< a href ="user/userAdd.action?name=Don&age=26" > 添加用户 </ a >
< a href ="user/userAdd.action?name=Don&age=26" > 添加用户 </ a >
如果视图存在中文参数,应尽量使用post方法提交,以防止发生乱码。
这篇关于Structs2参数传递的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!