目前在做一个网络应用程序,struts2 + spring + hibernate,服务器是tomcat。希望用户在IE地址栏乱敲的时候,所敲入的所有未定义的URL都能被程序捕捉到,然后转到一个自制的404错误处理页面。

首先查阅网上的资料,得知可以在web.xml中加入这么一段:

<error-page>

   <error-code>404</error-code>

   <location>/error/404.jsp</location>

</error-page>

这样,所以未找到的404错误都会转到/error/404.jsp接受处理。

但是经过试验,我发现以.action结尾的URL会先被struts2框架所捕获,而不会直接转入web.xml中配置的处理页面。这样,如果在struts.xml没有配置相应的action,则会在tomcat里输出错误:

严重: Could not find action or result

There is no Action mapped for namespace / and action name ******. - [unknown location]

虽然最后也能转入web.xml中配置的处理页面,但是这样在命令台瞎输出一些东西,非常不便于程序的管理和调试,所以还得想办法让struts2框架处理未定义的action。

查阅网上资料,发现可以在struts.xml中,添加一个默认的package,然后在这个默认的package中,添加一个默认的action,让这个action转向404错误处理页面:

<package name="default" extends="struts-default">

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

   <action name="notFound">

    <result>/error/404.jsp</result>

   </action>

</package>

默认的package的特点就在于不定义namespace属性,这样所有未定义的namespace都会转向这里。name="default"是为了便于阅读,其实name是什么都可以,也可以为空:name=""。

然后还要在其它已定义的package中也添加默认的action,转向404错误处理页面。这样就可以完全处理所有的404错误了。

网友评论:

<global-results>

<result name="notFound">/404.html</result>

</global-results>,也可以

struts2 404处理的更多相关文章

  1. struts2 404错误

    action类必须放在xxxx.xxx.xxx.xxx.action  包下才可以.

  2. struts2完全捕获404错误的方法

    目前在做一个网络应用程序,struts2 + spring + hibernate,服务器是tomcat.希望用户在IE地址栏乱敲的时候,所敲入的所有未定义的URL都能被程序捕捉到,然后转到一个自制的 ...

  3. 路径名称和struts.xml配置不一致导致struts2报404

    struts.xml中写的是<result name="...">authorityInterceptor/xxx.jsp</result> 但是实际的文件 ...

  4. Struts2 Handle 404 error page and wrong action

    1. To handle 404 not found yourself, just add this code to your web.xml <error-page> <error ...

  5. <welcome-file>index.action</welcome-file>直接设置action,404和struts2中的解决方案

    这几天的项目页面的访问全部改为.action访问,在修改首页时遇到了问题.将web.xml文件中<welcome-file>index.action</welcome-file> ...

  6. struts2 第一次使用 404 页面引发的一系列问题

    环境:ubuntu  . eclipse.struts-2.3.24 问题描写叙述: 1.  struts2 訪问出现404 2.  严重: Exception starting filter Str ...

  7. struts2中<welcome-file>index.action</welcome-file>直接设置action,404的解决方案

    这几天的项目页面的访问全部改为.action访问,在修改首页时遇到了问题.将web.xml文件中<welcome-file>index.action</welcome-file> ...

  8. struts2中怎样处理404?

    眼下在做一个网络应用程序,struts2 + spring + hibernate,server是tomcat.希望用户在IE地址栏乱敲的时候.所敲入的全部没有定义的URL都能被程序捕捉到,然后转到一 ...

  9. 配置Struts2后运行jsp出现404的解决方法

    更新:善用控制台查看错误信息 --------------------------------------------- 原因:Java Build Path没有导入正确的jar包或者导入了但没有把相 ...

随机推荐

  1. Creating a settings table that can handle almost any type of value

    Update: Updated article here. Today I wanted to be able to have a table store any type of value as a ...

  2. HTML5 video 事件

    1.获取视频时间长度 当视频载入video后,使用 onloadedmetadata 事件获取视频的时间长度. video.onloadedmetadata = function () { var v ...

  3. 深入理解 静态类和静态字段(C# 基础)

    序言 以前,总是被提醒,在编程过程中尽量少用静态变量,数据丢失什么的,今天有空,禁不住对静态变量的强烈好奇,跟我一起了解下静态家族的内幕吧. 静态类 定义 静态类与非静态类的重要区别在于静态类不能实例 ...

  4. .net 既可以输入又可以选择的dropdownlist

    第一.<script src="../../../Common/Scripts/combox.js" type="text/javascript"> ...

  5. Array.prototype.slice()的用法

    我们知道,Array.prototype.slice.call(arguments)能将具有length属性的对象转成数组,除了IE下的节点集合(因为ie下的dom对象是以com对象的形式实现的,js ...

  6. 5W1H分析法

    "5W1H分析法"也叫"六何分析法",它是一种分析方法也可以说是一种创造技法.是对选定的项目.工序和操作,都要从原因(Why).对象(What).地点(Wher ...

  7. Codeforces 372B Counting Rectangles is Fun

    http://codeforces.com/problemset/problem/372/B 题意:每次给出一个区间,求里面有多少个矩形 思路:预处理,sum[i][j][k][l]代表以k,l为右下 ...

  8. QtCreator调试传入运行参数

    QtCreator是非常不错的IDE,最近在做的Qt命令行应用,因为调试的环境不同等问题,需要在调试的时候为 main() 传入参数.度娘了半天,没找到方法,只能自力更生.后来在“项目-构建和运行-运 ...

  9. PowerShell常用的属性

    get-location | get-member  -membertype  property -------获取对象的属性---------- 获取对象特定的成员, 湖区.Net Framwork ...

  10. CentOS6.4安装mplayer

    1.准备软件 mplayer官网:http://www.mplayerhq.hu/design7/news.html RPM Fusion网址:http://rpmfusion.org/ EPEL网址 ...