1、全局异常
在struts-config.xml文件下添加以下节点:
xml 代码
- <global-exceptions >
- <exception key="error" type="java.lang.Exception"
- path="/error.jsp" scope="request">
- </exception></global-exceptions>
- <global-forwards />
在资源文件下(扩展名为properties)下配置异常错误信息
error=change number wrong!
在Action文件下加入代码:
java 代码
- public ActionForward execute(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response)throws Exception {
- int num = 0;
- try{
- num = Integer.parseInt(request.getParameter("number"));
- }catch(Exception e){
- throw new Exception("error1");
- }
- request.setAttribute("num", num);
- // TODO Auto-generated method stub
- return mapping.findForward("exceptionForward");
- }
2、局部异常
和全局异常不同之处在于struts-config.xml文件不同:将exception节点加在action下
xml 代码
- <action path="/exceptionForward" type="com.action.ExceptionForwardAction" >
- <exception key="error" type="java.lang.Exception"
- path="/error.jsp" scope="request">
- </exception>
- <forward name="exceptionForward" path="/index1.jsp"></forward></action>