开发者

Struts 2 session attribute not displaying in jsp

I have an action class for a Login page where I开发者_如何学编程 am setting a session attribute thus:

ActionContext context = ActionContext.getContext();
context.getSession().put(username, getUsername());

I am able to access the session attribute through the Action class thus:

System.out.println("Username from session: " + context.getSession().get(username).toString());

But when I try to access the same attribute through a subsequently displayed jsp, it does not display anything:

Welcome <s:property value="#session['username']" />

just displays:

Welcome

Please help as to the correct syntax for retriving session attributes.


LoginAction

ActionContext.getContext().getSession().put("username", getUsername());


Get username at :

1) OtherAction

ActionContext.getContext().getSession().get("username");

2) .jsp

<s:property value="#session['username']" />

 or

${username}

Implementing SessionAware is more preferred.

See also :
1. How do we get access to the session
2. How do we get invalidate the session


Implementing SessionAware is more preferred.

This certainly makes things easier. It allows you to manipulate the session in your struts action class as a map structure.

From the jsp page I generally access my session objects like the following:

<s:property value="%{#session.username}" />

or for complex objects in session, like a user object with name and password for instance:

<s:property value="%{#session.user.username}" />
<s:property value="%{#session.user.password}" />
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜