【发布时间】:2012-03-31 02:49:27
【问题描述】:
我的 jsp 文件中有以下代码,其中 getSession 信息:
<jsp:useBean id="bookBean" class="beans.trade.BookBean" scope="session">
<% bookBean.setSession( request.getSession() ); %>
</jsp:useBean>
现在我正在尝试在页面上使用jsf,并在我的managedBean 中调用EJB 并获取他们的参考。
这是示例:
public void setSession(HttpSession session)
{
super.setSession(session);
InitialContext ic = getInitialContext();
booksLocalOps = ((BookOpsLocalHome) ic.lookup(BookOpsLocalHome.JNDI_NAME)).create();
books = booksLocalOps.findBooksByOrg("ORG");
}
现在我的xhtml 页面为:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:icecore="http://www.icefaces.org/icefaces/core"
xmlns:ice="http://www.icesoft.com/icefaces/component"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:ace="http://www.icefaces.org/icefaces/components"
xmlns:p="http://java.sun.com/jsf/core"
xmlns:ice-cc="http://www.icesoft.com/icefaces-composite-comps">
<h:head>
<title>bookTemplate</title>
<link rel="stylesheet" type="text/css" href="/xmlhttp/css/rime/rime.css"/>
</h:head>
<h:body>
<ice:form>
<p align="center">
<ice:outputText value="Book Template" style="text-align:center;font-size:40px;"></ice:outputText>
</p>
<br/>
<br/>
<p align="center">
<ice:panelGrid columns="2">
<ice:panelGrid>
<ice:outputText value="Book Name:" style="text-align:left;font-size:20px;"
id="bookName"></ice:outputText>
</ice:panelGrid>
<ice:panelGrid>
<ice:inputText id="BookNameInputText" style="width: 195px;"
value="#{bookBean.bookName}"></ice:inputText>
</ice:panelGrid>
</ice:panelGrid>
</p>
<br/>
<br/>
</ice:form>
</h:body>
</html>
所以我的问题是如何在该页面上获取session 信息?
更新
我正在调试应用程序,当我尝试获取 initialContext 时,我最终抛出了 javax.servlet.ServletException: Session cannot be null 异常,不知道如何处理这个问题。
【问题讨论】:
-
按照 BalusC 的建议,我使用了
FacesContext context = FacesContext.getCurrentInstance(); HttpSession session = (HttpSession) context.getExternalContext().getSession(true);并在我的 Bean 中获取了 HTTP 会话,并且能够获取会话中存在的 userDetails 信息。