Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 996
Big Al Avatar asked May 23 '12 16:05

Big Al


People also ask

How do you pass string parameters in GET request URL?

Any word after the question mark (?) in a URL is considered to be a parameter which can hold values. The value for the corresponding parameter is given after the symbol "equals" (=). Multiple parameters can be passed through the URL by separating them with multiple "&".

What are query string parameters in URL?

What are query string parameters? Query string parameters are extensions of a website's base Uniform Resource Locator (URL) loaded by a web browser or client application. Originally query strings were used to record the content of an HTML form or web form on a given page.

How can I tell if a URL has a query string?

To check if a url has query parameters, call the includes() method on the string, passing it a question mark as a parameter, e.g. str. includes('? ') . If the url contains query parameters, the includes method will return true , otherwise false is returned.


4 Answers

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
like image 129
BalusC Avatar answered Oct 08 '22 18:10

BalusC


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

FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap()
like image 26
user1415890 Avatar answered Oct 08 '22 17:10

user1415890


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

@Inject @Param
private String key;

@Inject @Param
private Long id;
like image 7
Toumi Avatar answered Oct 08 '22 16:10

Toumi


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

like image 1
ogarzonm Avatar answered Oct 08 '22 16:10

ogarzonm