2012-03-06 50 views
0

我想在我的應用程序中使用自定義ViewHandler,該應用程序使用JSF2,PrimeFaces 3.2.x構建,並部署在Glassfish AS v3.1.1上。一旦我在faces-config.xml中添加了我的自定義ViewHandler,第一個頁面就可以正常顯示,但是任何操作都會導致NPE。我修改了我的ViewHandler,所以它除了代理父級ViewHandler外什麼都沒做,但問題依然存在。使用自定義ViewHandler時引發NullPointerException

爲CustomViewHandler

import java.io.IOException; 
import java.util.Locale; 
import javax.faces.FacesException; 
import javax.faces.application.ViewHandler; 
import javax.faces.component.UIViewRoot; 
import javax.faces.context.FacesContext; 

public class CustomViewHandler extends ViewHandler { 
    private ViewHandler parent; 

    public CustomViewHandler(ViewHandler parent) { 
     this.parent = parent; 
    } 

    @Override 
    public UIViewRoot restoreView(FacesContext facesContext, String viewId) { 
     return parent.restoreView(facesContext, viewId); 
    } 

    @Override 
    public Locale calculateLocale(FacesContext facesContext) { 
     return parent.calculateLocale(facesContext); 
    } 

    @Override 
    public String calculateRenderKitId(FacesContext facesContext) { 
     return parent.calculateRenderKitId(facesContext); 
    } 

    @Override 
    public UIViewRoot createView(FacesContext facesContext, String viewId) { 
     return parent.createView(facesContext, viewId); 
    } 

    @Override 
    public String getActionURL(FacesContext facesContext, String actionId) { 
     return parent.getActionURL(facesContext, actionId); 
    } 

    @Override 
    public String getResourceURL(FacesContext facesContext, String resId) { 
     return parent.getResourceURL(facesContext, resId); 
    } 

    @Override 
    public void renderView(FacesContext facesContext, UIViewRoot viewId) throws IOException, FacesException { 
     parent.renderView(facesContext, viewId); 

    } 

    @Override 
    public void writeState(FacesContext facesContext) throws IOException { 
     parent.writeState(facesContext); 
    } 

    public ViewHandler getParent() { 
     return parent; 
    } 
} 

我faces-config.xml中的代碼

<application> 
    <view-handler>com.util.CustomViewHandler</view-handler> 
    <message-bundle>messages</message-bundle> 
    <locale-config> 
     <default-locale>en</default-locale> 
     <supported-locale>en</supported-locale> 
    </locale-config> 
    <resource-bundle> 
     <base-name>messages</base-name> 
     <var>msg</var> 
    </resource-bundle> 
</application> 

堆棧跟蹤

java.lang.NullPointerException 
at javax.faces.component.UIComponentBase.getRenderer(UIComponentBase.java:1402) 
at javax.faces.component.UIComponentBase.decode(UIComponentBase.java:785) 
at javax.faces.component.UIComponentBase.processDecodes(UIComponentBase.java:1181) 
at javax.faces.component.UIComponentBase.processDecodes(UIComponentBase.java:1176) 
at javax.faces.component.UIComponentBase.processDecodes(UIComponentBase.java:1176) 
at javax.faces.component.UIViewRoot.processDecodes(UIViewRoot.java:933) 
at com.sun.faces.lifecycle.ApplyRequestValuesPhase.execute(ApplyRequestValuesPhase.java:78) 
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) 
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) 
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593) 
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1539) 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:343) 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217) 
at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:79) 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256) 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217) 
at com.seer.filter.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:21) 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256) 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217) 
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279) 
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175) 
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655) 
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595) 
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98) 
at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91) 
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162) 
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:330) 
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231) 
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:232) 
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:828) 
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:725) 
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1019) 
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225) 
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137) 
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104) 
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90) 
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79) 
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54) 
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59) 
at com.sun.grizzly.ContextTask.run(ContextTask.java:71) 
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532) 
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513) 
at java.lang.Thread.run(Thread.java:722) 

感謝

+0

對glassfish 3.1.2有同樣的問題,也使用primefaces 3.4。 – djmj 2012-10-21 22:14:56

+0

我仍然沒有針對此問題的解決方案。自我發佈消息以來,我還沒有嘗試過這種方法。我現在不需要它,我只是在玩弄將來可能需要的功能。我沒有足夠的時間回到這裏玩更多的東西。抱歉。 – jjd 2012-10-28 20:23:38

回答

2

我有同樣的錯誤,但通過延長ViewHandlerWrapper而不是ViewHandler它解決了錯誤。

這裏是我的CustomViewHandler的例子(不是一個完整的例子,但我們的應用程序中建作爲概念證明):

import javax.faces.application.ViewExpiredException; 
import javax.faces.application.ViewHandler; 
import javax.faces.application.ViewHandlerWrapper; 
import javax.faces.component.UIViewRoot; 
import javax.faces.context.FacesContext; 

import org.apache.log4j.Logger; 

public class CustomViewHandler extends ViewHandlerWrapper { 

    private ViewHandler wrapped; 
    private static Logger LOGGER = Logger.getLogger(CustomViewHandler.class); 

    public CustomViewHandler(ViewHandler wrapped) { 
     LOGGER.info("CustomViewHandler.CustomViewHandler():wrapped View Handler:"+wrapped.getClass()); 
     this.wrapped = wrapped; 
    } 

    @Override 
    public UIViewRoot restoreView(FacesContext context, String viewId) { 
     UIViewRoot root; 
     try { 
      LOGGER.info("restoring view : " + viewId); 
      root = wrapped.restoreView(context, viewId); 
     } catch (ViewExpiredException e) { 
      LOGGER.error("View Expired : " + e.getMessage() + " -> recreating"); 
      root = wrapped.createView(context, viewId); 
     } 
     return root; 
    } 

    @Override 
    public ViewHandler getWrapped() { 
     return wrapped; 
    } 
} 

如果你想要一個更完整,功能自定義視圖處理程序,看看在OmniFacesRestorableViewHandler。源代碼在該頁面上可用。

從Java文檔

javax.faces.application.ViewHandlerWrapper

提供了一個簡單的ViewHandler實現,可以由希望提供專業化的行爲到現有的ViewHandler實例開發商被繼承。所有方法的默認實現是調用到包裝的ViewHandler。

用法:擴展此類並重寫getWrapped以返回要包裝的實例。

相關問題