struts对象绑定属性被覆盖,初学struts用法问题?
描述:action中有user对象,这个类中有add()和update() ,但是在add(){
user = new User();
user.setUserName("某个");
//操纵属性
}
update(){
user.getUserName();
}
这样加入多线程情况下(用户提交表单到update , 但是add被人先执行了,那么user被清空。属性被覆盖),update中的getUsername就不是前台绑定并传递过来的,而是add中set的【属性丢了】
不知道我描述的清除吗
附上action一点代码
package com.hzyc.hzycpos.action;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.stereotype.Controller;
import com.hzyc.hzycpos.domain.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
/**
* 用户管理控制层
* 通过实现自动引入接口实现IOC方式的request注入
*
* @author SHAOSHUAI
* 使用ModelDriven<User>驱动方式,对于传递多个对象不方便
* 改使用set对象方法传递
*/
@Controller
public class UserAction extends ActionSupport implements ServletRequestAware, ServletResponseAware {
//model绑定
private User user;
//List<User> User对象集合
private List<User> userList;
private HttpServletRequest request;
private HttpServletResponse response;
/**
*
*/
private static final long serialVersionUID = 1L;
private String info = "";
public String add() throws Exception{
info = "添加用户信息";
//如何能获取input中的值呢
System.out.println(user.getUsername());
request.setAttribute("name","是谁呢");
user = new User();
user.setPassword("123456");
user.setUsername("邵帅");
userList = new ArrayList<User>();
userList.add(user);
return SUCCESS;
}
public String update() throws Exception{
info = "更新用户信息";
System.out.println(user.getUsername()+"====update");
return ERROR;
}
public String getInfo(){
return info;
}
public void setInfo(String info){
this.info = info;
}
public void setServletResponse(HttpServletResponse response) {
this.response = response;
}
public void setServletRequest(HttpServletRequest request) {
// TODO Auto-generated method stub
this.request = request;
}
/**
* 获取参数绑定model
*/
public void setUser(User user){
System.out.println("set User对象");
this.user = user;
}
/**
* 用于jsp的ognl标签获取
* */
public User getUser(){
System.out.println("get User对象");
return this.user;
}
/**
* 用于jsp标签获取
* */
public List<User> getUserList() {
return userList;
}
}
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.stereotype.Controller;
import com.hzyc.hzycpos.domain.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
/**
* 用户管理控制层
* 通过实现自动引入接口实现IOC方式的request注入
*
* @author SHAOSHUAI
* 使用ModelDriven<User>驱动方式,对于传递多个对象不方便
* 改使用set对象方法传递
*/
@Controller
public class UserAction extends ActionSupport implements ServletRequestAware, ServletResponseAware {
//model绑定
private User user;
//List<User> User对象集合
private List<User> userList;
private HttpServletRequest request;
private HttpServletResponse response;
/**
*
*/
private static final long serialVersionUID = 1L;
private String info = "";
public String add() throws Exception{
info = "添加用户信息";
//如何能获取input中的值呢
System.out.println(user.getUsername());
request.setAttribute("name","是谁呢");
user = new User();
user.setPassword("123456");
user.setUsername("邵帅");
userList = new ArrayList<User>();
userList.add(user);
return SUCCESS;
}
public String update() throws Exception{
info = "更新用户信息";
System.out.println(user.getUsername()+"====update");
return ERROR;
}
public String getInfo(){
return info;
}
public void setInfo(String info){
this.info = info;
}
public void setServletResponse(HttpServletResponse response) {
this.response = response;
}
public void setServletRequest(HttpServletRequest request) {
// TODO Auto-generated method stub
this.request = request;
}
/**
* 获取参数绑定model
*/
public void setUser(User user){
System.out.println("set User对象");
this.user = user;
}
/**
* 用于jsp的ognl标签获取
* */
public User getUser(){
System.out.println("get User对象");
return this.user;
}
/**
* 用于jsp标签获取
* */
public List<User> getUserList() {
return userList;
}
}