auser auser - 1 year ago 110
Java Question

What is a "request scoped variable" in JSF

I was reading an article titled: "JSF 2 GETs Bookmarkable URLs".

The article has the following paragraph:

Introducing view parameters

The API documentation describes a view parameter, represented by the javax.faces.component.UIViewParameter component class, as a declarative binding between a request parameter and a model property. The binding to the model property is expressed using an EL value expression (e.g., #{blog.entryId}). If the expression is omitted, the request parameter is bound instead to a request-scoped variable with the same name.

Could someone kindly provide an example of a request scoped variable.

Answer Source

A "request scoped variable" is an variable which is stored as an attribute of the HttpServletRequest.

request.setAttribute("foo", foo);

This attribute is available in EL the usual way by #{foo}. The HttpServletRequest itself has a lifetime of exactly one HTTP request-response cycle. Once the HTTP response associated with the HTTP request has arrived fully at the client (the webbrowser), then the HttpServletRequest instance, including all of its attributes, is garbaged. JSF request scoped managed beans are by the way also stored as an attribute of the HttpServletRequest.

As JSF runs "on top" of the basic HTTP Servlet API, this is all easier to understand if you also understand how HTTP and the Servlet API work. This answer might give some enlightenment: How do servlets work? Instantiation, session variables and multithreading.

Your subsequent question shall probably be "How is this related to the quoted paragraph then?" Well, it is basically telling that, when you omit the value attribute of the <f:viewParam>, that it instead is been put as a variable in the request scope.

So, if you use

<f:viewParam name="entryId" />

instead of

<f:viewParam name="entryId" value="#{blog.entryId}" />

then it becomes available as #{entryId} in the request scope.

<p>The entry ID view parameter is: #{entryId}</p>

This is however not the way view parameters are usually been used. You'd alternatively also just have used the #{param} map instead, hereby making the whole <f:viewParam> superfluous.

<p>The entry ID view parameter is: #{param.entryId}</p>

See also:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download