ManagedProperty 未注入@FacesConverter

发布于 2024-11-09 23:25:17 字数 876 浏览 7 评论 0原文

我试图通过以下方式在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荒岛晴空 2024-11-16 23:25:18

您可能没有解析 托管 bean 名称

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

例如,考虑这两个组件:

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

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文