在@FacesConverter 中未注入 ManagedProperty [英] ManagedProperty not injected in @FacesConverter
问题描述
我正在尝试通过以下方式在我的 FacesConverted 中注入 ManagedBean:
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;
}
}
问题是 groupService 没有被注入,我得到了一个 NullPointerEx.它不应该自动装配,因为它也是 ManagedBean 吗?当我将getAsObject"更改为return new Group();"时,这一切都有效很明显.
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.
有什么想法吗?
推荐答案
很可能您没有解决托管 bean 名称.
@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}" />
当在第一个组件上设置转换器时,它将由 Application.createConverter.转换器不是托管 bean.如果您匹配一个转换器按类型.
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.
在第二个组件中,一个值表达式用于返回一个实现了转换器.这使用通常的托管 bean 机制.在这种情况下,@FacesConverter
注释是无关紧要的.
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.
这篇关于在@FacesConverter 中未注入 ManagedProperty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!