`
newslxw
  • 浏览: 208282 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

struts异常处理

阅读更多
struts.xml配置
  <!DOCTYPE struts PUBLIC
  "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  "http://struts.apache.org/dtds/struts-2.0.dtd">
  <struts>
  <!-- 对默认拦截器的引入 -->
  <include file="struts-default.xml" />
  <package name="eticket" extends="struts-default">
  <!-- 全局异常映射 -->
  <global-exception-mappings>
  <exception-mapping result="error" exception="java.lang.Exception">
  </exception-mapping>
  </global-exception-mappings>
  <action name="airTicketAction" class="AirTicketAction">
  <result>airticket/airticket.jsp</result>
  <result name="detail">airticket/airticketdetail.jsp</result>
  <result name="order">airticket/airticketorder.jsp</result>
  <result name="error">error.jsp</result>
  <!-- 局部异常映射 -->
  <exception-mapping result="nullPoint" exception="java.lang.NullPointerException"></exception-mapping>
  <result name="nullPoint">error.jsp</result>
  </action>
  </package>
  </struts>


error.jsp
  <%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"%>
  <%@ taglib prefix="s" uri="/struts-tags"%>
  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  <html>
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title><s:text name="login.title"></s:text></title>
  </head>
  <body>
  <h1>出错了!</h1>
  <s:property value="exception.message" />
  <s:property value="exceptionStack" />
  </body>
  </html>


示例
  import com.opensymphony.xwork2.ActionSupport;
  @SuppressWarnings("serial")
  public class Test extends ActionSupport {
  @Override
  public String execute() throws Exception {
  // TODO 自动生成方法存根
  //产生一个除数为0的异常
  int i = 1/0;
  return SUCCESS;
  }
  }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics