JSF 2.0 CDI-在请求Bean中注入的会话Bean包含空属性 [英] JSF 2.0 CDI - injected session bean within request bean contains null properties

查看:152
本文介绍了JSF 2.0 CDI-在请求Bean中注入的会话Bean包含空属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在WebSphere App Server v8.0.0.5中使用JSF 2.0,CDI 1.0.

I'm using JSF 2.0, CDI 1.0 within WebSphere App Server v8.0.0.5.

我有一个奇怪的情况...成功登录后,将创建一个CDI会话范围的bean,并将用户重定向到欢迎页面.会话范围的Bean被注入到欢迎页面上引用的请求范围的Bean中.问题在于,会话范围的Bean仅在每个浏览器首次成功登录后才保留其字段值.我已经尝试过使用Chrome,Firefox甚至IE使用同一用户.如果我注销或重新启动WAS并尝试再次登录,则注入到请求范围的bean中时,会话范围的bean的值都将设置为null.

I have a bizarre situation... Upon successful login, a CDI session-scoped bean is created, and the user is redirected to a welcome page. The session-scoped bean is injected into a request-scoped bean referened on the welcome page. The problem is that the session-scoped bean ONLY retains its field values upon first successful login per browser. I've tried the same user using Chrome, Firefox, and even IE. If I log out or restart WAS and attempt to log in again, the session-scoped bean's values are all set to null when injected into the request-scoped bean.

我在所有范围内都使用javax.enterprise.context.

I'm using javax.enterprise.context for all my scopes.

请,我需要紧急帮助.由于这个问题,很多人处于危险之中.

Please, I need emergency help. A lot is riding at stake due to this problem.

登录表单的Auth bean的相关代码段(重定向后,我省略了一些代码):

Relevant snippet of login form's Auth bean (I've omitted some code after the redirect):

import com.ibm.websphere.security.WSSecurityException;
import com.ibm.websphere.security.auth.WSSubject;
import com.ibm.websphere.security.cred.WSCredential;
import com.ibm.websphere.wim.exception.WIMException;
import com.ibm.websphere.wim.util.SDOHelper;

import java.io.IOException;
import java.io.Serializable;
import java.rmi.RemoteException;
import java.security.Principal;

import javax.annotation.PostConstruct;
import javax.enterprise.context.ConversationScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import javax.security.auth.Subject;
import javax.security.auth.login.CredentialExpiredException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import com.ibm.websphere.wim.SchemaConstants;
import com.ibm.websphere.wim.Service;
import com.ibm.websphere.wim.client.LocalServiceProvider;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import com.ibm.ws.security.core.ContextManagerFactory;

import commonj.sdo.DataObject;

@Named
@ConversationScoped
public class Auth implements Serializable {
/**
 * 
 */
private static final long serialVersionUID = -6106803531512607236L;
private String userId;
private String password;
private String originalURL;

@Inject
UserService userService;
private Service service;
private String uniqueSecurityName;
private String l;

@PostConstruct
    public void init() {
    ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
    originalURL = (String) externalContext.getRequestMap().get(RequestDispatcher.FORWARD_REQUEST_URI);

    System.out.println("The PostContstruct has been called.");

    if (originalURL == null) {
        originalURL = externalContext.getRequestContextPath() + "/index.xhtml";
    } else {
        String originalQuery = (String) externalContext.getRequestMap().get(RequestDispatcher.FORWARD_QUERY_STRING);

        if (originalQuery != null) {
            originalURL += "?" + originalQuery;
        }
    }
}

public void login() throws IOException, WIMException, PrivilegedActionException {
    FacesContext context = FacesContext.getCurrentInstance();
    ExternalContext externalContext = context.getExternalContext();
    HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
    System.out.println("The login method has been called.");

    try {
        Principal userPrincipal = request.getUserPrincipal();
        request.getUserPrincipal();
        if (userPrincipal != null) {
            request.logout();
        }
        request.login(userId, password);

        User user = new User();

        if (request.isUserInRole("STAFF")) {
            Staff staff = userService.getStaff(userId);
            user.setLocation(staff.getCenter().getCity());
            user.setRole("STAFF");
            user.setUserId(userId);
            externalContext.getSessionMap().put("user", user);
            externalContext.redirect("staff/staff-home?faces-redirect=true");
        }
}

public String logout() {
    FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
    return "/index?faces-redirect=true";
}  

用户bean:

import java.io.Serializable;

import javax.enterprise.context.SessionScoped;
import javax.inject.Named;

@Named
@SessionScoped
public class User implements Serializable {
/**
 * 
 */
private static final long serialVersionUID = 7198980241243868166L;
private String role;
private String location;
private String userId;
private Role sessionRole;

public User() { }

/**
 * @return the role
 */
public String getRole() {
    return role;
}

/**
 * @param role the role to set
 */
public void setRole(String role) {
    this.role = role;
}

/**
 * @return the location
 */
public String getLocation() {
    return location;
}

/**
 * @param location the location to set
 */
public void setLocation(String location) {
    this.location = location;
}

/**
 * @return the userId
 */
public String getUserId() {
    return userId;
}

/**
 * @param userId the userId to set
 */
public void setUserId(String userId) {
    this.userId = userId;
}   
}

欢迎页面Bean的相关部分:

relevant portion of welcome page's bean:

import java.text.DateFormatSymbols;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;

@Named
@RequestScoped
public class CenterInfoBean {    
@Inject
CenterInfo centerInfo;

@Inject
User user;

private State state;
private Center center;

@PostConstruct
public void init() {
    center = centerInfo.getCenterByCityName(user.getLocation());
}

为什么在使用唯一的浏览器进行首次登录时auth只填充值,而在以后的登录时却不填充值?

Why is auth only populated with values upon the initial login with a unique browser and never populated with values upon subsequent logins?

推荐答案

将容器托管的Bean代码与类似的东西混合在一起不是一个好主意,

It is not a good idea to mix the container managed bean code with something like ,

User user = new User();

将其添加到sessionMap应该可以,但是如果容器已经解决了在会话中已经注入会话Bean的问题,又该怎么办呢?

adding it in sessionMap should work but what if container has already resolved the injection of session bean in your request bean as you are already in session.

当用户由容器管理时,请尝试避免使用User user = new User();这样的代码.

Try avoiding code like User user = new User(); when User is container managed.

在您的情况下,我建议您检查用户是否已在会话中.

In your case I would suggest checking if the User is already there in session.

User user = (User)externalContext.getSessionMap().get("user");

如果可以,请更新此引用,如果不可用,请继续

if so then update this reference , if it is not available then go with

User user = new User();

这篇关于JSF 2.0 CDI-在请求Bean中注入的会话Bean包含空属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆