EJB 3.1 中 Bean 未注入,只有该 Bean 具有此行为
1 和 JSF 2.0 + primefaces 很酷:D
我有这个验证器,但我无法将持久性单元正确注入其中。
所有其他 bean 都工作正常,这是唯一一个不能正常工作的 bean,它在 roleFacade
处给了我一个 Nullpointer
注入验证器是否非法?现在处于生命周期是否为时过早?它会是什么?
谢谢 !
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
@FacesConverter("rolesConverter")
@Stateless
public class RolesConverter implements Converter{
@EJB
private RoleFacade roleFacade;
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
Roles role = null;
if ((value != null) && (!value.equals(""))) {
role = roleFacade.find(Long.valueOf(value));
}
return role;
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
Integer retorno = null;
if (!(value == null)) {
Roles role = new Roles();
role = (Roles) value;
retorno = role.getId();
}
return retorno.toString();
}
和
外观
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Stateless
public class RoleFacade extends AbstractFacade<Roles> {
@PersistenceContext(unitName = "br.com.cflex.itm-PU", type= PersistenceContextType.TRANSACTION)
private EntityManager em;
private static Logger log = LoggerFactory.getLogger(RoleFacade.class);
@Override
protected EntityManager getEntityManager() {
return em;
}
public RoleFacade() {
super(Roles.class);
}
public RoleFacade(EntityManager em) {
super(Roles.class);
this.em = em;
}
public List<Roles> getListOfRoles(){
log.debug("Calling method 'RoleFacade: List getListOfRoles()'");
List<Roles> list = getEntityManager().createNamedQuery("Roles.findAll").getResultList();
return list;
}
}
1 and JSF 2.0 + primefaces which is cool :D
And I have this validator, but I cannot get the persistence unit injected properly in it.
All other beans are working properlly, this is the only one that doesn't, it gives me a Nullpointer at the roleFacade
Is it illegal to inject in a validator ? is it too early in the life cycle ? what can it be ?
Thanks !
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
@FacesConverter("rolesConverter")
@Stateless
public class RolesConverter implements Converter{
@EJB
private RoleFacade roleFacade;
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
Roles role = null;
if ((value != null) && (!value.equals(""))) {
role = roleFacade.find(Long.valueOf(value));
}
return role;
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
Integer retorno = null;
if (!(value == null)) {
Roles role = new Roles();
role = (Roles) value;
retorno = role.getId();
}
return retorno.toString();
}
}
And the Facade
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Stateless
public class RoleFacade extends AbstractFacade<Roles> {
@PersistenceContext(unitName = "br.com.cflex.itm-PU", type= PersistenceContextType.TRANSACTION)
private EntityManager em;
private static Logger log = LoggerFactory.getLogger(RoleFacade.class);
@Override
protected EntityManager getEntityManager() {
return em;
}
public RoleFacade() {
super(Roles.class);
}
public RoleFacade(EntityManager em) {
super(Roles.class);
this.em = em;
}
public List<Roles> getListOfRoles(){
log.debug("Calling method 'RoleFacade: List getListOfRoles()'");
List<Roles> list = getEntityManager().createNamedQuery("Roles.findAll").getResultList();
return list;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
你可以做到,只是你做错了。
试试这个:
在您的 xhtml 中,使用
converter="#{rolesConverter}"
。当然,我假设您使用的是 CDI。顺便说一下,PrimeFaces 非常棒。 :)
You can do it, you're just doing it wrong.
Try this:
And in your xhtml, use
converter="#{rolesConverter}"
. Of course, I'm assuming you're using CDI.And PrimeFaces is awesome, by the way. :)
您必须问自己的另一个问题是:让转换器查看数据库是否是正确的设计原则?我的回答是:绝对不会。视图与模型紧密相连。推迟对真实对象的查找,直到对数据库进行更改(在持久层中)。
Another question you have to ask yourself is: Is it a correct design principle to have the converter to look in the database? My answer: definitely not. View is tied up with the model. Postpone that lookup of the real object until you do the changes to the DB (in persistence layer).
根据 Java EE 6 平台规范,表 EE.5-1,仅 JSF 托管 bean 类支持注入,其他 JSF 类不支持注入。
Per the Java EE 6 platform specification, table EE.5-1, injection is only supported for JSF managed bean classes, not other JSF classes.
您听说过 CODI 吗?
@Advanced 注释可以解决问题。
https://cwiki.apache.org/confluence/display/EXTCDI/JSF+Usage
Have you heard about CODI?
@Advanced annotation do the trick.
https://cwiki.apache.org/confluence/display/EXTCDI/JSF+Usage
也许你认为这是题外话。然而,作为一名使用过大多数(但不是全部)框架的 Web 开发人员,请远离 JSF。这一定是有史以来效率最低、最耗时的地狱般的框架。你想做出成绩吗?通过 REST 学习 Ext JS。也许并不适合所有应用程序,但对许多应用程序来说都是值得的。请认为这是友好的建议,无意冒犯任何人。
Maybe you think this is off topic. However, as a web developer having used the majority (not all however) of frameworks, please stay far far far away from JSF. That must be the least productive most time-consuming hellish framework ever built. You want to make results? Learn Ext JS with REST. Maybe not suited for all apps but worthwhile for many.. Please consider this to be friendly advice without any intention to insult no one.
在构造函数中进行查找。
<前>尝试{
InitialContext ctx = new InitialContext();
MyInterface myInstance = (MyInterface) ctx.lookup("java:module/MyEJB");
} catch (NamingException e) {
log.error(e.getMessage());
}
Do a lookup in constructor.