1.我写这段代码实现的愿望,ssh验证提示使用的不是很顺手,可能是我用的不太熟悉吧,最后我考虑了一下自己的SSH消息提示信息。主要是通过struts2中的自定义异常与strut2的拦截器实现消息提示。
2.拦截器中分为两种异常一种为系统中已经定义好的异常,处理方式为直接转跳到一个固定页面进行友好提示,第二种方式为自定义系统中的消息提示。如下提出代码:
自定义异常类:
package com.finance.exception; import com.finance.util.ApplicationContextUtils; import com.finance.util.PropertiesUtils; /** * 请求运行时异常 * * @author guyw * * 2014-3-9 */ public class AppContextException extends RuntimeException{ /** * 根据状态判断错误 * @param sate 错误状态 * @param message 错误信息 */ public AppContextException(String state,String message) { // PropertiesUtils propertiesUtils=(PropertiesUtils) ApplicationContextUtils.getApplicationContext().getBean("propertiesUtils"); // String errorMessage=(String) propertiesUtils.getProperties().get(state); super(state+":"+message); } /** * 根据状态判断错误 * @param sate 错误状态 */ public AppContextException(String state) { // PropertiesUtils propertiesUtils=(PropertiesUtils) ApplicationContextUtils.getApplicationContext().getBean("propertiesUtils"); // String errorMessage=(String)propertiesUtils.getProperties().get(state); super(state); } }
自定义:拦截器
package com.finance.intercepter;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import org.springframework.dao.DataAccessException;
import com.finance.exception.AppContextException;
import com.finance.util.ApplicationContextUtils;
import com.finance.util.PropertiesUtils;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
/**
*
* 全局异常拦截器
*
* @author guyw
*
* 2014-3-9
*/
public class AppContextExceptionIntercepter implements Interceptor {
private static final long serialVersionUID = 1L;
private String errorMessage;
@Override
public void destroy() {
}
@Override
public void init() {
}
@Override
public String intercept(ActionInvocation invocation) {
ActionContext ctx = invocation.getInvocationContext();
HttpServletRequest request = (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST);
String result=null;
try {
result=invocation.invoke();
} catch (DataAccessException ex) {
throw new AppContextException("10001", "数据库操作失败!");
} catch (NullPointerException ex) {
throw new AppContextException("10001", "调用了未经初始化的对象或者是不存在的对象!");
} catch (IOException ex) {
throw new AppContextException("10001", "IO异常!");
} catch (ClassNotFoundException ex) {
throw new AppContextException("10001", "指定的类不存在!");
} catch (ArithmeticException ex) {
throw new AppContextException("10001", "数学运算异常!");
} catch (ArrayIndexOutOfBoundsException ex) {
throw new AppContextException("10001", "数组下标越界!");
} catch (IllegalArgumentException ex) {
throw new AppContextException("10001", "方法的参数错误!");
} catch (ClassCastException ex) {
throw new AppContextException("10001", "类型强制转换错误!");
} catch (SecurityException ex) {
throw new AppContextException("10001", "违背安全原则异常!");
} catch (SQLException ex) {
throw new AppContextException("10001", "操作数据库异常!");
} catch (NoSuchMethodError ex) {
throw new AppContextException("10001", "方法末找到异常!");
} catch (InternalError ex) {
throw new AppContextException("10001", "Java虚拟机发生了内部错误");
}
catch (AppContextException ex) {
PropertiesUtils propertiesUtils = (PropertiesUtils) ApplicationContextUtils.getApplicationContext().getBean("propertiesUtils");
String state=ex.toString();
String message=(String) propertiesUtils.getProperties().get(state);
if(errorMessage!=null){
errorMessage=state+","+message;
}else{
errorMessage=state;
}
request.setAttribute("errorMessage",errorMessage);
return "success";
}
catch (Exception ex) {
throw new AppContextException("10001", "程序内部错误,操作失败!");
}
return "exceptionErrorMessage";
}
}
如上红色代码 return "success"; 此处有一个疑问 如果在Action中抛出异常后拦截器捕获后返回结果集为 404错误,不知道改则么处理:错误页面代码:
HTTP Status 404 - result 'null' not found
type Status report
message result 'null' not found
description The requested resource (result 'null' not found) is not available.
Apache Tomcat/6.0.13
PropertiesUtils 为一些自定义的消息固定提示信息key-value 比如:10001=登录失败!。
使用方式为 此处有一个上传功能:
/** * 保存合作机构 * @return */ public String saveOrg(){ if(true){ throw new AppContextException("10001", "上传文件错误!"); } return SUCCESS; }
这样就能构建全局的消息提示信息。新手发帖赶紧喷吧。
请教问题:如上红色代码 return "success"; 此处有一个疑问 如果在Action中抛出异常后拦截器捕获后返回结果集为 404错误,不知道改则么处理,是不是如果在Action抛出异常后,在拦截器中返回的结果集与Action的拦截器就无关了?
相关推荐
Struts2拦截器(Interceptor) Struts2拦截器(Interceptor)
Struts2拦截器.ppt Struts2拦截器.ppt Struts2拦截器.ppt
struts2,Interceptor struts2拦截器实例,两套实例,一套是针对单个的action配置的,另一套实例是针对全局的action配置的拦截器interceptor
Struts2拦截器源程序 Struts2拦截器源程序 Struts2拦截器源程序 Struts2拦截器源程序 Struts2拦截器源程序
综合使用servlet filter与struts2 interceptor对权限进行验证。
关于struts2 拦截器的详细的解释及其在开发中对struts2的拦截器的应用和拦截器在框架中的作用.
struts2拦截器的使用说明文档,配置说明文档。可以看一下
NULL 博文链接:https://cmexico.iteye.com/blog/621208
Struts2拦截器实现权限控制demo,我感觉对初学者还是有一定帮助的
JSP 开发之Struts2内建自定义拦截器.docx
struts2拦截器应用小例子,与大家共分享
其中,init和destroy方法会在程序开始和结束时各执行一遍,不管使用了该拦截器与否,只要在struts.xml中声明了该Struts2拦截器就会被执行。 intercept方法就是拦截的主体了,每次拦截器生效时都会执行其中的逻辑。
基于struts2的拦截器测试,实现了页面的跳转,中间过程的拦截
使用struts的拦截器拦截一些敏感性的词或一些非法或不文秘的言论
利用登陆拦截器 源码 演示struts2自定义拦截器的使用和配置。
strut2 配置自定义拦截器 包括struts2.xml 和 action对应的xml 以及拦截器 可能有点简单 但是基本配置都有
Struts2拦截器是在访问某个Action或Action的某个方法,字段之前或之后实施拦截,并且Struts2拦截器是可插拔的,拦截器是AOP的一种实现.
struts1.2拦截器使用插件
Struts2全局、局部自定义拦截器源码。