我一直在开发jsf中的web应用程序。我使用会话来访问来自不同页面的对象。首先我在登录时将用户对象存储在会话中,在一些导航后我想要另一个用户对象,所以,我已经在会话中将该对象存储在不同的键名中,但是在存储第二个时,它正在替换第一个并且它具有不同的关键名称。我怎么能纠正这个。
package com.transcare.ace.bean;
import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import com.transcare.ace.appBean.ChoiceBean;
import com.transcare.ace.dao.ServiceRequestDAO;
import com.transcare.ace.utils.DisplayMessage;
import com.transcare.ace.vo.Role;
import com.transcare.ace.vo.ServiceCategory;
import com.transcare.ace.vo.ServiceRequest;
import com.transcare.ace.vo.ServiceStatus;
import com.transcare.ace.vo.User;
@ManagedBean(name = "userRequest")
@ViewScoped
public class ServiceRequestAction implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@ManagedProperty(value = "#{selectedRequest}")
private ServiceRequest service = null;
@ManagedProperty(value = "#{user}")
private User user = null;
@ManagedProperty(value = "#{pickedUser}")
private User pickedCustomer = null;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public ServiceRequest getService() {
return service;
}
public void setService(ServiceRequest service) {
this.service = service;
}
@ManagedProperty(value = "#{choices}")
private ChoiceBean choices = null;
public ChoiceBean getChoices() {
return choices;
}
public void setChoices(ChoiceBean choices) {
this.choices = choices;
}
public User getPickedCustomer() {
return pickedCustomer;
}
public void setPickedCustomer(User pickedCustomer) {
this.pickedCustomer = pickedCustomer;
}
@PostConstruct
public void init() {
/*
* FacesContext.getCurrentInstance().getExternalContext().getSessionMap()
* .remove("role");
* FacesContext.getCurrentInstance().getExternalContext(
* ).getSessionMap() .remove("redirect");
* FacesContext.getCurrentInstance
* ().getExternalContext().getSessionMap() .remove("newCustomer");
* FacesContext
* .getCurrentInstance().getExternalContext().getSessionMap()
* .remove("userRequest");
*/
// System.out.println("session cleared!");
service = new ServiceRequest();
service.setCategory(new ServiceCategory());
//pickedCustomer=(User) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("pickedUser");
if (pickedCustomer != null) {
service.setCustomer(pickedCustomer);
System.out.println("Picked User value name is:"
+ pickedCustomer.getFullName());
} else {
System.out.println("Picked User value is null.");
}
System.out.println("Session user");
System.out.println("Logged User role is:"
+ user.getRole().getRoleName() +" and the user is: "+ user.getFullName());
}
@PreDestroy
public void preDestroy(){
System.out.println("View scope expires for user request!");
FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("pickedUser");
}
public String addNewServiceRequestForUser() {
System.out.println("main called");
System.out.println(service.getTitle());
System.out.println(service.getDescription());
System.out.println(service.getCategory().getServiceCategoryId());
ServiceStatus status = new ServiceStatus();
status.setStatus("CREATED");
status.setStatusId(1);
service.setStatus(status);
service.setCustomer(pickedCustomer);
try {
ServiceRequestDAO.getInstance().getrequestByUser(this);
DisplayMessage.setInfoMessage("dashboardForm",
"You have created the request successfully!", "");
return "success";
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DisplayMessage.setInfoMessage("",
"Creating request is failed! Try again.", "");
return null;
}
public String addNewServiceRequestWithPayment() {
System.out.println("main called");
System.out.println(service.getTitle());
System.out.println(service.getDescription());
System.out.println(service.getCategory().getServiceCategoryId());
try {
int key = ServiceRequestDAO.getInstance().getrequestByUser(this);
if (key > 0) {
DisplayMessage.setInfoMessage("dashboardForm",
"You have created the request successfully!", "");
FacesContext.getCurrentInstance().getExternalContext()
.getSessionMap().put("redirect", "/pages/dashBoard");
FacesContext.getCurrentInstance().getExternalContext()
.getSessionMap().put("serviceIdKey", key);
System.out.println("Process success waiting to redirect!");
// System.out.print(key);
return "success";
}
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DisplayMessage.setInfoMessage("",
"Creating request is failed! Try again.", "");
return null;
}
public String selectCustomer() {
Role role = new Role();
role.setInternal(false);
role.setRoleId(1);
role.setRoleName("Customer");
FacesContext.getCurrentInstance().getExternalContext().getSessionMap()
.remove("pickedUser");
FacesContext.getCurrentInstance().getExternalContext().getSessionMap()
.put("role", role);
FacesContext.getCurrentInstance().getExternalContext().getSessionMap()
.put("redirect", "/pages/serviceRequest");
/*
* FacesContext.getCurrentInstance().getExternalContext().getSessionMap()
* .put("newCustomer", true);
*/
return "success";
}
public String makePayment() {
FacesContext.getCurrentInstance().getExternalContext().getSessionMap()
.put("redirect", "/pages/serviceRequest");
return "success";
}
}
这是我的托管bean,我正在使用 pickedUser 来检索第二个用户对象,但它取代了用户对象。
选择器bean在这里
package com.transcare.ace.bean;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import com.transcare.ace.dao.PickUserDAO;
import com.transcare.ace.vo.Role;
import com.transcare.ace.vo.User;
@ManagedBean
@ViewScoped
public class PickUser implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private List<User> users;
@ManagedProperty(value = "#{role}")
private Role role = null;
@ManagedProperty(value="#{editRequest}")
private EditRequest editRequest;
@ManagedProperty(value = "#{redirect}")
private String redirectTo = null;
//@ManagedProperty(value = "#{newCustomer}")
//private boolean newCustomer;
/*public boolean isNewCustomer() {
return newCustomer;
}
public void setNewCustomer(boolean newCustomer) {
this.newCustomer = newCustomer;
}
*/
public String getRedirectTo() {
return redirectTo;
}
public void setRedirectTo(String redirectTo) {
this.redirectTo = redirectTo;
}
@PostConstruct
public void init() {
users = new PickUserDAO().getAllUsers(role);
}
public EditRequest getEditRequest() {
return editRequest;
}
public void setEditRequest(EditRequest editRequest) {
this.editRequest = editRequest;
}
public List<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
public String selectUser(User user) {
/*if (role.isInternal()) {
editRequest.getRequest().setSupplier(user);
} else if (isNewCustomer()) {
FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("pickedUser", user);
} else {
((ServiceRequest)FacesContext.getCurrentInstance()
.getExternalContext().getSessionMap()
.get("selectedService")).setCustomer(user);
editRequest.getRequest().setCustomer(user);
}*/
FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("pickedUser", user);
return redirectTo;
}
}
这里我正在使用pickedUser密钥进行会话中的第二个用户 但它正在取代现有的。
谢谢。答案 0 :(得分:0)
我修复了这个错误。我已经在登录时将用户对象存储到会话中,然后我使用相同的用户对象来操纵和显示用户列表。如果我选择任何一个用户,它会自动替换会话中的现有用户对象。谢谢。
#{user}
已在会话中
我在这里使用了相同的对象
<ui:repeat var="user" value="#{users}">
..........
</ui:repeat>
这里它会自动替换现有的。抱歉,添麻烦了。 谢谢。