在学习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. CAD参数绘制多段线(网页版)

    多段线又被称为多义线,表示一起画的都是连在一起的一个复合对象,可以是直线也可以是圆弧并且它们还可以加不同的宽度. 主要用到函数说明: _DMxDrawX::PathLineTo 把路径下一个点移到指定 ...

  2. mac 下安装python pil

    mac:sudo brew install freetype sudo pip install pillow ubuntu: sudo apt-get install libfreetype6-dev ...

  3. ios摇一摇功能

    在 UIResponder中存在这么一套方法 - (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event __OSX_A ...

  4. Failed to resolve filter报错原因

    问题 页面写过滤器,控制台报错,Failed to resolve filter 分析 语法错误?先检查 ``` {{ params | filterA }} filters: { filterA: ...

  5. 零基础入门学习Python(30)--文件系统:介绍一个高大上的东西

    知识点 os,os.path模块中关于文件.目录常用的函数使用方法 在使用os模块,需要先进行import操作: import os os模块中关于文件/目录常用的函数使用方法 函数名 函数作用 示例 ...

  6. buf.entries()详解

    buf.entries() 返回:{Iterator} 从当前 Buffer 的内容中,创建并返回一个 [index, byte] 形式的迭代器. const buf = Buffer.from('b ...

  7. LeetCode(63)Unique Paths II

    题目 Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. Ho ...

  8. LeetCode(29)Divide Two Integers

    题目 Divide two integers without using multiplication, division and mod operator. If it is overflow, r ...

  9. 61. mybatic insert异常:BindingException: Parameter 'name' not found【从零开始学Spring B】

    mybatic insert异常:BindingException: Parameter 'name' not found [从零开始学习Spirng Boot-常见异常汇总] 异常信息如下: nes ...

  10. LaTex/Overleaf使用笔记

    1. 添加网页引用 @misc{ r1,   author = "Wikipedia",   title = "Binary decision diagram --- { ...