非请求范围Bean中的@ManagedProperty(value ="#{param.id}") [英] @ManagedProperty(value = "#{param.id}") in a non-request Scope Bean

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

问题描述

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

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

Bean的范围是ViewScope



<我最终得到了这个错误:


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


我该怎么办?



arjan看看:



我的页面:




Facelet标题

 < form method =postaction =faces / index.xhtml> 
< input name =idvalue =4/>
< input type =submitvalue =submit/>
< / form>

< h:form>
< h:commandLink value =clickaction =index>
< f:param id =idname =idvalue =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 <期间可用/ code>和请求范围bean的操作方法。您只需要记住,在没有参数的情况下回发到同一视图时, id 可能会丢失。


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

      @ManagedBean 
    @ViewScoped
    公共类ViewBean {

    private Integer id;

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

    这样初始 id 在整个视图范围内可用。



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

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

And the scope of the Bean is ViewScope

I end up with this error:

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

What can I do?

arjan take a look:

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>

解决方案

Two ways:

  1. Make the bean request scoped and inject the view scoped one as another @ManagedProperty.

    @ManagedBean
    @RequestScoped
    public class RequestBean {
    
        @ManagedProperty(value="#{param.id}")
        private Integer id;
    
        @ManagedProperty(value="#{viewBean}")
        private ViewBean viewBean;
    }
    

    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.

  2. 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"));       
        }
    }
    

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

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

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