我已在ApplicationScoped豆以下插入豆成轉換
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;
}
}
我嘗試注入一個轉換器
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;
}
}
內部的豆每當我嘗試訪問屬性,它爲空。二傳手永遠不會被調用。
從this question和this one,它看起來像它是不可能的通常的方式注入bean(請糾正我,如果我錯了)。還有其他的選擇,所以我避免每次都必須從EJB(=數據庫訪問)中獲取整個值列表。
關閉。它是一個applicationcoped bean,所以我必須使用'FacesContext.getCurrentInstance()。getExternalContext()。getApplicationMap()。get(「priorityList」)' – SJuan76