Jsf 为什么我的@Inject';将转换器与@Param一起使用时,ed字段为null?
我在这里有一个非常基本的设置:Jsf 为什么我的@Inject';将转换器与@Param一起使用时,ed字段为null?,jsf,cdi,jboss-weld,omnifaces,Jsf,Cdi,Jboss Weld,Omnifaces,我在这里有一个非常基本的设置: @Named @ApplicationScoped public class TalentIdConverter implements Converter { @EJB private EntityManagerDao em; @Override public Object getAsObject(FacesContext context, UIComponent component, String value) {
@Named
@ApplicationScoped
public class TalentIdConverter implements Converter {
@EJB
private EntityManagerDao em;
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
if (Strings.isNullOrEmpty(value)) {
return null;
}
return em.find(Talent.class, Long.parseLong(value));
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
return String.valueOf(((Talent) value).getId());
}
}
// Manager.class
public class Manager {
@Inject @Param(converterClass = TalentIdConverter.class, name = "talentId")
private ParamValue<Talent> curTalent
@PostConstruct
public void init() {
// use curTalent.getValue()
}
}
@Named
@适用范围
公共类TalentIdConverter实现转换器{
@EJB
私营企业管理公司;
@凌驾
公共对象getAsObject(FacesContext上下文、UIComponent组件、字符串值){
if(Strings.isNullOrEmpty(value)){
返回null;
}
返回em.find(Talent.class,Long.parseLong(value));
}
@凌驾
公共字符串getAsString(FacesContext上下文、UIComponent、对象值){
返回字符串.valueOf(((人才)值).getId());
}
}
//经理级
公共班级经理{
@Inject@Param(converterClass=TalentIdConverter.class,name=“talentId”)
私有参数值
@施工后
公共void init(){
//使用curtilene.getValue()
}
}
但是每次调用TalentIdConverter.getaObject
时,em
都为null。有人能告诉我这是为什么吗?
我还尝试在转换器上使用@facesconterver
,但行为没有改变
这是在Wildfly-8.0.0.Beta1上,使用Weld-2.1.0.CR1和Omnifaces-1.6.3在当前的1.6.3版本中,
@Param(converterClass)
创建给定转换器类的非托管实例。这就像你在做新的TalentIdConverter()
而没有任何注入一样。如果您需要托管实例,那么在此特定情况下,您应该使用CDI托管转换器类(通过@Named
注册)使用@Param(converter)
:
@Inject @Param(converter = "#{talentIdConverter}", name = "talentId")
private ParamValue<Talent> curTalent;
或者,如果它注册为@FacesConverter(forClass=Talent.class)
,则无需再明确指定转换器
@Inject @Param(name = "talentId")
private ParamValue<Talent> curTalent;
@Inject@Param(name=“talentId”)
私人准价;
另一方面,尝试通过
@Param(converterClass)
的BeanManager
创建托管实例毕竟不是一个坏主意。我们可能会在未来的OmniFaces版本中对此进行研究。感谢您的解释和forClass
提示-我觉得这很好(虽然从泛型基类继承转换器似乎有问题,但现在可能是PEBKAC)
@Inject @Param(name = "talentId")
private ParamValue<Talent> curTalent;