Struts2中自定义404错误页面

目的:

客户访问一个该网站下不存在的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="https://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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值