4

通过定义托管属性将一个托管bean注入另一个托管bean时遇到了一些麻烦。但是该属性为空。谁能告诉我,为什么我的财产是空的?用户登录Bean.java

    @RequestScoped
public class UserLoginBean extends AbstractMB implements Serializable{

    private static final long serialVersionUID = 1L;
    private String username;
        private String password;
//getter and setter
        public void login() throws ServletException, IOException{
    try
    {
      ExternalContext context = FacesContext.getCurrentInstance().getExternalContext(); 
      HttpServletRequest request = ((HttpServletRequest)context.getRequest());

      ServletResponse resposnse = ((ServletResponse)context.getResponse());
      RequestDispatcher dispatcher = request.getRequestDispatcher("/j_spring_security_check");
      dispatcher.forward(request, resposnse);
      FacesContext.getCurrentInstance().responseComplete();
     }
    catch (Exception ex) {
        ex.printStackTrace();
        displayErrorMessageToUser("Login or Password Failed");
    }

回收MB.java

@RequestScoped
public class ReclamationMB extends AbstractMB implements Serializable {
...
@ManagedProperty("#{loginBean}")
    private UserLoginBean userLogin;
/getter and setter

但在 .xhtml 中它不为空,它返回用户名:

<h:outputText value="#{loginBean.username}
4

1 回答 1

0

ReclamationMB 中的 userLogin 是 null 还是用户名 null?如果 userLogin 不为 null,则注入工作正常,但 bean 是新创建的,因为它是 Requestscoped。您可以将 UserLoginBean 放入 Viewscope 以防止重新创建 bean:

@ViewScoped
 public class UserLoginBean extends AbstractMB implements Serializable{...}

或者您可以通过提供 PostConstruct 方法在 ReclamationsMB 中手动填充 userLogin 所需的数据

@RequestScoped
 public class ReclamationMB extends AbstractMB implements Serializable {
 ...
 @ManagedProperty("#{loginBean}")
 private UserLoginBean userLogin; //getter and setter

 @PostConstruct
 public void init() {
   //Call Bean methods or set variables manually
   userLogin.setUsername(...);
 }
于 2013-05-24T14:09:48.990 回答