【问题标题】:FacesContext loosing its context value only for first timeFacesContext 仅第一次丢失其上下文值
【发布时间】:2013-02-15 14:18:17
【问题描述】:

我有一个 Facelets 页面,其中有一个 h:commandLink

<h:commandLink value="#{View} &#187;" 
    onclick="return myJavaScript('#{myBean.param1}');">
</h:commandLink>

function myJavaScript(paramReceived)
{
   var relPath = "/myWarName/Pages/myReceiptPage.jsf?"
                    + myActionValue;
    billWindow = window.open(relPath, '', 'width=' + width + ',height='
                    + height + ',top=' + top + ',left=' + left
                    + ',scrollbars=1,resizable=1');
    billWindow.focus();
}

下面是我的myReceiptPage.xhtml页面代码,

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<div xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://java.sun.com/jsf/core">
    <head>
    </head>
      <f:view>
          <body>
             <h:form>
               <ui:insert>
                           <ui:include src="/someOtherPage.xhtml" />
               </ui:insert>

                <h:outputText value="#{facesContext.externalContext.requestParameterMap.UserID}" /> 

                          </h:form>
                     <body>
             </f:view>
      <div>

当我单击 h:commandLink 时,Java 脚本被调用并打开一个窗口,但仅第一次出现以下异常。从第二次开始,当我再次单击 h:commandLink 时,将打开没有异常的窗口并且数据已正确填充。

当我点击h:commandLink PhaseListener 被调用时,它正在调用一种方法。下面是代码,

public void getUserId()
    {
        try
        {
            FacesContext context = FacesContext.getCurrentInstance(); 
            ExternalContext externalContext = context.getExternalContext();
                    //Code continues
        }
            Catch(Exception E)
            {
              //Am getting to know the exception here
             }
      }

以下是异常(显示在窗口上),

javax.faces.FacesException: javax.el.ELException: /view.xhtml @192,150 userID="#{myBean.userID}": Error reading 'userID' on type com.myBean
        at javax.faces.component.UIOutput.getValue(UIOutput.java:187)
        at org.richfaces.component.UITabPanel.getValue(UITabPanel.java:103)
        at org.richfaces.renderkit.TabPanelRendererBase.encodeTabs(TabPanelRendererBase.java:309)
        at org.richfaces.renderkit.html.TabPanelRenderer.doEncodeBegin(TabPanelRenderer.java:228)
        at org.richfaces.renderkit.html.TabPanelRenderer.doEncodeBegin(TabPanelRenderer.java:180)
        at org.ajax4jsf.renderkit.RendererBase.encodeBegin(RendererBase.java:100)
        at javax.faces.component.UIComponentBase.encodeBegin(UIComponentBase.java:813)
        at javax.faces.component.UIComponent.encodeAll(UIComponent.java:928)
        at javax.faces.render.Renderer.encodeChildren(Renderer.java:148)
        at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:837)
        at javax.faces.component.UIComponent.encodeAll(UIComponent.java:930)
        at javax.faces.component.UIComponent.encodeAll(UIComponent.java:933)
        at com.sun.facelets.FaceletViewHandler.renderView(FaceletViewHandler.java:592)
        at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:189)
        at org.jboss.portletbridge.application.PortletViewHandler.renderView(PortletViewHandler.java:247)
        at org.ajax4jsf.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:100)

这可能是什么原因?

编辑view.xhtml 的内容粘贴在下方。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:a4j="http://richfaces.org/a4j"
    xmlns:rich="http://richfaces.org/rich"
    xmlns:t="http://myfaces.apache.org/tomahawk">
<head>
</head>
<h:form id="myFormId">
<ui:insert>
    <ui:include src="/myWarName/Pages/someOtherPage.xhtml" />  //Guess, problem is with this include. 
//This include is also there in myReceiptPage.xhtml page.
</ui:insert>

<rich:tabPanel selectedTab="#{myBean.getSelectedTab}">
    <rich:tab name="Tab Name">
        <h:panelGroup>
            <ui:insert>
               <ui:include src="somePage1.xhtml" />
            </ui:insert>
        </h:panelGroup>
        <h:panelGroup>
            <ui:insert>
                <ui:include src="somePage2.xhtml" />
            </ui:insert>
        </h:panelGroup>
</rich:tab>
</h:form
</html>

添加到例外列表&lt;rich:tabPanel selectedTab="#{someBean.getSelectedTab}"&gt;

getSelectedTab上面的方法是这样的,

public class myBean implements Serializable{
 public String getSelectedSubTab()
 {
   FacesContext con=FacesContext.getCurrentInstance();  
   ExternalContext externalContext = con.getExternalContext();
   String remoteUserId = externalContext.getRemoteUser();
   User user = UserServiceUtil.getUserById(Long.parseLong(remoteUserId));
   this.userID = user.getScreenName();          
 }
}

【问题讨论】:

  • 这是整个堆栈跟踪吗?看起来有一个潜在的原因导致 EL 窒息。
  • 好吧,我们在这里看不到view.xhtml,而这里的地方是有问题的EL。
  • @kolossus 除了那个异常,我也得到了 NullPointerException。这已在我的问题中更新。
  • @partlov 也包含了view.xhtml 代码。在我怀疑有问题的地方发表了评论
  • @VikasV FacesContext.getCurrentInstance() 通常会在您在非托管 bean 中进行调用时返回 null。 someBean 的声明和范围是否正确?

标签: jsf facelets jsf-1.2


【解决方案1】:

我必须说我仍然没有在任何xhtml文件中看到表达式#{myBean.userID},但问题可能是因为你的getter方法getUserId()被声明为void,所以这里的属性userId被认为是非可读。您必须从 getter 返回属性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-27
    • 2011-04-14
    • 2021-09-26
    • 2019-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多