<struts-config>
//对于这里的标签是有顺序的,如<global-forward><global-exception>不能放在form-beans的前面
<form-beans>
<form-bean></form-bean>
</form-beans>
//全局异常处理,要放在global-forward的前面
<global-exceptions>
<exception type="异常的类型" handler="定义处理类" path="转到哪个页面" key="资源文件中的key"></exception>
....
</global-exceptions>
//全局的分派
<global-forward>
<forward name="" path="/">
...
</global-forward>
<action-mapping>
<action path="" type="" name="" atrtibute=""input="" validate="">
<exception type="" handler=""path="" key=""/>
<forward name="" path=""/>
...
</action>
</action-mapping>
</struts-cofing>
|-- 要注意的是,关键字key一般是在资源文件中的键值,要注意配置资源文件
异常处理:
|-- 全局异常:
全局异常放在<global-exceptions></global-exceptions>里面
<global-exceptions>
<exception type="异常的类型" handler="处理类" path="去哪里" key="异常信息的键,值在ApplicationResources.properties的资源文件中"></exception>
....
</global-exceptions>
====================
<global-exceptions>
<exceptiontype="java.lang.Exception"handler="hwt.Exception.MyException"path="/error.jsp"key="myException"/>
</global-exceptions>
|-- 局部异常:
局部的异常放在<action>里面,当这个action里面的execute方法发生了异常的话,就会交个一个异常处理类来处理
<action path="/login"parameter="method"type="hwt.action.LoginAction">
<exception type="java.lang.Exception"handler="hwt.Exception.MyException"path="/error.jsp"key="myException"/>
<forward name="loginPage" path="/login.jsp"></forward>
</action>
在实现类中:
public class MyExceptionextends ExceptionHandler {
@Override
public ActionForward execute(Exception arg0, ExceptionConfig ec,
ActionMapping arg2, ActionForm arg3, HttpServletRequestarg4,
HttpServletResponse arg5) throws ServletException {
String path = ec.getPath();
return new ActionForward(path);
}
}