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

4

38

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,

http://my.company.url/productName/login.faces?companyId=acme.

The way we do it now, there is a bit of scriptlet code that grabs the value from the request and then set it in the session. That parameter changes their look and feel starting from the login page forward so each customer could have a different login page view. We are using extjs until I switch over to JSF.

Is there a way to do that using JSF 2 or perhaps PrimeFaces?

Cochleate answered 23/5, 2012 at 16:49 Comment(0)
O
63

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:

Officer answered 23/5, 2012 at 17:0 Comment(2)
I used to use PostConstruct for this type of things (and DB access). The problem I just found out is that on requestscoped beans, the method will be recalled on ajax req that have nothing to do with said bean. That is not the case with viewAction. For anyone wondering why : stackoverflow.com/questions/24490152/…Rangel
If i use <f:viewParam>, there is no need for context.getExternalContext().getRequestParameterMap() in the ManagedBean? "... you can even return a navigation outcome." Is it redirection?Nut
T
33

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

FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap()
Tarmac answered 24/5, 2012 at 19:51 Comment(1)
They are request parameters. The way as you proposed however allows less fine grained conversion/validation which may result in lot of unnecessary boilerplate code in backing bean. See also the "See also" links in the accepted answer.Officer
N
7

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

@Inject @Param
private String key;

@Inject @Param
private Long id;
Nysa answered 2/3, 2018 at 10:27 Comment(0)
K
1

You can use the request.getQueryString() if you want to get full query parameter string.

Kamilahkamillah answered 19/12, 2020 at 1:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.