0

我需要自定义“在 ExternalContext 中找不到作为资源”页面。我已尝试更改 web.xml 以添加 404 和 500error-code部分:

<error-page>
  <error-code>500</error-code>
  <location>/500.xhtml</location>
</error-page>
<error-page>
  <error-code>404</error-code>
  <location>/404.xhtml</location>
</error-page>

并遵循 SO/forums/blog 提供的信息,例如
http://duckranger.com/2010/08/configure-a-custom-404-page-in-glassfish/
如何在 JSF 2.0 中创建自定义 404 消息?

但没有成功:
我得到的只是一个空白页面(或“此 XML 文件似乎没有任何与之关联的样式信息。文档树如下所示。”浏览器生成)和以下堆栈跟踪一个不存在的页面:

SEVERE: doFilter
/m/test.xhtml Not Found in ExternalContext as a Resource
  com.sun.faces.context.FacesFileNotFoundException: /m/test.xhtml Not Found in ExternalContext as a Resource
at com.sun.faces.facelets.impl.DefaultFaceletFactory.resolveURL(DefaultFaceletFactory.java:232)
at com.sun.faces.facelets.impl.DefaultFaceletFactory.resolveURL(DefaultFaceletFactory.java:273)
at com.sun.faces.facelets.impl.DefaultFaceletFactory.getMetadataFacelet(DefaultFaceletFactory.java:209)
at com.sun.faces.application.view.ViewMetadataImpl.createMetadataView(ViewMetadataImpl.java:114)
at com.sun.faces.lifecycle.RestoreViewPhase.execute(RestoreViewPhase.java:233)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.RestoreViewPhase.doPhase(RestoreViewPhase.java:116)
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 com.example.v2.filter.AbstractFilter.doFilter(AbstractFilter.java:55)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
at com.example.v2.filter.AbstractFilter.doFilter(AbstractFilter.java:55)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
at com.example.v2.filter.AbstractFilter.doFilter(AbstractFilter.java:55)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
at com.example.v2.filter.AbstractFilter.doFilter(AbstractFilter.java:55)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
at com.example.v2.filter.MobileFilter.skip(MobileFilter.java:111)
at com.example.v2.filter.MobileFilter.doFilter(MobileFilter.java:67)
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:174)
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)

EDIT and FIX
In some blog/forum, the Filter chaining examples are as follows:

try {
  chain.doFilter(request, response);
  } catch (Throwable t) { // or Exception
    // do some logging
  }

This is wrong and prevents JSF to continue the normal flow of error processing. I kept the logging but re-throw the Exception. After that the error-code 500 in web.xml takes care of the issue.

try {
  chain.doFilter(request, response);
  } catch (Throwable t) { // or Exception
    // do some logging
    throw t; // so JSF/Container can continue normal low of error handling
  }
4

2 回答 2

2

This is a Facelets-specific "feature". A missing resource results in a FacesFileNotFoundException (which is a subclass of FileNotFoundException) being thrown instead of that the response is been returned with a 404 status.

To cover that, add the following to web.xml as well:

<error-page>
    <exception-type>java.io.FileNotFoundException</exception-type>
    <location>/404.xhtml</location>
</error-page>

Alternatively, you could use OmniFaces' FacesExceptionFilter which transparently delegates those exceptions to 404's.

As to the second problem:

All I get is a blank page (or a "This XML file does not appear to have any style information associated with it.

The error page <location> has to match FacesServlet's URL pattern as well. Better is to just map the FacesServlet on *.xhtml directly, this way you never need to worry about virtual URLs.

于 2012-10-31T17:23:48.377 回答
0

In some blog/forum, the Filter chaining examples I had blindly copied are as follows:

try {
  chain.doFilter(request, response);
  } catch (Throwable t) { // or Exception
    // do some logging
  }

This is wrong and prevents JSF to continue the normal flow of error processing. I kept the logging but re-throw the Exception. After that the error-code 500 in web.xml takes care of the issue.

try {
  chain.doFilter(request, response);
  } catch (Throwable t) { // or Exception
    // do some logging
    throw t; // so JSF/Container can continue normal low of error handling
  }
于 2012-11-01T04:16:07.583 回答