FacesContext仅在第一次丢失其上下文值

时间:2013-02-15 14:18:18

标签: jsf facelets jsf-1.2

我有一个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>

要添加到例外列表<rich:tabPanel selectedTab="#{someBean.getSelectedTab}">

上面的

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();          
 }
}

1 个答案:

答案 0 :(得分:0)

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