externalContext.getSession(false)返回null [英] externalContext.getSession(false) returns null
问题描述
我当前的平台是NB 7 rc 1,我有一个只有一个托管bean"的JSF 2应用程序.及时地,我正在使用Tomcat 7.0.34.
My current platform is NB 7 rc 1 and I have a JSF 2 app with just one "managed bean". In time, I'm using Tomcat 7.0.34.
以下是发生错误的代码:
Here is the code where the error occurs:
@ManagedBean
@SessionScoped
public class CopyController implements Serializable {
private static final long serialVersionUID = 1L;
private String pathBancoSentencas;
private List<Arquivo> arquivosUpload;
private HttpSession session;
private List<String> listaPdfs;
public List<Arquivo> getArquivosUpload() {
return arquivosUpload;
}
public void setArquivosUpload(List<Arquivo> arquivosUpload) {
this.arquivosUpload = arquivosUpload;
}
public CopyController() {
arquivosUpload = new ArrayList<Arquivo>();
}
@PostConstruct
public void doInit() {
session = (HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(false);
pathBancoSentencas = (String)session.getAttribute("DIRETORIO_TRABALHO");
}
处理完请求后,例程将调用如下视图:
And after processing the request, the routine calls a view like this:
<p:dataTable value="#{copyController.arquivosUpload}" var="arquivo" paginator="true" paginatorPosition="bottom"
paginatorTemplate="{FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink}">
<f:facet name="header">
Item processado
</f:facet>
<h:column>
<h:outputText value="#{arquivo.nome}" />
</h:column>
</p:dataTable>
但是,该视图未显示,并且发生此错误:
However, the view is not displayed and this error occurs:
Caused by: java.lang.NullPointerException
at br.jus.tjmg.dspace.copy.CopyController.doInit(CopyController.java:54)
... 70 more
有人可以帮助我吗? 谢谢!
Can someone help me? Thanks!
推荐答案
从您的doInit()
方法中,
getExternalContext().getSession(false);
如果当前尚未创建会话,则返回null
.但是,您显然希望在下一行中进行非null
会话.
This returns null
if the session is currently not been created yet. But yet you're explicitly expecting a non-null
session in the next line.
您需要传递true
来触发自动创建.
You need to pass true
to trigger autocreate.
getExternalContext().getSession(true);
另请参见 javadoc (重点是我的):
See also the javadoc (emphasis mine):
如果
create
参数为true
,则创建(如有必要)并返回与当前请求关联的会话实例.如果create
参数为false
,则返回与当前请求关联的任何现有会话实例,如果没有这样的会话,则返回或返回null
.
If the
create
parameter istrue
, create (if necessary) and return a session instance associated with the current request. If thecreate
parameter isfalse
return any existing session instance associated with the current request, or returnnull
if there is no such session.
无关与具体问题无关,整个doInit()
方法是不必要的,可以用@ManagedProperty
代替:
Unrelated to the concrete problem, that whole doInit()
method is unnecessary and can be replaced by a @ManagedProperty
:
@ManagedProperty("#{DIRETORIO_TRABALHO}")
private String pathBancoSentencas;
或者,如果您完全肯定需要在doInit()
中执行此操作,则更好的方法是从ExternalContext#getSessionMap()
中获取它.
Or, if you're absolutely positive that you need do to it in the doInit()
, a nicer way is to get it from ExternalContext#getSessionMap()
instead.
pathBancoSentencas = (String) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("DIRETORIO_TRABALHO");
您应该尝试避免 javax.servlet.*
导入您的JSF支持bean.
You should try to avoid javax.servlet.*
imports in your JSF backing bean.
这篇关于externalContext.getSession(false)返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!