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 的声明式验证: ...
随机推荐
- [转载] 关于Windows Boot Manager、Bootmgfw.efi、Bootx64.efi、bcdboot.exe 的详解
原帖: http://bbs.wuyou.net/forum.php?mod=viewthread&tid=303679 前言:1.本教程针对于UEFI启动来叙述的,根据普遍的支持UEFI的机 ...
- pro文件常用内容
qmake生成的pro文件中常用变量 SUBDIRS 指定子目录 TARGET 指定生成的应用程序名(默认为项目名) DEPENDPATH 指定程序编译时依赖的相关路径 INCLUDEPATH 指定头 ...
- 纯CSS做的一个Silder
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- 删除cygwin
由于cygwin 学习了linux 的用户所有者的方式,要删除有一定困难. 所以要右击 然后点 获取管理员所有权 几分钟之后 就可以删了
- java设计模式案例详解:观察者模式
观察者模式的应用场景: 1. 对一个对象状态的更新,需要其他对象同步更新,而且其他对象的数量动态可变. 2. 对象仅需要将自己的更新通知给其他对象而不需要知道其他对象的细节. 举个例子说明,这个例子讲 ...
- Struts2中的验证框架
通过注解的方式,可以让方法不用验证 @SkipValidation public String toRegView() { System.out.println("toRegView&quo ...
- CDockablePane 关闭的问题
显示或者隐藏 当点击CDockablePane上的关闭按钮时,并不能将其关闭,知识将其隐藏了起来,如果需要重新显示或隐藏,则相关命令的响应函数如下: if(m_Panes.GetSafeHwnd()) ...
- java 持有对象
1.泛型和类型安全的容器 ArrayList,可以自动扩充大小的数组,add插入对象,get访问对象,size查看对象数目. 1 /** 2 * 泛型和类型安全的容器 3 * 2016/5/6 4 * ...
- 利用未文档化API:RtlAdjustPrivilege 提权实现自动关机
这里主要是利用NTDLL.dll中未文档化的API: RtlAdjustPrivilege 来实现提权.自动关机的功能. RtlAdjustPrivilege定义如下: NTSTATUS RtlAdj ...
- PAT1016
A long-distance telephone company charges its customers by the following rules: 一个长途电话公司费用告诉它的顾客需要遵循 ...