JSF get a session attribute in back bean
Hi to everyone,
I've a jsf page, in this page i'm setting a session attribute with this code:
<%request.setAttribute("user", "admin"); %>
i've a submit button with action like this:
<h:commandButton styleClass="aButton" value="Aggiorna"
style="vertical-align:middle;" action="#{UserBean.update}"/>
In the method updatei want retrive the attribute "user" so i've write this code in update function:
FacesContext ctx = FacesContext.getCurrentInstance();
HttpServletRequest request = (HttpServletRequest)ctx.getExternalContext().getRequest();
//LoginBean lb = (LoginBean) app.createValueBinding("#{login}).getValue(ctx);
user = (String)request.getAttribute("user");
but the user is null, why?
Can someone help me?
Thanks, Fabio.
[830 byte] By [
fabdiba] at [2007-10-3 1:52:32]

What's the managed bean scope? Request or Session?
Anyway, it's better to use f:param to pass parameters to methods called by commandlink or commandbutton.
<h:commandButton action="#{myBean.action}">
<f:param name="user" value="admin" />
</h:commandButton>
Hi balusC and thanxs for your answer,
i'm tryng your solution, i'm write in the jsf page:
<h:commandButton styleClass="aButton" value="Aggiorna"
style="vertical-align:middle;" action="# {userBean.update}">
<f:param name="user" value="admin"/>
</h:commandButton>
in the update method i'm write:
user = (String) FacesContext.getCurrentInstance().getExternalContext().
getRequestMap().get("user");
but the value of user is always null ?
Another problem is the user value is inthe session on my application how i can set this value in value of f:param?
Hi,
> Hi to everyone,
> I've a jsf page, in this page i'm setting a session
> attribute with this code:
> <%request.setAttribute("user", "admin"); %>
this is NOT a session attribute but a REQUEST attribute. It is available only until the rendering of the JSP is finished. That's the reason why you don't have it any more in the next request going into your backing bean.
Use session.setAttribute("...", ...) instead.
> In the method updatei want retrive the attribute
> "user" so i've write this code in update function:
>
> FacesContext ctx =
> FacesContext.getCurrentInstance();
> HttpServletRequest request =
> (HttpServletRequest)ctx.getExternalContext().getReque
> t();
> //LoginBean lb = (LoginBean)
> app.createValueBinding("#{login}).getValue(ctx);
>user = (String)request.getAttribute("user");
To retrieve a SESSION attribute use request.getSession().getAttribute("...").
Hope that helps,
Ren?