Struts2中自定义404错误页面

来源:互联网 发布:美国国立指南数据库 编辑:程序博客网 时间:2024/05/18 08:57

目的:

客户访问一个该网站下不存在的action,为了给客户一个友好的界面提示

在struts.xml中配置

<default-action-ref name="404Error"></default-action-ref><action name="404Error"><result type="redirect">/error/404error.html</result></action>
这样defalut-action-ref就是当用户输入非法的action时,所匹配的action,默认到ActionSupport中并返回Action.SUCCESS
但是在struts2中,如果用户输入的是非法的http://xxxxxxxx/xx.jsp时,在Struts2的FilterPreparedFilterAndExecuter中只会对
<span style="white-space:pre"></span>/xxx和/xxx.action进行拦截见(ActionMapping中的部分代码),所以<span style="font-family: Arial;">defalut-action-ref此时就不起作用了</span>
<pre name="code" class="java">    protected String dropExtension(String name, ActionMapping mapping) {        if (extensions == null) {            return name;        }        for (String ext : extensions) {            if ("".equals(ext)) {                // This should also handle cases such as /foo/bar-1.0/description. It is tricky to                // distinquish /foo/bar-1.0 but perhaps adding a numeric check in the future could                // work                int index = name.lastIndexOf('.');                if (index == -1 || name.indexOf('/', index) >= 0) {                    return name;                }            } else {                String extension = "." + ext;                if (name.endsWith(extension)) {                    name = name.substring(0, name.length() - extension.length());                    mapping.setExtension(ext);                    return name;                }            }        }        return null;    }
<span style="font-family: Arial;">这时我们需要</span><span style="font-family: Arial;">在web.xml中配置错误页面</span>
<span style="font-family: Arial;"></span><pre name="code" class="java"><span style="white-space:pre"></span><error-page><error-code>404</error-code><location>/error/404error.html</location></error-page>
这样其实就可以啦,但是部分用户调试时发现web.xml这个没起作用,也没有跳转到指定的error页面。

<span style="font-family: Arial;"><span style="white-space:pre"></span>而是显示这个页面(当时就困扰我一下午)</span>
<span style="font-family: Arial;"><img src="http://img.blog.csdn.net/20160404233928964?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /></span>
<span style="font-family: Arial;">别急:这是浏览器对错误页面进行优化导致的,只需要</span>
<span style="font-family: Arial;"><span style="color: rgb(50, 62, 50); font-family: simsun; font-size: 14px; line-height: 21px; background-color: rgb(127, 163, 206);">在IE</span><span style="color:#FF0000;word-wrap: normal; word-break: normal; line-height: 21px; font-family: simsun; font-size: 14px; background-color: rgb(127, 163, 206);">工具</span><span style="color: rgb(50, 62, 50); font-family: simsun; font-size: 14px; line-height: 21px; background-color: rgb(127, 163, 206);">-></span><span style="color:#FF0000;word-wrap: normal; word-break: normal; line-height: 21px; font-family: simsun; font-size: 14px; background-color: rgb(127, 163, 206);">Internet选项</span><span style="color: rgb(50, 62, 50); font-family: simsun; font-size: 14px; line-height: 21px; background-color: rgb(127, 163, 206);">-></span><span style="color:#FF0000;word-wrap: normal; word-break: normal; line-height: 21px; font-family: simsun; font-size: 14px; background-color: rgb(127, 163, 206);">高级</span><span style="color: rgb(50, 62, 50); font-family: simsun; font-size: 14px; line-height: 21px; background-color: rgb(127, 163, 206);">中将</span><span style="color:#FF0000;word-wrap: normal; word-break: normal; line-height: 21px; font-family: simsun; font-size: 14px; background-color: rgb(127, 163, 206);">显示友好http错误提示</span><span style="color: rgb(50, 62, 50); font-family: simsun; font-size: 14px; line-height: 21px; background-color: rgb(127, 163, 206);">的前面的勾取消,ok了。</span></span>




0 0
原创粉丝点击