首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Struts2 错误处理

2012-09-09 
Struts2 异常处理Struts2 异常处理机制任何程序设计语言都是有异常处理机制,Struts2通过使用声明式异常机

Struts2 异常处理

Struts2 异常处理机制

任何程序设计语言都是有异常处理机制,Struts2通过使用声明式异常机制处理异常。

在Struts2中主要有两种异常映射方式:

1:局部异常处理 (action)

2:全局异常处理 (package)

异常主要是通过Struts2的拦截器机制处理异常和异常处理机制来处理异常,

?

Struts2中大部分都是使用拦截器实现。interceptor


输出异常信息

使用Struts2的标签来输出异常信息:

﹤s:propertyvalue="exception.message"/﹥:输出异常对象本身。

﹤s:propertyvalue="exceptionStack"/﹥: 输出异常堆栈信息。

?

?

实例

  <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>My JSP 'index.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">-->  </head>    <body>   struts2的声明式异常处理:      <a href="/exep_holder.action">异常处理</a>     </body></html>   

?

struts.xml

<package name="global-default" namespace="/" extends="struts-default"><!-- 全局异常处理 --><global-results><result name="error">/error.jsp</result></global-results><global-exception-mappings><exception-mapping  result="error" exception="java.lang.ArithmeticException"></exception-mapping></global-exception-mappings></package><package name="exeception_holder" namespace="/" extends="global-default"><action name="exep_holder" method="holder"><!--  局部异常处理  <result name="error">/error.jsp</result><exception-mapping  result="error" exception="java.lang.ArithmeticException"></exception-mapping> --></action></package>
?

? ExceptionHolderController.java

package com.struts2.action;import com.opensymphony.xwork2.ActionSupport;public class ExceptionHolderController extends ActionSupport {public String holder() throws Exception {String str_error="";int i= 100/0;if(i>0) {str_error="错误显示";}else{str_error="正确显示";}this.addFieldError("error_message", str_error);return ERROR;}}

?

error.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><%@taglib uri="/struts-tags" prefix="s" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>My JSP 'error.jsp' starting page</title>    <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">-->  </head>    <body>  <s:property value="exception.message"/><br/> <s:property value="exceptionStack"/><br/>   <s:property value="error_message"/><br/>   <s:debug></s:debug>  </body></html>
?

?

热点排行