Get Request and Session Parameters and Attributes from JSF pages
Asked Answered
E

8

56

I'm using JSF with facelets and I need to get the request and session parameters inside the JSF page. In JSP pages I got this parameter like that: "${requestScope.paramName}" or "${sessionScope.paramName}". But now after using JSF there are only beans and you can't get any value except bean attributes.

NOTE: The session attributes what I need is auto filled using acegi security so I can't get any access to them.

So what to do now?

Eyeopening answered 15/2, 2009 at 7:7 Comment(0)
E
94

You can get a request parameter id using the expression:

<h:outputText value="#{param['id']}" />
  • param—An immutable Map of the request parameters for this request, keyed by parameter name. Only the first value for each parameter name is included.
  • sessionScope—A Map of the session attributes for this request, keyed by attribute name.

Section 5.3.1.2 of the JSF 1.0 specification defines the objects that must be resolved by the variable resolver.

Eventuality answered 15/2, 2009 at 11:7 Comment(0)
D
47

You can also use a bean (request scoped is suggested) and directly access the context by way of the FacesContext.

You can get the HttpServletRequest and HttpServletResposne objects by using the following code:

HttpServletRequest req = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
HttpServletResponse res = (HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();

After this, you can access individual parameters via getParameter(paramName) or access the full map via getParameterMap() req object

The reason I suggest a request scoped bean is that you can use these during initialization (worst case scenario being the constructor. Most frameworks give you some place to do code at bean initialization time) and they will be done as your request comes in.

It is, however, a bit of a hack. ;) You may want to look into seeing if there is a JSF Acegi module that will allow you to get access to the variables you need.

Disproportionation answered 17/2, 2009 at 20:1 Comment(2)
+1, but who in the world, came up with such a ridiculous API for getting a single parameter?Mythologize
@GDemecki: Noone... did, maybe the php people switching to JSF. #28780551Industrious
G
36

You can either use

<h:outputText value="#{param['id']}" /> or

<h:outputText value="#{request.getParameter('id')}" />

However if you want to pass the parameters to your backing beans, using f:viewParam is probably what you want. "A view parameter is a mapping between a query string parameter and a model value."

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

This will set the id param of the GET parameter to the blog bean's entryId field. See http://java.dzone.com/articles/bookmarkability-jsf-2 for the details.

Gastrovascular answered 15/11, 2011 at 9:28 Comment(0)
N
14

You can like this:

#{requestScope["paramName"]} ,#{sessionScope["paramName"]}

Because requestScope or sessionScope is a Map object.

Neona answered 12/11, 2012 at 8:34 Comment(0)
I
6

You can also use a tool like OcpSoft's PrettyFaces to inject dynamic parameter values directly into JSF Beans.

Incommodity answered 8/2, 2010 at 16:40 Comment(0)
B
5

Assuming that you already put your object as attribute on the session map of the current instance of the FacesContext from your managed-bean, you can get it from the JSF page by :

<h:outputText value="#{sessionScope['yourObject'] }" />

If your object has a property, get it by:

<h:ouputText value="#{sessionScope['yourObject'].anyProperty }" />
Bobble answered 17/7, 2013 at 12:3 Comment(2)
How can i put session object as an attribute on the session map of the current instance of the FacesContext from my managed-bean ???Dogma
#4495904Bobble
U
4

Are you sure you can't get access to request / session scope variables from a JSF page?

This is what I'm doing in our login page, using Spring Security:

<h:outputText
    rendered="#{param.loginFailed == 1 and SPRING_SECURITY_LAST_EXCEPTION != null}">
    <span class="msg-error">#{SPRING_SECURITY_LAST_EXCEPTION.message}</span>
</h:outputText>
Unchain answered 18/2, 2009 at 9:32 Comment(0)
Q
0

In the bean you can use session.getAttribute("attributeName");

Quinquevalent answered 19/3, 2019 at 13:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.