我需要自定义“在ExternalContext中找不到资源”页面。我已尝试更改web.xml以添加404和500 error-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/
How to create custom 404 messages in JSF 2.0?
但没有成功:
我得到的只是一个空白页面(或“这个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)
编辑和修改
在某些博客/论坛中,过滤器链接示例如下:
try {
chain.doFilter(request, response);
} catch (Throwable t) { // or Exception
// do some logging
}
这是错误的,并阻止JSF继续正常的错误处理流程。我保留了日志,但重新抛出异常。之后,error-code
中web.xml
500负责处理此问题。
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
}
答案 0 :(得分:2)
这是Facelets特有的“功能”。丢失的资源导致FacesFileNotFoundException
(FileNotFoundException
的子类)被抛出,而不是以404状态返回响应。
为了解决这个问题,请将以下内容添加到web.xml
:
<error-page>
<exception-type>java.io.FileNotFoundException</exception-type>
<location>/404.xhtml</location>
</error-page>
或者,您可以使用OmniFaces'FacesExceptionFilter
透明地将这些例外委托给404.
至于第二个问题:
我得到的只是一个空白页面(或“此XML文件似乎没有与之关联的任何样式信息。
错误页面<location>
也必须与FacesServlet
的网址格式相匹配。更好的方法是直接在FacesServlet
上映射*.xhtml
,这样您就不必担心虚拟网址。
答案 1 :(得分:0)
在某些博客/论坛中,我盲目复制的过滤器链接示例如下:
try {
chain.doFilter(request, response);
} catch (Throwable t) { // or Exception
// do some logging
}
这是错误的,并阻止JSF继续正常的错误处理流程。我保留了日志,但重新抛出异常。之后,error-code
中web.xml
500负责处理此问题。
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
}