开发者

How do I access EJB bean when inside a custom Converter [duplicate]

开发者 https://www.devze.com 2023-01-14 11:52 出处:网络
This question already has answers here: How to inject @EJB, @PersistenceContext, @Inject, @Autowired, etc in @FacesConverter?
This question already has answers here: How to inject @EJB, @PersistenceContext, @Inject, @Autowired, etc in @FacesConverter? (5 answers) Closed 8 years ago.

This converter is called from my JSF. I already register it inside faces-config.xml

public class ProjectConverter implements Converter{

    @EJB
    DocumentSBean sBean;

    @ManagedProperty(value="#{logging}")
    private Logging log;    

    public ProjectConverter(){
    }

    public Object getAsObject(FacesContext context, UIComponent component, String value) 
    {
        if(value.trim().equals("")){
            return null;
        }
        return sBean.getProjectById(value);

    }

    public String getAsString(FacesContext context, UIComponent component, Object value) 
    {
        if(value == null){
            return null;
        }
        return String.valueOf(((Project) value).getId());
    }
}

I ran i开发者_如何学JAVAnto java.lang.NullPointerException, when I am in getAsObject(), the primary reason is because my Session Bean sBean is null. I dont know how to fix this, I need to access to my Session bean so that I can query from my database


As BalusC said, injection only works in managed beans. You can however declare your converter as a managed bean in your faces-config

<managed-bean>
   <managed-bean-name>myConverter</managed-bean-name>
   <managed-bean-class>com.example.MyConverter</managed-bean-class>
   <managed-bean-scope>request</managed-bean-scope>
</managed-bean>

And later reference it in a jsf component with an el expression:

<h:outputText value="#{myBean.value}" converter="#{myConverter}" />
0

精彩评论

暂无评论...
验证码 换一张
取 消