ManagedProperty 未注入@FacesConverter
我试图通过以下方式在我的 FacesConverted 中注入 ManagedBean:
@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();”时,一切都有效明显地。
有什么想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能没有解析 托管 bean 名称。
例如,考虑这两个组件:
当在第一个组件上设置转换器时,它将由 Application.createConverter。 转换器不是托管 bean。如果您 匹配按类型划分的转换器。
在第二个组件中,值表达式用于返回实现 Converter< 的类/a>.这使用了通常的托管 bean 机制。在这种情况下,
@FacesConverter
注释是无关紧要的。It is likely that you are not resolving the managed bean name.
For example, consider these two components:
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.