Struts 2自定义404错误页面从不显示 [英] Struts 2 custom 404 error page never shows

查看:66
本文介绍了Struts 2自定义404错误页面从不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Struts 2框架用于Web应用程序.尝试创建自定义404错误页面时,我发现Struts 2为我完成了预期的工作.

I'm using the Struts 2 framework for a webapp. While trying to create a custom 404 error page I discovered Struts 2 is doing more work for me then intended.

我只使用一个 struts.xml 和一个 web.xml 文件.在 web.xml 文件中,添加了以下代码:

I only use a struts.xml and a web.xml file. In the web.xml file I added the following code:

<error-page>
    <error-code>404</error-code>
    <location>/error/Error404.jsp</location>
</error-page>

现在,当我启动我的Web应用程序时,它会照常转到首页.当我尝试这种类型的网址(所有不存在的网址)时:

Now, when I start my webapp it goes to the homepage as usual. When I try urls of this type (all urls that don't exist):

http://localhost:8080/MyWebApp/fhgfhfhfhgfhgfhfhfh
http://localhost:8080/MyWebApp/fhgfhfhfhgfhgfhfhfh.action

Struts将处理请求,运行拦截器,并自动重定向到默认页面.我希望我的404错误页面会显示在这些页面上.我什至在 struts.xml 中删除了这一行,但它仍然重定向!

Struts will process the request, run the interceptors, and automatically redirect to the default page. I would have expected my 404 error page to be shown on these pages. I even removed this line in struts.xml but it still redirects!

<default-action-ref name="index" />

当我使用以下网址时:

http://localhost:8080/MyWebApp/fhgfhfhfhgfhgfhfhfh.jsp

Struts仅显示一个空白屏幕,没有任何请求正在处理(没有拦截器,没有重定向...).我也觉得这很奇怪,因为我看不到该请求的无效内容(不存在的JSP文件除外)

Struts just shows a blank screen and no request is being processed (no interceptors, no redirecting...). I find this weird too because I don't see what's invalid about that request (other than the JSP file that doesn't exist)

所以我想我需要修改我的 struts.xml ,以便可以覆盖一些默认的重定向行为(我不希望通过转到主页来掩盖404错误),并且我的 web.xml 也可以处理任意模糊的请求(因此,实际上将处理用户键入的任何URL,而不仅仅是显示空白屏幕.我的配置中缺少什么?

So I guess I need to modify my struts.xml so I can override some default redirecting behaviour (I don't want the 404 error to be covered up by going to the homepage), and my web.xml to also handle any arbitrary obscure requests (so any URL that is being typed in by the user will at actually be handled instead of just showing blank screens. What am I missing in my configuration?

PS:我不是在谈论Java异常/错误,而只是在谈论HTTP错误.Java异常得到了正确的处理,就像我希望它们表现的那样.

PS: I'm not talking about Java exceptions/errors, but only the HTTP errors. The Java exceptions are handled properly like I want them to behave.

推荐答案

我找到了它不显示的原因.

I found the reasons why it was not showing.

黑屏问题如下:我的404页面的路径不正确.它应该是从webContent文件夹开始的路径,并以斜杠"/"开头.

The problem with blank screen was as follows: the path to my 404 page was not correct. It should be the path starting from the webContent folder, and starting with a slash "/".

默认操作的问题可能是服务器重新启动或缓存问题.我关闭浏览器和IDE,然后重新启动一切.然后default-action-ref真的消失了.

The problem with the default action was probably a server reboot or cache problem. I close my browser and my IDE, and restarted everything. Then the default-action-ref was REALLY gone.

我还发现了另一个有趣的怪癖:struts2仅处理动作映射,例如:

I also discovered another interesting quirk: struts2 handles only action mappings, for example:

http://www.mywebsite.com/webapp/whatever
http://www.mywebsite.com/webapp/whatever.action

但是它不会像这样处理服务器资源(注意文件扩展名)

but it will NOT handle server resources like this (notice the file extensions)

http://www.mywebsite.com/webapp/whatever.zip
http://www.mywebsite.com/webapp/whatever.jsp
http://www.mywebsite.com/webapp/whatever.html

您可以通过在web.xml文件中添加其他过滤器映射来对此进行补救:

You can remedy this by adding an additional filter mapping in the web.xml file:

<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/error/*</url-pattern>
    <dispatcher>ERROR</dispatcher>
</filter-mapping>

这篇关于Struts 2自定义404错误页面从不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆