向值栈放对象的步骤如下:
1、定义一个对象变量,比如User;
package com.manman.cm;
public class User {
}
2、生成变量的get方法;
private String username;
private String password;
private String address;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
3、在执行的方法里面面向对象中设置值;
那我既然要用之前创建的User,那就需要去给它new一个,然后生成他的get方法,最后给里面放值。
package com.manman.cm;
import com.opensymphony.xwork2.ActionSupport;
public class ObjectDemoAction extends ActionSupport{
private User user = new User();
public User getUser() {
return user;
}
@Override
public String execute() throws Exception {
user.setUsername("Alisa");
user.setPassword("123");
user.setAddress("西安");
return "success";
}
}
这里依然是用标签查看,运行结果如下:
image.png
把值存进了User里面,成功!!!
向值栈中放list集合
用途:查询表中所有的对象,就可以用向值栈中放list集合。
第一步:定义list集合变量。
package com.manman.cm;
public class UserLIst {
private String username;
private String password;
private String address;
@Override
public String toString() {
return "UserLIst [username=" + username + ", password=" + password + ", address=" + address + "]";
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
第二步:生成变量的get方法。
//1定义list变量
private List<UserLIst> list =new ArrayList<UserLIst>();
//2生成get方法
public List<UserLIst> getList() {
return list;
}
第三步:在执行的方法里面向list集合设置值。
//向list中设置值
UserLIst user1 = new UserLIst();
user1.setUsername("manman");
user1 .setPassword("201");
user1 .setAddress("ouya");
UserLIst user2 = new UserLIst();
user2.setUsername("mama");
user2 .setPassword("20");
user2 .setAddress("ou");
list.add(user1);
list.add(user2);
return NONE;
运行结果图:
image.png
这样就向list中存进了值。
网友评论