struts1 声明式异常

所谓struts声明式异常,就是自己不try catch了,交给struts框架去处理。

步骤:

1.定义一个自己的异常,比如AppException,让它继承RunTimeException,并实现父类的所有构造方法。

比如:

package com.germmyedu.util;

public class AppException extends RuntimeException {

	private static final long serialVersionUID = 1L;

	public AppException() {
		super();
		// TODO Auto-generated constructor stub
	}

	public AppException(String message, Throwable cause) {
		super(message, cause);
		// TODO Auto-generated constructor stub
	}

	public AppException(String message) {
		super(message);
		// TODO Auto-generated constructor stub
	}

	public AppException(Throwable cause) {
		super(cause);
		// TODO Auto-generated constructor stub
	}
	
	
}

2.在struts-config.xml的action中定义一个exception或者定义一个global的exception。

这个exception对应的类就是上面定义的AppException

这个标签的含义是指当程序中碰到AppException时,就转到相应的页面。

<global-exceptions>
		<exception key="errors.detail"
			type="com.germmyedu.util.AppException" path="/WEB-INF/jsp/error.jsp" />
	</global-exceptions>

3.在error.jsp页面用<html:errors />将异常信息显示出来。

注意:<html:errors />只读error key,而<html:message/>是读的message key.

4.此时如何将显示的异常信息变成红色?

需要用到国际化资源文件中的系统属性,如下:

errors.header=<UL>
errors.prefix=<LI><font color='red'>
errors.suffix=</font></LI>
errors.footer=</UL>
errors.detail={0}

 

猜你喜欢

转载自wandejun1012.iteye.com/blog/2028660