Struts 2会话属性不在jsp中显示

Ash*_*h S 2 session attributes struts2

我有一个Login页面的动作类,我在其中设置了一个session属性:

ActionContext context = ActionContext.getContext();
context.getSession().put(username, getUsername());
Run Code Online (Sandbox Code Playgroud)

我可以通过Action类访问session属性:

System.out.println("Username from session: " + context.getSession().get(username).toString());
Run Code Online (Sandbox Code Playgroud)

但是当我尝试通过随后显示的jsp访问相同的属性时,它不会显示任何内容:

Welcome <s:property value="#session['username']" />
Run Code Online (Sandbox Code Playgroud)

只显示:

欢迎

请帮助您更新会话属性的正确语法.

lsc*_*hin 10

在LoginAction

ActionContext.getContext().getSession().put("username", getUsername());
Run Code Online (Sandbox Code Playgroud)


获取用户名:

1)OtherAction

ActionContext.getContext().getSession().get("username");
Run Code Online (Sandbox Code Playgroud)

2).jsp

<s:property value="#session['username']" />
Run Code Online (Sandbox Code Playgroud)

 要么

${username}
Run Code Online (Sandbox Code Playgroud)

实现SessionAware是更优选的.

另请参阅:
1.我们如何访问会话
2. 如何使会话无效