我有以下 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注入转换器
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;
}
}
每当我尝试访问该属性时,它都是空的。setter 永远不会被调用。
从这个问题和这个问题来看,似乎无法以通常的方式注入 bean(如果我错了,请纠正我)。有什么替代方法可以避免每次都从 EJB(= 数据库访问)中获取整个值列表吗?