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]
# 1

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>

BalusCa at 2007-7-14 18:50:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

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?

fabdiba at 2007-7-14 18:50:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
Use getRequestParameterMap(). Also see http://balusc.xs4all.nl/srv/dev-jep-com.html
BalusCa at 2007-7-14 18:50:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

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?

rzannera at 2007-7-14 18:50:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
Thanks a lot bulusC i've solved my problem i've read the solution on the link you have poste me.Thanks for taking time to help me. I really appreciate it.Bye, Fabio.
fabdiba at 2007-7-14 18:50:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...