#{param} 上的 @ManagedProperty 在 @ViewScoped 中不起作用

发布于 2024-11-01 18:34:03 字数 1399 浏览 0 评论 0原文

我的 bean 有这个:

@ManagedBean
@ViewScoped
public class BookBean implements Serializable
{       
    @ManagedProperty(value = "#{param.id}") // does not work with @ViewScoped
    private String id;

    public void init()
    {
        id = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id")
        if (id != null) {
            System.out.println("ID: " + id);
            currentBook = bookService.find(id);
        }
    }

    @PostConstruct
    public void post()
    {   
        // does not work with @ViewScoped
        System.out.println("ID: " + id);
        currentBook = bookService.find(id);    
    }

    public String getId() {
        return id;
    } 

    public void setId(String id) {
       this.id = id;
    }
}

目标 Facelet 有这个:

<f:metadata>
    <f:viewParam name="id" value="#{bookBean.id}">
        <f:event type="preRenderView" listener="#{bookBean.init}" />
    </f:viewParam>
</f:metadata> 

通过测试,我注意到 @ManagedProperty@PostConstruct 仅适用于 @RequestScoped 豆。

对于 @ViewScoped bean,我发现我必须执行 FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id") 才能获取id 参数的值。

这是使用 @ViewScoped 获取请求参数值的唯一方法吗?

有什么想法吗?

My bean has this:

@ManagedBean
@ViewScoped
public class BookBean implements Serializable
{       
    @ManagedProperty(value = "#{param.id}") // does not work with @ViewScoped
    private String id;

    public void init()
    {
        id = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id")
        if (id != null) {
            System.out.println("ID: " + id);
            currentBook = bookService.find(id);
        }
    }

    @PostConstruct
    public void post()
    {   
        // does not work with @ViewScoped
        System.out.println("ID: " + id);
        currentBook = bookService.find(id);    
    }

    public String getId() {
        return id;
    } 

    public void setId(String id) {
       this.id = id;
    }
}

And the destination Facelet has this:

<f:metadata>
    <f:viewParam name="id" value="#{bookBean.id}">
        <f:event type="preRenderView" listener="#{bookBean.init}" />
    </f:viewParam>
</f:metadata> 

Through testing, I've noticed that @ManagedProperty and @PostConstruct only work with @RequestScoped bean.

For @ViewScoped bean, I found that I had to do this FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id") to get the value of the id parameter.

Is this the only way to get a request parameter's value with @ViewScoped?

Any thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

魂ガ小子 2024-11-08 18:34:03

视图范围比请求范围更广泛。 @ManagedProperty 只能设置与托管 bean 范围相比具有相同或更宽范围的属性。

只需继续使用 。你不应该将它们嵌套在一起。

<f:metadata>
    <f:viewParam name="id" value="#{bookBean.id}" />
    <f:event type="preRenderView" listener="#{bookBean.init}" />
</f:metadata> 

<

@ManagedBean
@ViewScoped
public class BookBean implements Serializable {

    private String id;

    public void init() {
        if (id != null) {
            currentBook = bookService.find(id);
        }
    }

    // ...
}

f:viewParam> 将设置请求参数, 将在设置这些参数后执行侦听器方法。

@PostConstruct 在视图作用域 bean 上也能正常工作,但它仅在 bean 构造并且设置所有依赖项注入后直接运行(例如 @ManagedProperty@EJB@Inject@Resource 等)。然而 之后会设置该属性,因此它在 @PostConstruct 中不可用。

The view scope is broader than the request scope. The @ManagedProperty can only set properties which have the same or broader scope as compared to the managed bean's scope.

Just keep using <f:viewParam> with <f:event>. You should only not nest them in each other.

<f:metadata>
    <f:viewParam name="id" value="#{bookBean.id}" />
    <f:event type="preRenderView" listener="#{bookBean.init}" />
</f:metadata> 

with

@ManagedBean
@ViewScoped
public class BookBean implements Serializable {

    private String id;

    public void init() {
        if (id != null) {
            currentBook = bookService.find(id);
        }
    }

    // ...
}

The <f:viewParam> will set the request parameter and the <f:event> will execute the listener method after setting of those parameters.

The @PostConstruct works fine on view scoped beans as well, but it only runs directly after bean's construction and all dependency injections are been set (such as @ManagedProperty, @EJB, @Inject, @Resource, etc). The <f:viewParam> however sets the property thereafter, so it's not available in @PostConstruct.

神爱温柔 2024-11-08 18:34:03

这是在 ViewScoped bean 中获取请求参数的另一种方法。这就是 #{param.device} 在 RequestScoped bean 中得到的内容。这样做的优点是在表示层中不需要任何标签。

private int deviceID;
public int getDeviceID() {
    if (deviceID == 0) {
        String s = FacesContext.getCurrentInstance().getExternalContext().
                getRequestParameterMap().get("device");
        try {
            deviceID = Integer.parseInt(s);
        } catch (NumberFormatException nfe) {
        }
    }
    return deviceID;
}

Here is another method for getting a request parameter inside a ViewScoped bean. This would be what #{param.device} would get in a RequestScoped bean. This has the advantage of not requiring any tags in the presentation layer.

private int deviceID;
public int getDeviceID() {
    if (deviceID == 0) {
        String s = FacesContext.getCurrentInstance().getExternalContext().
                getRequestParameterMap().get("device");
        try {
            deviceID = Integer.parseInt(s);
        } catch (NumberFormatException nfe) {
        }
    }
    return deviceID;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文