@ManagedProperty(value = "#{param.id}") 在非请求作用域 Bean 中 [英] @ManagedProperty(value = "#{param.id}") in a non-request Scope Bean

查看:30
本文介绍了@ManagedProperty(value = "#{param.id}") 在非请求作用域 Bean 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将参数 (POST) 传递给 @managedBean,我使用了这样的托管属性:

I need to pass a parameter (POST) to a @managedBean, I used managed properties like this:

@ManagedProperty(value = "#{param.id}")
private int id;

Bean 的作用域是 ViewScope

And the scope of the Bean is ViewScope

我最终遇到了这个错误:

I end up with this error:

无法创建托管 bean 收据.发现以下问题: - 表达式#{param.id}, request 所引用的对象的范围比视图的引用托管bean 范围要短

Unable to create managed bean receipt. The following problems were found: - The scope of the object referenced by expression #{param.id}, request, is shorter than the referring managed beans scope of view

我能做什么?

arjan 看看:

我的页面:Facelet 标题

My page: Facelet Title

<form method="post" action="faces/index.xhtml">
  <input name="id" value="4" />
  <input type="submit" value="submit" />
</form>

<h:form>
  <h:commandLink value="click" action="index">
    <f:param id="id" name="id" value="20"/>
  </h:commandLink>
</h:form>

推荐答案

两种方式:

  1. 将 bean 请求设置为范围并将视图范围作为另一个@ManagedProperty 注入.

@ManagedBean
@RequestScoped
public class RequestBean {

    @ManagedProperty(value="#{param.id}")
    private Integer id;

    @ManagedProperty(value="#{viewBean}")
    private ViewBean viewBean;
}

视图作用域 bean 在 @PostConstruct 和请求作用域 bean 的操作方法期间可用.你只需要记住 id 在你不带参数回发到同一个视图时可能会丢失.

The view scoped bean is available during @PostConstruct and action methods of request scoped bean. You only need to keep in mind that the id can get lost when you do a postback to the same view without the parameter.

或者,在 bean 初始化期间从请求参数映射中手动获取它.

Or, grab it manually from the request parameter map during bean's initialization.

@ManagedBean
@ViewScoped
public class ViewBean {

    private Integer id;

    @PostConstruct
    public void init() {
        id = Integer.valueOf(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id"));       
    }
}

这样初始 id 在整个视图范围内都是可用的.

This way the initial id is available during the entire view scope.

这篇关于@ManagedProperty(value = "#{param.id}") 在非请求作用域 Bean 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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