在学习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. SqlServer数据库练习20190211

    一条update语句,修改多个条件 update orderdt_jimmy set qty = (case else qty end); 好了,就这样

  2. 排序算法,以php为代码示例

    一.冒泡排序 <?php/** * Created by PhpStorm. * User: 郑楚周 * Date: 2018/9/28 * Time: 16:10 */ /**冒泡排序 * C ...

  3. linux 命令终端提示符显示-bash-4.2#解决方法

    原地址:http://blog.csdn.net/liulihui1988/article/details/52796395 终端提示符显示的是-bash-4.2# 而不是root@主机名 + 路径的 ...

  4. matplotlib.pyplot.pcolormesh

     matplotlib.pyplot.pcolormesh(*args, alpha=None, norm=None, cmap=None, vmin=None, vmax=None, shading ...

  5. build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing

    maven test项目时遇到一下错误 Some problems were encountered while building the effective model for cn.temptat ...

  6. Oracle 实现查询不区分大小写(设置数据库)

    转http://blog.csdn.net/shl7765856/article/details/7622756 查询数据的时候. SQL Server 默认 不区分大小写. 如果要区分,就要额外的设 ...

  7. 集训第六周 M题

    Description   During the early stages of the Manhattan Project, the dangers of the new radioctive ma ...

  8. 使用java发送电子邮件

    经常在账号绑定邮箱或找回密码时,邮箱会收到一条验证邮件,好奇用代码该怎么发送邮件,看到了许多相关的博客,实现步骤都写的很详细,今天照着其他博客的步骤也确实实现了代码发送邮件,在这里重新记录下步骤,加深 ...

  9. linux-NMON监控

  10. BNUOJ 7697 Information Disturbing

    Information Disturbing Time Limit: 3000ms Memory Limit: 65536KB This problem will be judged on HDU. ...