在学习j2se的时候学习过利用java的exception类去处理异常。在struts2框架中也提供了对于异常的处理。简单说就是当Action发生异常时。能够在struts2.xml文件里进行配置,将页面跳转进行调整,给用户一个更人性化的反馈。

在struts2的异常处理中。分为两种异常处理,一种是全局的:全部Action出现异常都能够跳转到此页面。

还有一种是局部的:指定某一个Action异常跳转到此页面。

局部异常处理

在struts2.xml中在exception-mapping节点中指定result属性名称;exception属性中指定异常类,此类要继承exception。

result节点中指定name属性,以及异常发生后跳转的页面。

<?xml version="1.0" encoding="UTF-8"?

>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="struts2" extends="struts-default">
<action name="login" class="com.syq.struts2.LoginAction" >
<exception-mapping result="usernameInvalid" exception="com.syq.exception.UsernameException"></exception-mapping>
<exception-mapping result="passwordInvalid" exception="com.syq.exception.PasswordException"></exception-mapping> <result name="success">/result.jsp</result>
<result name="usernameInvalid">/UsernameInvalid.jsp</result>
<result name="passwordInvalid">/PasswordInvalid.jsp</result>
</action>
</package> </struts>

全局异常处理

global-results节点中指定异常跳转的页面;global-exception-mappings中指定异常类。

<?

xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="struts2" extends="struts-default">
<global-results>
<result name="usernameInvalid">/UsernameInvalid.jsp</result>
<result name="passwordInvalid">/PasswordInvalid.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping result="usernameInvalid" exception="com.syq.exception.UsernameException"></exception-mapping>
<exception-mapping result="passwordInvalid" exception="com.syq.exception.PasswordException"></exception-mapping>
</global-exception-mappings> <action name="login" class="com.syq.struts2.LoginAction" >
<result name="success">/result.jsp</result>
</action>
</package> </struts>

我们即能够在Action中定义异常与结果。也能够定义全局的异常与结果,局部的优先级要大于全局的,假设定义全局的那么能够为全部的Action所公用。而局部的异常与结果仅仅能被当前的Action所独享。

SSH-struts2的异常处理的更多相关文章

  1. SSH(Struts2+Spring+Hibernate)框架搭建流程<注解的方式创建Bean>

    此篇讲的是MyEclipse9工具提供的支持搭建自加包有代码也是相同:用户登录与注册的例子,表字段只有name,password. SSH,xml方式搭建文章链接地址:http://www.cnblo ...

  2. Struts2的异常处理

    Struts2的异常处理 1.异常处理机制(1)发送请求到控制器(Action); (2)Action出现异常后,依照所捕捉的不同异常转入不同的视图资源. 2.异常捕捉 (1)在Action的处理逻辑 ...

  3. 工作笔记3.手把手教你搭建SSH(struts2+hibernate+spring)环境

    上文中我们介绍<工作笔记2.软件开发经常使用工具> 从今天開始本文将教大家怎样进行开发?本文以搭建SSH(struts2+hibernate+spring)框架为例,共分为3步: 1)3个 ...

  4. 【struts2】Struts2的异常处理

    在Action中execute方法声明为:public String execute() throws Exception,这样,Action可以抛出任何Exception. 1)自己实现异常处理 我 ...

  5. (十)struts2的异常处理机制

    成熟的MVC框架应该提供成熟的异常处理机制.当然可以在方法中手动捕捉异常,当捕捉到特定异常时,返回特定逻辑视图名. 这种方式非常繁琐,需要在方法中写大量try catch块,最大的缺点还是一旦需要改变 ...

  6. SSH答疑解惑系列(三)——Struts2的异常处理

    Struts2的异常采用声明式异常捕捉,具体通过拦截器来实现. 在项目中,我们可以在Action中直接抛出异常,剩下的就交给Struts2的拦截器来处理了.当然,我们需要进行相关配置. Struts2 ...

  7. SSH(Struts2+Spring4+HIbernate5)的简化

    今天给大家带来的是一个简单的新闻发布系统 首先在学习过程中我是深有体会,做事情不要浮躁,不要想着一口吃下一个胖子, 最最重要的是理解,理解透了学什么东西都是随心所欲的. 开发环境:win10系统 jd ...

  8. SSH:Struts2.2+Hibernate3.6+Spring3.1分页示例[转]

    参考资料 1 ssh分页(多个例子) http://useryouyou.iteye.com/blog/593954 2 ssh2分页例子 http://459104018-qq-com.iteye. ...

  9. struts2:异常处理

    Struts2框架提供了自己的异常处理机制,只需要在struts.xml文件中配置异常处理即可,而不需要在Action方法中来捕捉异常. 传统方法 public String execute() th ...

  10. Struts2之异常处理

    一.学习案例:通过在input.jsp页面输入登录账号和password測试异常处理机制. 二.案例分析:struts2提供了局部异常处理机制和全局异常处理机制.局部优先于全局异常处理,当异常找不到局 ...

随机推荐

  1. windows sdk 设置窗体透明

    #define WINVER 0x0501 #include <windows.h> /* Declare Windows procedure */ LRESULT CALLBACK Wi ...

  2. VS2017 ATL创建ActiveX编程要点

    VS2017 ATL创建ActiveX控件编程要点: 一.创建vs项目需要安装器visual studio installer中: 安装 visual studio扩展开发中的 用于x86和x64的V ...

  3. 解决【npm ERR! Unexpected end of JSON input while parsing near '...sh_time":141072930277'】方案

    问题描述执行npm install的时候报错npm ERR! Unexpected end of JSON input while parsing near '...sh_time":141 ...

  4. vue 常用功能和命令

    1. vue-cli 构建项目 # 全局安装 vue-cli $ npm install --global vue-clif # 创建一个基于 webpack 模板的新项目 $ vue init we ...

  5. mac apache 配置

    mac系统自带apache这无疑给广大的开发朋友提供了便利,接下来是针对其中的一些说明 一.自带apache相关命令 1. sudo apachectl start 启动服务,需要权限,就是你计算机的 ...

  6. Buffer.allocUnsafe()

    Buffer.allocUnsafe(size) size {Number} 分配一个 size 字节大小的新的非零填充(non-zero-filled)的 Buffer.size 必须小于等于 re ...

  7. LeetCode 188. Best Time to Buy and Sell Stock IV (stock problem)

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  8. python——文件管理

    文件操作分为读.写.修改 一.读文件 f = open(file='D:/工作日常/兼职白领学生空姐模特护士联系方式.txt',mode='r',encoding='utf-8') data = f. ...

  9. Oracle导出txt文本文件

    转自: http://blog.csdn.net/ahngzw77/article/details/8652722 对于SPOOL数据的SQL,最好要自己定义格式,以方便程序直接导入,SQL语句如:  ...

  10. 使用 docker 安装 OpenVAS 漏洞扫描软件

    https://blog.csdn.net/freewebsys/article/details/78804624