1

我一直在 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,我正在使用pickUser检索第二个用户对象,但它替换了用户对象。

采摘豆在这里

   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 键在会话中搜索第二个用户,但它正在替换现有的用户。

谢谢你。

4

1 回答 1

1

我修复了这个错误。我在登录时将用户对象存储到会话中,然后我使用相同的用户对象来操作和显示用户列表。如果我选择任何一个用户,它会自动替换会话中现有的用户对象。谢谢你。

#{user}已经在会话中

我在这里使用了相同的对象

<ui:repeat var="user" value="#{users}">
..........
</ui:repeat>

在这里它会自动替换现有的。抱歉,添麻烦了。谢谢你。

于 2013-06-12T08:41:30.203 回答