exception-struts1

exception---struts1
关于struts里的异常处理,用到动态actionform,无需写代码,只需在配置文件里配置即可

myeclipse里新建web项目ch04exception,添加struts特性

新建action,用工具新建,会自动在配置文件里配置action的信息

package action;
/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import service.UserNameService;

/** 
 * MyEclipse Struts
 * Creation date: 08-04-2010
 * 
 * XDoclet definition:
 * @struts.action
 */
public class SampleAction extends Action {
	/*
	 * Generated Methods
	 */

	/** 
	 * Method execute
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception{
		// TODO Auto-generated method stub
		UserNameService s = new UserNameService();       //action调用业务逻辑层
		s.usernamelogic();         
		return null;
	}
}


自定义异常类:

package selfexception;

/*
 * 自定义异常类
 */
public class UserNameException extends Exception {

	public UserNameException() {
		super();
	}

	public UserNameException(String s) {
		super(s);
	}

	public UserNameException(Exception ex) {
		super(ex);
	}

	public UserNameException(Throwable t) {
		super(t);
	}

	public UserNameException(String s, Throwable t) {
		super(s, t);
	}
}


简单的业务逻辑:

package service;

import selfexception.UserNameException;
/*
 * 业务逻辑层
 */
public class UserNameService {
	public void usernamelogic() throws UserNameException {
		throw new UserNameException("username");
	}
}


资源文件里
UserNameException = \u7528\u6237\u540d\u65e0\u6cd5\u88ab\u786e\u8ba4


struts配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>
  <data-sources />
  <form-beans >
  <!-- 动态actionform,不需要定义,完全由配置文件生成 -->
    <form-bean name="dynamicActionForm" type="org.apache.struts.action.DynaActionForm">
      <form-property name="password" type="java.lang.String" />
      <form-property name="sex" type="java.lang.String" />
      <form-property name="userName" type="java.lang.String" />
      <form-property name="age" type="java.lang.String" />
      <form-property name="email" type="java.lang.String" />
    </form-bean>

  </form-beans>

  <global-exceptions />
  <global-forwards />
  <action-mappings >
    <action
      attribute="dynamicActionForm"
      input="/exception.jsp"
      name="dynamicActionForm"
      path="/sampleAction"
      scope="request"
      type="action.SampleAction"
      validate="false">
      <exception
        key="UserNameException"
        path="/exception.jsp"
        type="selfexception.UserNameException" />
    </action>

  </action-mappings>

  <message-resources parameter="ApplicationResources" />
</struts-config>


jsp页面:exception.jsp

<%@ page language="java" contentType = "text/html;charset=GBK"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>STRUTS异常实例</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>
  <!-- 取得报错信息,从资源文件中取得 -->
    <html:errors/><p/>
    <html:form action="sampleAction.do">
    UserName:  <html:text property="userName"></html:text><br/>
    Password:  <html:password property="password"></html:password><br/>
    sex:       <html:select property="sex">
    <html:option key="F" value="F" />
    <html:option key="M" value="M" /></html:select><br/>
    Age:         <html:text property="age"></html:text><br/>
    email:       <html:text property="email"></html:text><br/>
    <html:submit></html:submit>
    <html:reset></html:reset>
    </html:form>
  </body>
</html>

部署到tomcat,启动tomcat,输入http://localhost:8080/ch04exception/exception.jsp执行结果如下图:

exception-struts1

源码下载见附件