ManagedProperty未注入@FacesConverter [英] ManagedProperty not injected in @FacesConverter

查看:135
本文介绍了ManagedProperty未注入@FacesConverter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  @ManagedBean 
@RequestScoped

我试图在我的Faces中注入ManagedBean
@FacesConverter(forClass = Group.class)
public class GroupConverter implements Converter {

@ManagedProperty(#{groupService})
private GroupService groupService;

@Override
public Group getAsObject(FacesContext context,UIComponent arg1,
String groupName){
return groupService.findGroupByName(groupName);
}

@Override
public String getAsString(FacesContext arg0,UIComponent arg1,Object group){
return((Group)group).getName();
}

public GroupService getGroupService(){
return groupService;
}

public void setGroupService(GroupService groupService){
this.groupService = groupService;
}

}

问题是groupService isn'我被注入,我得到一个NullPointerEx。不应该自动连线,因为它也是一个ManagedBean?当我将getAsObject更改为返回新的Group();



任何想法?

解决方案

很可能你没有解决托管bean名称

  @ManagedBean(name =myConverter)
@RequestScoped
@FacesConverter(value =myConverter )
public class MyConverter实现转换器{

例如,考虑这两个组件: / p>

 < h:inputText converter =myConvertervalue =#{foo.prop}/> 
< h:inputText converter =#{myConverter}value =#{bar.prop}/>

当转换器设置在第一个组件上时,它将由 Application.createConverter 转换器不是托管bean 。如果您匹配按类型转换



在第二个组件中,值表达式用于返回实现Converter 。这使用通常的托管bean机制。在这种情况下, @FacesConverter 注释是无关紧要的。


I'm trying to inject a ManagedBean in my FacesConverted the following way:

@ManagedBean
@RequestScoped
@FacesConverter(forClass = Group.class)
public class GroupConverter implements Converter {

@ManagedProperty("#{groupService}")
private GroupService groupService;

@Override
public Group getAsObject(FacesContext context, UIComponent arg1,
        String groupName) {
    return groupService.findGroupByName(groupName);
}

@Override
public String getAsString(FacesContext arg0, UIComponent arg1, Object group) {
    return ((Group) group).getName();
}

public GroupService getGroupService() {
    return groupService;
}

public void setGroupService(GroupService groupService) {
    this.groupService = groupService;
}

}

The problem is that groupService isn't being injected and I get a NullPointerEx. Shouldn't it be autowired automatically since it's also a ManagedBean? It all works when I change "getAsObject" to "return new Group();" obviously.

Any ideas?

解决方案

It is likely that you are not resolving the managed bean name.

@ManagedBean(name = "myConverter")
@RequestScoped
@FacesConverter(value = "myConverter")
public class MyConverter implements Converter {

For example, consider these two components:

        <h:inputText converter="myConverter" value="#{foo.prop}" />
        <h:inputText converter="#{myConverter}" value="#{bar.prop}" />

When the converter is set on the first component, it will be created by Application.createConverter. A converter is not a managed bean. The same rules apply if you match a converter by type.

In the second component, a value expression is used to return a class that implements Converter. This uses the usual managed bean mechanisms. In this case, the @FacesConverter annotation is irrelevant.

这篇关于ManagedProperty未注入@FacesConverter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆