目前在做一个网络应用程序,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. 《那些事之Log4j》什么是log4j?【专题一】

    Log4j 简介 在应用程序中添加日志记录总的来说基于三个目的: 监视代码中变量的变化情况,周期性的记录到文件中供其他应用进行统计分析工作: 跟踪代码运行时轨迹,作为日后审计的依据: 担当集成开发环境 ...

  2. CSS定义网页滚动条

    (一)滚动条样式主要涉及到如下CSS属性: overflow属性: 检索或设置当对象的内容超过其指定高度及宽度时如何显示内容overflow: auto; 在需要时内容会自动添加滚动条overflow ...

  3. python学习(二)

    这几天脑子里一直在想一个应用,想以此来练习python.用一句话来概括这个应用的功能,大致表述是这样:自动采集全省各类公共文化机构网站上新发布的信息,并分类呈现.各类公共文化机构,是指公共图书馆.文化 ...

  4. jquery之下拉列表select

    选择下拉列表中的一项,文本框显示其值 html代码如下: <select id="ttt"> <option value="Volvo" id ...

  5. IOS 文件管理 2

    IOS开发-文件管理(二) 五.Plist文件 String方式添加               NSString *path = [NSHomeDirectory( )  stringByAppen ...

  6. ng-bind-html 的使用

    AngualrJS 提供了指令ng-bind-html 用于绑定包含HTML标签的文档,使用方式: <ANY ng-bind-html=""> ... </ANY ...

  7. BZOJ 1063 道路设计NOI2008

    http://www.lydsy.com/JudgeOnline/problem.php?id=1063 题意:给你一棵树,也有可能是不连通的,把树分成几个链,求每个点到根经过的最大链数最小,而且要输 ...

  8. 最新版AltiumDesignerSummer9下载+破解

    下载地址:ed2k://|file|AltiumDesignerSummer9Build9.3.1.19182.7z|1875307483|e65d364bf987fb5dcfb81c081a1562 ...

  9. TXMLDocument换行的两种方案

    手写代码:   XML格式化使用msxml引擎,Delphi代码如下: Delphi/Pascal code   ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...

  10. cf486A Calculating Function

    A. Calculating Function time limit per test 1 second memory limit per test 256 megabytes input stand ...