【发布时间】:2011-11-30 11:49:05
【问题描述】:
我正在以这种方式创建 HttpSession 容器:
@SessionScoped
@ManagedBean(name="userManager")
public class UserManager extends Tools
{
/* [private variables] */
...
public String login()
{
/* [find user] */
...
FacesContext context = FacesContext.getCurrentInstance();
session = (HttpSession) context.getExternalContext().getSession(true);
session.setAttribute("id", user.getID());
session.setAttribute("username", user.getName());
...
System.out.println("Session id: " + session.getId());
我有 SessionListener,它应该给我有关创建会话的信息:
@WebListener
public class SessionListener implements HttpSessionListener
{
@Override
public void sessionCreated(HttpSessionEvent event) {
HttpSession session = event.getSession();
System.out.println("Session id: " + session.getId());
System.out.println("New session: " + session.isNew());
...
}
}
如何获得username 属性?
如果我使用System.out.println("User name: " + session.getAttribute("username")) 尝试它,它会抛出java.lang.NullPointerException..
【问题讨论】:
-
所以 session 或 System.out 为空
-
在 JSF 代码中使用原始 Servlet API 几乎在所有情况下都是代码异味。在这种特殊情况下,为什么不只使用会话范围的托管 bean?
-
我不明白这个问题...?带有
FacesContext的第一部分代码在会话范围的托管bean 中。我要编辑我的帖子。你能告诉我更多吗?
标签: java jsf jakarta-ee listener