结果:我定义了全局错误拦截器,也指定了界面,也拦截到了错误。但是就是不能跳转到我的页面
定义全局拦截器
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="struts-iperson" extends="struts-default">
<interceptors>
<interceptor name="checkLogin"
class="com.iperson.common.CheckLoginInterceptor" />
<interceptor name="checkException"
class="com.iperson.common.ExceptionInterceptor" />
<!-- check user is login -->
<interceptor-stack name="ipersonCheckLogin">
<interceptor-ref name="checkException" />
<interceptor-ref name="checkLogin" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="ipersonCheckLogin" />
<global-results>
<result name="login" type="redirectAction">login</result>
<result name="error">/WEB-INF/global/error404.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="com.iperson.common.SystemException" result="error"/>
</global-exception-mappings>
</package>
</struts>
---------拦截器-------ExceptionInterceptor.java---------------------
package com.iperson.common;import java.io.IOException;
import java.sql.SQLException;import org.springframework.dao.DataAccessException;import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.interceptor.PreResultListener;public class ExceptionInterceptor extends AbstractInterceptor { private static final long serialVersionUID = 1L;
@Override
public String intercept(ActionInvocation arg0) throws Exception {
String result = ""; try {
result = arg0.invoke();
} catch (DataAccessException ex) { throw new SystemException("数据库操作失败!"); } catch (NullPointerException ex) { throw new SystemException("空指针,调用了未经初始化或者是不存在的对象!"); } catch (IOException ex) { throw new SystemException("IO读写异常!"); } catch (ClassNotFoundException ex) { throw new SystemException("指定的类不存在!"); } catch (ArithmeticException ex) { throw new SystemException("数学运算异常!"); } catch (ArrayIndexOutOfBoundsException ex) { throw new SystemException("数组下标越界!"); } catch (IllegalArgumentException ex) { throw new SystemException("调用方法的参数错误!"); } catch (ClassCastException ex) { throw new SystemException("类型强制转换错误!"); } catch (SecurityException ex) { throw new SystemException("违背安全原则异常!"); } catch (SQLException ex) { throw new SystemException("操作数据库异常!"); } catch (NoSuchMethodError ex) { throw new SystemException("调用了未定义的方法!"); } catch (InternalError ex) { throw new SystemException("Java虚拟机发生了内部错误!"); } catch (Exception ex) { throw new SystemException("程序内部错误,操作失败!"); } return result;
}}
----------最后相应类--------SystemException.java----------------------------
package com.iperson.common;public class SystemException extends RuntimeException { private static final long serialVersionUID = 1L;
public SystemException(String systemMessage){
super(createSystemMessage(systemMessage));
} public SystemException(Throwable throwable){ super(throwable); } public SystemException(Throwable throwable, String frdMessage){ super(throwable); } private static String createSystemMessage(String msgBody){
String prefixString = "抱歉";
String suffixStr="请稍后再试或者与管理员联系";
StringBuffer sbMsg = new StringBuffer();
sbMsg.append(prefixString);
sbMsg.append(msgBody);
sbMsg.append(suffixStr);
return sbMsg.toString(); }
}
------------------上面和网上的例子一模一样的。但是我的去不能跳转。不知道为什么Struts
定义全局拦截器
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="struts-iperson" extends="struts-default">
<interceptors>
<interceptor name="checkLogin"
class="com.iperson.common.CheckLoginInterceptor" />
<interceptor name="checkException"
class="com.iperson.common.ExceptionInterceptor" />
<!-- check user is login -->
<interceptor-stack name="ipersonCheckLogin">
<interceptor-ref name="checkException" />
<interceptor-ref name="checkLogin" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="ipersonCheckLogin" />
<global-results>
<result name="login" type="redirectAction">login</result>
<result name="error">/WEB-INF/global/error404.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="com.iperson.common.SystemException" result="error"/>
</global-exception-mappings>
</package>
</struts>
---------拦截器-------ExceptionInterceptor.java---------------------
package com.iperson.common;import java.io.IOException;
import java.sql.SQLException;import org.springframework.dao.DataAccessException;import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.interceptor.PreResultListener;public class ExceptionInterceptor extends AbstractInterceptor { private static final long serialVersionUID = 1L;
@Override
public String intercept(ActionInvocation arg0) throws Exception {
String result = ""; try {
result = arg0.invoke();
} catch (DataAccessException ex) { throw new SystemException("数据库操作失败!"); } catch (NullPointerException ex) { throw new SystemException("空指针,调用了未经初始化或者是不存在的对象!"); } catch (IOException ex) { throw new SystemException("IO读写异常!"); } catch (ClassNotFoundException ex) { throw new SystemException("指定的类不存在!"); } catch (ArithmeticException ex) { throw new SystemException("数学运算异常!"); } catch (ArrayIndexOutOfBoundsException ex) { throw new SystemException("数组下标越界!"); } catch (IllegalArgumentException ex) { throw new SystemException("调用方法的参数错误!"); } catch (ClassCastException ex) { throw new SystemException("类型强制转换错误!"); } catch (SecurityException ex) { throw new SystemException("违背安全原则异常!"); } catch (SQLException ex) { throw new SystemException("操作数据库异常!"); } catch (NoSuchMethodError ex) { throw new SystemException("调用了未定义的方法!"); } catch (InternalError ex) { throw new SystemException("Java虚拟机发生了内部错误!"); } catch (Exception ex) { throw new SystemException("程序内部错误,操作失败!"); } return result;
}}
----------最后相应类--------SystemException.java----------------------------
package com.iperson.common;public class SystemException extends RuntimeException { private static final long serialVersionUID = 1L;
public SystemException(String systemMessage){
super(createSystemMessage(systemMessage));
} public SystemException(Throwable throwable){ super(throwable); } public SystemException(Throwable throwable, String frdMessage){ super(throwable); } private static String createSystemMessage(String msgBody){
String prefixString = "抱歉";
String suffixStr="请稍后再试或者与管理员联系";
StringBuffer sbMsg = new StringBuffer();
sbMsg.append(prefixString);
sbMsg.append(msgBody);
sbMsg.append(suffixStr);
return sbMsg.toString(); }
}
------------------上面和网上的例子一模一样的。但是我的去不能跳转。不知道为什么Struts
解决方案 »
- jdbc 执行 on duplicate key update 时出错,非空字段需要个缺省值
- tomcat中类放在哪
- asm-2.2.3.jar冲突,怎么解决呢?
- myeclipse配置struts出现的一个问题
- 求教spring的配置问题,请多指教。
- 大家来看看,100分啊,求教weblogic和ejb的问题
- WSDL2JAVA 生成客户端 调用.net的web Service的问题
- properties.out 问题
- 使用Socket来判断域名是否可用的问题!
- 在Struts中的html标记<html:select>和<html:option>如何使用?
- Spring 3 + Hibernate4 事务不成功
- Hibernate修改问题
你使用这个默认拦截器的时候,系统默认的拦截器就会被覆盖,把系统默认的拦截器放在ipersonCheckLogin拦截器中就可以了。
它其实是拦截了的,就是不跳转。很多天了一直也不知道,方便的话能清楚告诉么。Thks