现在的位置: 首页 > 综合 > 正文

java web项目异常处理办法(基于struts2的项目)

2013年08月24日 ⁄ 综合 ⁄ 共 663字 ⁄ 字号 评论关闭

1.访问web中不存在的action,struts2找不到的action就会默认执行以下这个配置,最好将“error”配置为公共的。struts.xml添加如下配置

<default-action-ref name="error"></default-action-ref>
<global-results>
            <result name="error">/error.jsp</result>
</global-results>

2.访问web中action方法报错,即所有异常处理,为了不能将错误信息显示在页面,struts.xml添加如下配置

<global-exception-mappings>
	<exception-mapping result="error" exception="java.lang.Exception"></exception-mapping>
</global-exception-mappings>

3.访问web中不存在的jsp,这个在web.xml中配置,如下:

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

error-code是错误代码,location是转向页面。如果这个配置成功,当服务器出现这个错误代码的时候,就会跳转到location这个页面。location可以是html文件,也可以是jsp页面。

通过以上的几处配置,就可以避免在用户面前展现出不必要的错误内容。

抱歉!评论已关闭.