简体   繁体   中英

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 (eg, #{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.

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, sessions, shared 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:

A request scoped variable is instancinated for a each single request. So a request scoped variable instance does not exist in the context of another request.

A request scoped variable is, as the name implies, only valid in the current http request. A good use for request variables is when forwarding a request from a servlet to jsp. Eg set the variable in servlet, later read the same variable in jsp with ${myvar}

example(servlet)

request.setAttribute ("greeting", "world");        
getServletConfig().getServletContext().getRequestDispatcher("/jsp/page.jsp").forward(request, response);

example(jsp)

Hello ${greeting}

id is mapped with entryId in Managed-bean config or u can use annotations.

<managed-bean>
   <managed-bean-name>blog</managed-bean-name>
   <managed-bean-class>com.acme.Blog</managed-bean-class>
   <managed-property>
     <property-name>entryId</property-name>
      <value>#{param['id']}</value>
   </managed-property>
</managed-bean>

@RequestScope: (this is the default scope of a managed bean). This puts the bean in request scope. It makes a new instance for every HTTP request. Commonly, the bean is instantiated twice, once when form is displayed and once when it is submitted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM