Struts2---声明式异常处理
在service方法里 throw抛出一个异常, 然后再方法声明上加上throws:
public List<Category> list() throws SQLException{
Connection conn=DB.createConn();
String sql = "select * from _category";
PreparedStatement ps = DB.prepare(conn, sql);
List<Category> categories = new ArrayList<Category>();
try {
ResultSet rs=ps.executeQuery();
Category c = null;
while(rs.next()){
c =new Category();
c.setId(rs.getInt("id"));
c.setName(rs.getString("name"));
c.setDescription(rs.getString("description"));
categories.add(c);
}
} catch (SQLException e) {
e.printStackTrace();
throw(e);
}
DB.close(ps);
DB.close(conn);
return categories;
}
在调用list方法的action里 throws, 这样就不用try catch而是由struts2处理:
public String list() throws Exception{
categories=categoryService.list();
return SUCCESS;
}
struts.xml里如何配置?
<package name="admin" namespace="/admin" extends="struts-default" >
<default-action-ref name="index"/>
<action name="index">
<result>/admin/index.html</result>
</action>
<action name="*_*" class="com.bjsxt.bbs2009.action.{1}Action" method="{2}">
<result>/admin/{1}_{2}.jsp</result>
<result name="input">/admin/{1}_{2}.jsp</result>
<exception-mapping result="error" exception="java.sql.SQLException"/>
<result name="error">/error.jsp</result>
<result name="exception_handle">/admin/exception.jsp</result>
</action>
</package>
service里的sql语句改成错误的, 这样在调用页面的时候, 就会显示error.jsp.
下面是异常处理最常用的方法:
1. 配置新的package, global results, global-exception-mappings, 自己的action的包继承异常包即可.
原理:::拦截器
<package name="bbs2009_default" extends="struts-default">
<global-results>
<result name="error">/error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping result="error" exception="Exception"></exception-mapping>
</global-exception-mappings>
</package> <package name="admin" namespace="/admin" extends="bbs2009_default" >
<default-action-ref name="index"/>
<action name="index">
<result>/admin/index.html</result>
</action>
<action name="*_*" class="com.bjsxt.bbs2009.action.{1}Action" method="{2}">
<result>/admin/{1}_{2}.jsp</result>
<result name="input">/admin/{1}_{2}.jsp</result>
<!--<exception-mapping result="error" exception="java.sql.SQLException"/>
<result name="error">/error.jsp</result>-->
<result name="exception_handle">/admin/exception.jsp</result>
</action>
</package>
Struts2---声明式异常处理的更多相关文章
- Struts2声明式异常处理
通过配置.xml文件的方式处理异常信息: 注意:配置.xml文件的同时还要抛出异常 标签:<exception-mapping></exception-mapping>和< ...
- Struts2的声明式异常处理
在struts2应用程序中你还在使用try catch语句来捕获异常么?如果是这样的,那你OUT啦!struts2支持声明式异常处理,可以再Action中直接抛出异常而交给struts2来 处理,当然 ...
- Struts2学习---拦截器+struts的工作流程+struts声明式异常处理
这一节我们来看看拦截器,在讲这个之前我是准备先看struts的声明式异常处理的,但是我发现这个声明式异常处理就是由拦截器实现的,所以就将拦截器的内容放到了前面. 这一节的内容是这样的: 拦截器的介绍 ...
- Struts2学习第八课 声明式异常处理
异常处理:exception-mapping元素 exception-mapping元素:配置当前的action的声明式异常处理 exception-mapping元素有两个属性: --excepti ...
- struts的声明式异常处理
情景 使用Struts封装的下载文件的功能 当下载文件找不到的时候,struts获取的InputStream为null 这个时候,就会报500错误 java.lang.IllegalArgumentE ...
- 6.声明式异常处理、I18N
声明式异常处理 1.在Action 中进行异常映射 <exception-mapping result="error" exception="java.sql.SQ ...
- 学习Struts框架系列(三):声明式异常处理
在Struts1.X的版本中加入了对异常的处理Exception Handler,有了它我们可以不使用try/catch捕获异常,一旦出现了我们已经定义的异常,那么就会转到相应的页面,并且携带异常信息 ...
- Strut2_声明式异常处理
Service 往外抛异常 public List<Category> list() throws SQLException{ Connection conn = DB.createCon ...
- [原创]java WEB学习笔记60:Struts2学习之路--Actioin-声明式异常处理
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- 9、 Struts2验证(声明式验证、自定义验证器)
1. 什么是Struts2 验证器 一个健壮的 web 应用程序必须确保用户输入是合法.有效的. Struts2 的输入验证 基于 XWork Validation Framework 的声明式验证: ...
随机推荐
- PHP数组函数试题
使用Ctrl+A查看答案 1.将数组的键名全部转换成小写和大写的函数是什么?答:array_change_key_case($array [,CASE_LOWER|CASE_UPPER]) 2.创建一 ...
- List<T> 求差集
List<, , , , , }; List<, , , , , }; List<int> c = b.Except(a).ToList(); foreach (int i i ...
- win8上cmder文字重叠问题
1.用过ubuntu上的bash,zsh后发现win自带的cmd弱爆了,在网上搜索后找到了代替品cmder,下载安装后好发现中文错位的问题, 状况如下: 修复方法:把设置里面的Monospace选项勾 ...
- hadoop运行作业的脚本解析
#!/usr/bin/env bash # Licensed to the Apache Software Foundation (ASF) under one or more # contribut ...
- gvim work notes.. a few days' work on 64bit vim and plugin compilations
(a 600MB+ sized c/c++ compiler which is capable of hi-light and JB styled completion!! and of-course ...
- ubuntu server 11.10 mysql 自动备份脚本
1.下载最新的备份脚本(AutoMySQLBackup) 点这里下载 2.修改脚本配置部分 vi /root/automysqlbackup-2.5.1-01.sh USERNAME=root PA ...
- 基于VirtualBox 安装和配置Fuel OpenStack(V6.1)
1.环境准备 准备一台内存较大的主机,12G以上 下载安装VirtualBox及其匹配的扩展包 virtualbox: http://download.virtualbox.org/virtualbo ...
- Php和httpd.conf的配置
http://www.cnblogs.com/homezzm/archive/2012/08/01/2618062.html http://book.51cto.com/art/201309/4096 ...
- Struts2语法--Ognl
OGNL: Object Graph Navigation Language index.jsp: <body> 访问属性 <a href="<%=contextPa ...
- BCDedit 研究
bcdedit的研究 系统引导安装 2009-11-12 22:21:13 阅读13 评论0 字号:大中小 首先说明下引导: 微软在Vista之前的系统,采用的是Ntldr来进行引导系统,使用的是bo ...