将bean插入Converter [英] Insert a bean into a Converter

查看:161
本文介绍了将bean插入Converter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下ApplicationScoped bean

I have the following ApplicationScoped bean

package es.caib.gesma.gesman.data;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import es.caib.gesma.gesman.ejb.Dao;

@ManagedBean(name="priorityList")
@ApplicationScoped
public class PriorityList {

  @EJB
  Dao daoEjb;

  private List<Priority> priorities = null;

    public PriorityList() {
    }

    @PostConstruct
    public void refresh() {
      this.priorities = daoEjb.findPriorities();
    }

    public List<Priority> getPriorities() {
      return this.priorities;
    }

    public Priority fromId(int id) {
      for(Priority priority : this.priorities) {
        if (priority.getId() == id) {
          return priority;
        }
      }
      return null;
    }
  }

我尝试将bean注入Converter中

I try to inject that bean inside a Converter

package es.caib.gesma.gesman.data.converter;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;

import es.caib.gesma.gesman.data.Priority;
import es.caib.gesma.gesman.data.PriorityList;

@ManagedBean
@ApplicationScoped
public class PriorityConverter implements Converter {

  @ManagedProperty("#{priorityList}")
  private PriorityList priorityList;

  @Override
  public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
    ...
  }

  @Override
  public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
    ...
  }

  public void setPriorityList(PriorityList priorityList) {
    System.out.println("SET PRIORITYLIST " + priorityList);
    this.priorityList = priorityList;
  }

}

每当我尝试访问该属性时,该属性为null.设置器永远不会被调用.

Whenever I try to access the property, it is null. The setter is never called.

来自此问题

From this question and this one, it looks like it is not possible to inject the bean the usual way (please correct me if I am wrong). There is any alternative so I avoid having to get the entire list of values from the EJB (= database access) each time?

推荐答案

我认为您应该能够从HttpSession中拉出这个bean(它在SessionListener和SessionScoped bean中对我有用)

I think you should be able to pull this bean out from the HttpSession (it works for me in PhaseListener with SessionScoped bean)

FacesContext context = FacesContext.getCurrentInstance();
HttpSession session = (HttpSession) context.getExternalContext().getSession(true);
SessionForm sessionBean = (SessionForm) session.getAttribute("priorityList");

或者如果我可以借用BalusC的有关 JSF交流的文章,在底部描述了如何从ManagedBean制作转换器(以便您可以轻松地在其中注入ApplicationScoped bean)

Or if I may borrow article from BalusC about JSF communication, on the bottom is described how to make a converter from ManagedBean (so you could easily inject your ApplicationScoped bean there)

这篇关于将bean插入Converter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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