SJuan76
SJuan76

Reputation: 24895

Insert a bean into a Converter

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;
    }
  }

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;
  }

}

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?

Upvotes: 0

Views: 2116

Answers (2)

Brian
Brian

Reputation: 477

You can't (currently) inject dependencies into converters. However, if you can use Seam 3, the seam-faces module will enable this. You don't need to do anything special, just have the seam-faces JAR (and any of its dependencies) in the classpath and injection into converters will magically work. Just watch out for other unintended side-effects (I've noticed differences in transaction boundaries when the seam-persistence JAR is in the classpath).

Upvotes: 1

Petr Mensik
Petr Mensik

Reputation: 27526

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");

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)

Upvotes: 0

Related Questions