首页 技术 正文
技术 2022年11月17日
0 收藏 332 点赞 3,853 浏览 2180 个字

成熟的MVC框架应该提供成熟的异常处理机制。当然可以在方法中手动捕捉异常,当捕捉到特定异常时,返回特定逻辑视图名。

这种方式非常繁琐,需要在方法中写大量try catch块,最大的缺点还是一旦需要改变异常处理方法时,需要修改代码。

最好的方式是通过声明式的方式管理异常处理。struts2提供了一种声明式的异常处理方式。

一、原理

    我们看Action接口中的execute方法声明。
public String execute() throws Exception 这就意味着我们重写该方法时,无需进行任何异常处理,而是把异常抛给struts2框架处理. struts2框架接收到Action抛出的异常后,根据struts.xml文件配置的异常映射,转入指定的视图资源。 异常映射功能是由 exception的拦截器帮我们做的。 struts2的异常处理机制是通过在struts.xml中配置<exception-mapping..../>元素完成。
属性:
exception:异常类型
result:出现该异常时,系统转入result指定的结果

二、局部异常映射和全局异常映射

全局异常映射对所有的Action都有效,但局部异常映射只对该异常映射所在的Action有效。

局部异常映射:将<exception-mapping…./>元素作为<action…/>的子元素配置
如果全局异常映射和局部异常映射配置了同一个异常类型,在该Action内,局部覆盖全局。

全局异常映射:将<exception-mapping…./>元素作为<global-exception-mappings>元素的子元素配置

三、异常处理案例

 我们做一个简单的登陆应用

(1)编写我们的Action类

        public class LoginAction implements Action{
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String execute()throws Exception{
//当用户名为monster时,抛出我们的自定义异常
if(getUsername().equals("monster")){
throw new MyException("自定义异常");
}
//当用户名为sql时,抛出sql异常
if(getUsername().equalsIgnoreCase("sql")){
throw new SQLException("用户名不能为sql");
}
if(getUsername().equals("cad")&&getPassword().equals("123456")){
return "success";
}else
{
return "error";
}
} }

(2)我们编写我们的登陆jsp页面

        <body>
<form action="${pageContext.request.contextPath }/login" method="post">
用户名:<input type="text" name="username"><br>
密 码:<input type="password" name="password"><br>
<input type="submit" value="提交">
</form>
</body>

(3)我们编写我们的配置文件struts.xml

    <package name="p2" extends="struts-default">
//全局结果视图定义
<global-results>
<result name="sql">/exception.jsp</result>
</global-results> //全局异常定义
<global-exception-mappings>
//出现sql异常,就转入到视图名为sql的视图结果
<exception-mapping result="sql" exception="java.sql.Exception"></exception-mapping>
</global-exception-mappings> <action name="login" class="com.cad.struts2.LoginAction" >
//局部异常定义
<exception-mapping result="my" exception="conm.cad.struts2.MyException"></exception-mapping>
<result name="my">/exception.jsp</result>
<result name="success">/welcome.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>

  

  

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,556
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,406
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898