📄 weblayerexceptionhandler.java
字号:
package com.softdevelopstudio.exception;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionForward;
import javax.servlet.ServletException;
import org.apache.struts.config.ExceptionConfig;
import org.apache.struts.action.ActionMapping;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import java.io.*;
import java.util.Calendar;
import java.util.Date;
import java.text.SimpleDateFormat;
/**
*
* <p>Title:web层业务异常处理类. </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2008</p>
*
* <p>Company: </p>
*
* @author lzx
* @version 1.0
*/
public class WebLayerExceptionHandler extends org.apache.struts.action.
ExceptionHandler {
public ActionForward execute(Exception ex,
ExceptionConfig ae,
ActionMapping mapping,
ActionForm formInstance,
HttpServletRequest request,
HttpServletResponse response) throws
ServletException {
BizException biz = null;
if (ex instanceof com.softdevelopstudio.exception.BizException) {
biz = (BizException) ex;
}
if(biz!=null)
request.setAttribute("msg", biz.toMyString());
//如果是业务异常,转到业务提示页面.
System.out.println("ex = " + ex.toString());
if (ex instanceof com.softdevelopstudio.exception.BizException) {
return mapping.findForward("bizReminder");
}
Calendar calCurrent = Calendar.getInstance();
String path = request.getRealPath("/");
path += "error/";
if (!(new File(path).isDirectory())) {
new File(path).mkdir();
}
String fileName = getFileName(calCurrent);
String errorId = getId(calCurrent);
try {
PrintStream ps = new PrintStream(new FileOutputStream(path +
fileName, true));
ps.println("ErrorID : " + errorId);
ex.printStackTrace(ps);
ps.println("");
ps.println("");
ps.println("");
} catch (FileNotFoundException ex1) {
ex1.printStackTrace();
}
request.setAttribute("errorId", errorId);
//如果是系统异常,转到系统错误业面
return mapping.findForward("error");
}
private String getId(Calendar cal) {
Date date = cal.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmssSSS");
String Id = sdf.format(date);
Id += Math.round(Math.random() * 10);
System.out.println("ID = " + Id);
return Id;
}
private String getFileName(Calendar cal) {
Date date = cal.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String today = sdf.format(date) + ".txt";
System.out.println("FileName = " + today);
return today;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -