在项目开发中如果能使用统一异常处理的话,则不但可以简化开发过程,而且还更容易团队的开发与合作,事半功倍。以下通过使用struts1X 结合实现一个项目的统一异常处理解决方案的例子:
第一步:
配置好Struts整合web。
第二步:
编写统一异常类:SystemException.java
Java代码
package com.changtu.oa.managers;
/**
* 统一异常类
* 用于存储异常信息,供抛出
* @author Mike
*/
public class SystemException extends RuntimeException {
//异常代码
private String key;
private Object[] values;
// 构造器重载
public SystemException() {
super();
}
public SystemException(String message, Throwable throwable) {
super(message, throwable);
}
public SystemException(String message) {
super(message);
}
public SystemException(Throwable throwable) {
super(throwable);
}
public SystemException(String message,String key){
super(message);
this.key = key;
}
public SystemException(String message,String key,Object value){
super(message);
this.key = key;
this.values = new Object[]{value};
}
public SystemException(String message,String key,Object[] values){
super(message);
this.key = key;
this.values = values;
}
public String getKey() {
return key;
}
public Object[] getValues() {
return values;
}
}
package com.changtu.oa.managers;
/**
* 统一异常类
* 用于存储异常信息,供抛出
* @author Mike
*/
public class SystemException extends RuntimeException {
//异常代码
private String key;
private Object[] values;
// 构造器重载
public SystemException() {
super();
}
public SystemException(String message, Throwable throwable) {
super(message, throwable);
}
public SystemException(String message) {
super(message);
}
public SystemException(Throwable throwable) {
super(throwable);
}
public SystemException(String message,String key){
super(message);
this.key = key;
}
public SystemException(String message,String key,Object value){
super(message);
this.key = key;
this.values = new Object[]{value};
}
public SystemException(String message,String key,Object[] values){
super(message);
this.key = key;
this.values = values;
}
public String getKey() {
return key;
}
public Object[] getValues() {
return values;
}
}
编写统一异常处理类:SystemExceptionHandler.java
Java代码
package com.changtu.oa.web;
…
import com.changtu.oa.managers.SystemException;
/**
* 统一异常处理类
* @author Mike
*/
public class SystemExceptionHandler extends ExceptionHandler {
private static Log logger = LogFactory.getLog(SystemExceptionHandler.class);
/**
* 处理SystemException异常
*/
@Override
public ActionForward execute(
Exception ex,
ExceptionConfig ae,
ActionMapping mapping,
ActionForm formInstance,
HttpServletRequest request,
HttpServletResponse response) throws ServletException {
ActionForward forward = null;
if(ae.getPath() != null){
forward = new ActionForward(ae.getPath());
}else{
forward = mapping.getInputForward();
}
logger.debug("出现异常", ex);
// 在控制台显示的异常信息
ex.printStackTrace();
// 如果抛出的异常是自定义的统一异常
if(ex instanceof SystemException){
SystemException se = (SystemException)ex;
//取出key值
String key = se.getKey();
// 储存异常信息......
ActionMessage error = null;
if( key == null){
error = new ActionMessage(ae.getKey(),se.getMessage());
}else{
if(se.getValues() != null){
error = new ActionMessage(key,se.getValues());
}else{
error = new ActionMessage(key);
}
}
this.storeException(request, key, error, forward, ae.getScope());
return forward;
}
return super.execute(ex, ae, mapping, formInstance, request, response);
}
}
package com.changtu.oa.web;
…
import com.changtu.oa.managers.SystemException;
/**
* 统一异常处理类
* @author Mike
*/
public class SystemExceptionHandler extends ExceptionHandler {
private static Log logger = LogFactory.getLog(SystemExceptionHandler.class);
/**
* 处理SystemException异常
*/
@Override
public ActionForward execute(
Exception ex,
ExceptionConfig ae,
ActionMapping mapping,
ActionForm formInstance,
HttpServletRequest request,
HttpServletResponse response) throws ServletException {
ActionForward forward = null;
if(ae.getPath() != null){
forward = new ActionForward(ae.getPath());
}else{
forward = mapping.getInputForward();
}
logger.debug("出现异常", ex);
// 在控制台显示的异常信息
ex.printStackTrace();
// 如果抛出的异常是自定义的统一异常
if(ex instanceof SystemException){
SystemException se = (SystemException)ex;
//取出key值
String key = se.getKey();
// 储存异常信息......
ActionMessage error = null;
if( key == null){
error = new ActionMessage(ae.getKey(),se.getMessage());
}else{
if(se.getValues() != null){
error = new ActionMessage(key,se.getValues());
}else{
error = new ActionMessage(key);
}
}
this.storeException(request, key, error, forward, ae.getScope());
return forward;
}
return super.execute(ex, ae, mapping, formInstance, request, response);
}
}
第三步:
编写struts配置文件:
1、在src目录下新建文件名为:MessageResources.properties 资源文件,内容如下:
errors.detail={0}
2、struts-config.xml配置文件信息如下:
Java代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<!-- 统一异常处理 -->
<global-exceptions >
<exception
key="errors.detail"
type="java.lang.Exception"
path="/common/exception.jsp"
scope="request"
handler="com.changtu.oa.web.SystemExceptionHandler"
></exception>
</global-exceptions>
<action-mappings>
</action-mappings>
<!-- 加载国际化资源文件 -->
<message-resources parameter="MessageResources" />
</struts-config>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<!-- 统一异常处理 -->
<global-exceptions >
<exception
key="errors.detail"
type="java.lang.Exception"
path="/common/exception.jsp"
scope="request"
handler="com.changtu.oa.web.SystemExceptionHandler"
></exception>
</global-exceptions>
<action-mappings>
</action-mappings>
<!-- 加载国际化资源文件 -->
<message-resources parameter="MessageResources" />
</struts-config>
第四步:编写业务逻辑:UserManager.java UserManagerImple.java
UserManagerImpl.java
Java代码
package com.changtu.oa.managers.impl;
//...
public class UserManagerImpl implements UserManager {
// 登陆
public User login(String username, String password) {
/**
* 因为设置了User的auto-import="false",所以,在这里使用
* HQL查询的时候,必须使用全路径的类名
*/
User user = (User)getSession().createQuery(
"select u from com.changtu.oa.model.User u where u.username = ?")
.setParameter(0, username)
.uniqueResult();
if(user == null){
throw new SystemException("没有这个用户");
}
if(!user.getPassword().equals(password)){
throw new SystemException("密码错误!");
}
return user;
}
}
package com.changtu.oa.managers.impl;
//...
public class UserManagerImpl implements UserManager {
// 登陆
public User login(String username, String password) {
/**
* 因为设置了User的auto-import="false",所以,在这里使用
* HQL查询的时候,必须使用全路径的类名
*/
User user = (User)getSession().createQuery(
"select u from com.changtu.oa.model.User u where u.username = ?")
.setParameter(0, username)
.uniqueResult();
if(user == null){
throw new SystemException("没有这个用户");
}
if(!user.getPassword().equals(password)){
throw new SystemException("密码错误!");
}
return user;
}
}
第五步:异常页面 exception.jsp
Java代码
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@include file="/common/common.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
<html:errors/>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@include file="/common/common.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
<html:errors/>
</body>
</html>
第六步:编写自己的Action进行测试。在Action中出自定义的异常时,struts会自动根据你的配置跳转到异常界面,显示异常信息。(其中有不完整的,需自己补充)
分享到:
相关推荐
从网上搜索的一段关于struts1.x自定义异常处理的方法。
Struts1.x常用的开发包,需要学习struts1.x朋友可以下载
java Struts 1.x 框架 Web 开发 java Struts 1.x 框架 Web 开发
一个整个项目的所有代码和截图!内容非常详细,很适合struts2初学者学习哦~
项目中出现的异常通常要用一个友好的异常页面来显示,通过对struts2.xml的配置能拦截全局异常,只要出现异常就会转向异常页面。
Struts2异常处理机制
Struts2.x 项目实战 Struts2.x 项目实战 Struts2.x 项目实战
struts1小项目
NULL 博文链接:https://wjt276.iteye.com/blog/690323
Struts2项目开发经验总结 word版 Struts2项目开发经验总结 word版
功能: ...1. 创建web项目 2. 项目添加Struts2类包支持 3. 设置核心控制器(配置web.xml) 4. 创建业务逻辑控制器(Action) 5. 创建视图页面 6. 创建struts.xml配置Action 7. 部署和运行struts2项目
struts1.x apps 所有最新的Struts1.x项目应用 struts1.x apps 所有最新的Struts1.x项目应用 欢迎大家下载。。。
struts的小项目,用ibatis连接的Oracle数据库。增删改查的基本功能都有!
Struts开发技巧Struts开发技巧Struts开发技巧Struts开发技巧Struts开发技巧Struts开发技巧Struts开发技巧Struts开发技巧
struts1和struts2项目
这本册子不是大公司、大名气作者所著,却是本人从事Struts2项目开发二多年来的一些经验分享,因此书中的语言也不是很规范,都是我自己口语化写的,也希望大家不要太挑刺,水平有限,仅供大家参考,那些经验或许对你...
这个是是用struts开发的项目,内有完整的代码,包括数据库。
这是Struts1.x+Spring+Hibernate开发实例,只要配置一下数据库就可以运行
struts开发入门与项目实践光盘源码 里面有几个struts项目案例
struts1环境开发需要的加包,本压缩包包含了lib和apps两个文件,apps下的示例项目可以用winrar直接解压