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编码相关函数试题
1.检查字符串在指定的编码里是否有效的函数是什么? 2.获取字符编码的函数是什么? 3.解析 GET/POST/COOKIE 数据并设置全局变量的函数是什么? 4.大小写不敏感地查找字符串在另一个字符 ...
- PHP的几个常用加密函数【转载】
转自 https://jellybool.com/post/php-encrypt-functions 在网站的开发过程中,常常需要对部分数据(如用户密码)进行加密,本文主要介绍PHP的几个常见的加密 ...
- PopUpManager弹出窗口
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="ht ...
- C# List<T> To DataTable
public DataTable ConvertToDataTable<T>(IList<T> data) { PropertyDescriptorCollection pro ...
- tomcat连接数设置
如何加大tomcat连接数 在tomcat配置文件server.xml中的<Connector ... />配置中,和连接数相关的参数有:minProcessors:最小空闲连接线程数,用 ...
- 【dp 背包变形】 poj 1837
#include <cstdio> #include <memory.h> #include<iostream> using namespace std; ][]; ...
- iOS字符串转化成CGFloat
NSString *str = @"abc"; [str floatValue];
- ==和equals的异同
== 和 Equals 的区别 1. == 是一个运算符. 2.Equals则是string对象的方法,可以.(点)出来. 我们比较无非就是这两种 1.基本数据类型比较 2.引用对象比较 1.基本数据 ...
- io scheduler
http://doctorlzr1988.blog.163.com/blog/static/50456520201051905236683/
- 把一个 int 数字 n 格式化成16进制的字符串(前面补零成0位)
例如,输入n=10,要求输出 0x0000000A; C++: sprintf( buffer, "0x%08X", n); C#: string s = string.F ...