停用Jetty的默认404错误处理程序 [英] Deactivate Jetty's default 404 error handler
问题描述
我想在Spring 3.1 Web应用程序中提供一个自定义的404错误页面,但是我无法停用Jetty 8的默认404错误页面.
I want to provide a custom 404 error page in my Spring 3.1 web application, but I cannot deactivate Jetty 8's default 404 error page.
Jetty 8开箱即用,提供默认的404错误页面:
当访问Jetty托管的网站并提供任何Servlet无法处理的URL路径时(例如,通过访问http://www.example.com/nonexisting
),Jetty会使用自己的默认HTML错误页面
Jetty 8, out of the box, provides a default 404 error page:
when visiting a Jetty-hosted website, and providing a URL path that is not handled by any servlet (e.g. by visiting http://www.example.com/nonexisting
), Jetty responses with its own default HTML error page:
HTTP ERROR 404
Problem accessing /nonexisting. Reason:
Not Found
Powered by Jetty://
要替换此默认行为,
- 我已从Jetty XML文件中删除了
DefaultHandler
, - 我已经编辑了
web.xml
以包括指向/error
的Servlet 2.5和Servlet 3.0错误处理程序位置, - 我已经设置了专用的
@Controller
来处理对/error
的请求,
- I've removed the
DefaultHandler
from my Jetty XML file, - I've edited my
web.xml
to include both Servlet 2.5 and Servlet 3.0 error handler locations pointing to/error
, - I've set up a dedicated
@Controller
for handling the request to/error
,
,但是我的网站仍然返回 Jetty自己的默认HTML错误页面.
but my website still returns Jetty's own default HTML error page.
Jetty 8的官方文档讨论了如何设置自定义错误页面" ,但是那里的建议说
- 配置一个自定义的Jetty错误处理程序(我不想这样做,我想如上所述在我自己的Spring
@Controller
内部执行此操作) - 创建捕获所有上下文并创建映射到
/
URI的根" Web应用程序". (我不想这样做,因为在我的web.xml
内部,我已经将Spring MVC的DispatcherServlet
映射到了/.
- to configure a custom Jetty error handler (I don't want to do this, I want to do it inside my own Spring
@Controller
as mentioned above) - to create a "catch all context and create a "root" web app mapped to the
/
URI." (I don't want to do this as inside myweb.xml
I have already mapped Spring MVC'sDispatcherServlet
to /.
如何关闭Jetty的默认错误处理程序,并按照上述指示进行错误处理?
How can I turn off Jetty's default error handler and have the error handling be done as pointed out above?
推荐答案
我的问题的解决方案是添加自定义org.eclipse.jetty.server.handler.ErrorHandler
.
The solution to my problem was to add a custom org.eclipse.jetty.server.handler.ErrorHandler
.
如果用户未明确指定某些ErrorHandler
,则Jetty服务器实例似乎注册了默认的ErrorHandler
.
If a user doesn't explicitly specify some ErrorHandler
, the Jetty server instance seems to register a default ErrorHandler
.
如 http://www.eclipse.org/jetty/documentation/所述current/custom-error-pages.html ,要注册自定义ErrorHandler
,您可以按照以下步骤操作.
As outlined on http://www.eclipse.org/jetty/documentation/current/custom-error-pages.html, to register a custom ErrorHandler
, you can follow the following steps.
- 实现某些
org.eclipse.jetty.server.handler.ErrorHandler
子类,例如com.example.CustomErrorHandler
. - 将此子类提供给您的Eclipse服务器实例,例如通过将
CustomErrorHandler
捆绑到jar
文件中,然后将该jar
文件复制到${jetty.base}/lib/ext
目录中. - 配置您的Jetty服务器实例以将此自定义
ErrorHandler
注册为Bean:
- Implement some
org.eclipse.jetty.server.handler.ErrorHandler
subclass, e.g.com.example.CustomErrorHandler
. - Make this subclass available to your Eclipse server instance, e.g. by bundling
CustomErrorHandler
in ajar
file and then copying thatjar
file into the${jetty.base}/lib/ext
directory. - Configure your Jetty server instance to have this custom
ErrorHandler
registered as a bean:
文件jetty.xml
:
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN"
"http://www.eclipse.org/jetty/configure_9_0.dtd">
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<!-- more configuration -->
<Call name="addBean">
<Arg>
<New class="com.example.CustomErrorHandler">
<Set name="server"><Ref refid="Server" /></Set>
</New>
</Arg>
</Call>
</Configure>
这篇关于停用Jetty的默认404错误处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!