How do I process GET query string URL parameters in backing bean on page load?

后端 未结 3 544
一向
一向 2020-11-22 15:26

I\'ve read how to send parameters using JSF but what if the user types their companyId in the URL when accessing their login page? For example,

相关标签:
3条回答
  • 2020-11-22 15:49

    url paramters can also be treated as request parameters so you can also access through

    FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap()
    
    0 讨论(0)
  • 2020-11-22 15:56

    There is a utility library, OmniFaces which does this out of the box.

    @Inject @Param
    private String key;
    
    @Inject @Param
    private Long id;
    
    0 讨论(0)
  • 2020-11-22 15:59

    Yes, you can use the <f:viewParam> to set a request parameter as a managed bean property.

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

    You can if necessary invoke a bean action using <f:viewAction> (JSF 2.2+ only) or <f:event type="preRenderView">.

    <f:metadata>
        <f:viewParam name="companyId" value="#{bean.companyId}" />
        <f:viewAction action="#{bean.onload}" />
    </f:metadata>
    

    When using <f:viewAction> you can even return a navigation outcome.

    public String onload() {
        // ...
    
        return "somepage";
    }
    

    When not on JSF 2.2 yet, you can use ExternalContext#redirect() for that. See also among others How to perform navigation in preRenderView listener method.

    Note that this is not specific to PrimeFaces. It's just part of standard JSF. PrimeFaces is merely a component library which provides enhanced ajax and skinnability support.

    See also:

    • What can <f:metadata>, <f:viewParam> and <f:viewAction> be used for?
    • Communication in JSF 2.0 - Processing GET request parameters
    • @ManagedProperty with request parameter not set in a @Named bean
    0 讨论(0)
提交回复
热议问题