@ManagedProperty with request parameter not set in a @Named bean

前端 未结 1 414
时光说笑
时光说笑 2021-01-05 11:20

I\'ve a CDI managed bean wherein I\'d like to set request parameters as managed properties:

import javax.inject.Named;
import javax.enterprise.context.Reques         


        
相关标签:
1条回答
  • 2021-01-05 12:12

    The JSF-specific @ManagedProperty annotation works only in JSF managed beans, not in CDI managed beans. In other words, it works only in classes annotated with JSF-specific @ManagedBean annotation, not in classes annotated with CDI-specific @Named annotation.

    CDI does not offer an annotation out the box to inject specifically a HTTP request parameter. JSF utility library OmniFaces offers a @Param annotation for the very purpose of injecting a HTTP request parameter in a CDI managed bean.

    @Inject @Param
    private String key;
    
    @Inject @Param
    private Long id;
    

    Alternatively, use the <f:viewParam> tag in the view.

    <f:metadata>
        <f:viewParam name="key" value="#{bean.key}" />
        <f:viewParam name="id" value="#{bean.id}" />
    </f:metadata>
    

    See also

    • ViewParam vs @ManagedProperty(value = "#{param.id}")
    • How do I process GET query string URL parameters in backing bean on page load?
    0 讨论(0)
提交回复
热议问题