Struts声明式异常
编程式异常 :
即我们在Action中调用业务逻辑层对象的方法时,用try{}catch的方式来截获异常之后,手工对异常进行处理。在编程式异常处理的时候,我们可以使用struts的消息处理机制来对这些异常信息进行处理。
编程式异常的处理过程:1,截获异常,2创建相应的异常信息,3传递异常信息,4转向相应的页面处理异常。
声明式异常(自动处理的异常):
声明式异常(自动处理的异常)
* 在struts-config.xml文件中配置<exeception/>标签
* 理解局部和全局exception
* 注意局部<exception/>标签需要配置到<forward/>标签的前面,详见dtd中的约束
<exeception/>标签中的属性说明:
* key:指异常信息对应的国际化消息文本,这个key值需要在国际化资源文件中定义
* type: 处理那种异常(异常类的完整路径),
* path: 定义一但出现异常,需要转向那个页面,如果不定义path,
默认情况下将使用<action>标签中input属性对应的页面 ,path的优先级高。
* scope:可以取值request和session,默认为request
* handler:异常的处理类,struts默认采用org.apache.struts.action.ExceptionHandler,
如果做个性化的异常处理可以继承此类覆写相应的方法
首先在struts-config.xml中做配置:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<form-beans>
<form-bean name="loginForm" type="com.mine.struts.LoginActionForm"/>
</form-beans>
<global-exceptions>//用于配置全局exception,当发生当前配的异常时,都会在这里转
<!--
<exception key="user.not.found" type="com.mine.struts.UserNotFoundException" path="/login_error.jsp"/>
<exception key="user.password.error" type="com.mine.struts.PasswordErrorException" path="/login_error.jsp"/>
-->
<!--
<exception key="user.not.found" type="com.mine.struts.UserNotFoundException" handler="org.apache.struts.action.ExceptionHandler"/>
<exception key="user.password.error" type="com.mine.struts.PasswordErrorException" handler="org.apache.struts.action.ExceptionHandler"/>
-->
<!--
<exception key="error.exception" type="com.mine.struts.ErrorCodeException" handler="com.bjsxt.struts.ErrorCodeExceptionHandler"/>
-->
<!--
<exception key="error.exception" type="com.mine.struts.AppException" handler="com.bjsxt.struts.AppExceptionHandler"/>
-->
<exception key="error.exception" type="com.mine.struts.AppException"/>
</global-exceptions>
<action-mappings>
<action path="/login"
type="com.mine.struts.LoginAction"
name="loginForm"
scope="request"
validate="false"
input="/login.jsp"
>
<!-- //异常的配置使用exception标签。这个标签的配置要在forward标签的前面配置,这个是由dtd决定的,当exception在这里配置时,只适合当前的exception,可以配置全局的exception
<exception key="user.not.found" type="com.mine.struts.UserNotFoundException" path="/login_error.jsp"/>
<exception key="user.password.error" type="com.mine.struts.PasswordErrorException" path="/login_error.jsp"/>
-->
<forward name="success" path="/login_success.jsp"/>
<forward name="error" path="/login.jsp"/>
</action>
<action path="/changelang"
type="com.mine.struts.ChangeLanguageAction"
>
<forward name="index" path="/index.jsp"/>
</action>
<action path="/login1"
type="org.apache.struts.actions.ForwardAction"
parameter="/login.jsp"
></action>
</action-mappings>
<message-resources parameter="res.MessageResources" />
</struts-config>
在exception 标签里有个key属性,这个是国际化消息文本,用于当发生异常的时候的提示信息:
# -- standard errors --
errors.header=<UL>
errors.prefix=<LI><font color="red">
errors.suffix=</font></LI>
errors.footer=</UL>
user.title=用户登录
user.username=用户
user.password=密码
user.button.login=登录
user.login.success={0},登录成功
user.not.found=用户不能找到,用户名称=[{0}] //即显示用户不能找到
user.password.error=密码错误
user.login.error=登录失败
error.exception={0}
配置完上面俩个之后,配置当发生异常时,转向的处相应的页面的这个配置,在action标签里有一个属性叫input,通过这个来配置,转到这个属性指向的页面:
input="/login.jsp"
转到这个页面之后,就在这里处理这个异常,由于这个异常信息是存在errorkey里,在这个页面,取出这个异常有俩中方法:
一种是通过<html:errors/>去取,也可以通过<html:messages>标签去取。
<html:messages id="msg" property="error1">
<bean:write name="msg"/>
</html:messages>
http://xxp3369-126-com.javaeye.com/blog/314617
1、编程式异常
* 截获异常
* 创建相应的异常消息
* 传递异常消息
* 转向相应的页面处理异常
2、声明式异常(自动处理的异常)
* 在struts-config.xml文件中配置<exeception/>标签
* 理解局部和全局exception
* 注意局部<exception/>标签需要配置到<forward/>标签的前面,详见dtd中的约束
<exeception/>标签中的属性说明:
* key:指异常信息对应的国际化消息文本,这个key值需要在国际化资源文件中定义
* type: 处理那种异常
* path: 定义一但出现异常,需要转向那个页面,如果不定义path,
默认情况下将使用<action>标签中input属性对应的页面
* scope:可以取值request和session,默认为request
* handler:异常的处理类,struts默认采用org.apache.struts.action.ExceptionHandler,
如果做个性化的异常处理可以继承此类覆写相应的方法
参见:ErrorCodeExceptionHandler.java和AppExceptionHandler.java
struts-config.xml
- <?xml version="1.0" encoding="ISO-8859-1" ?>
- <!DOCTYPE struts-config PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
- "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
- <struts-config>
- <form-beans>
- <form-bean name="loginForm" type="com.bjsxt.struts.LoginActionForm"/>
- </form-beans>
- <global-exceptions>
- <!--
- <exception key="user.not.found" type="com.bjsxt.struts.UserNotFoundException" path="/login_error.jsp"/>
- <exception key="user.password.error" type="com.bjsxt.struts.PasswordErrorException" path="/login_error.jsp"/>
- -->
- <!--
- <exception key="user.not.found" type="com.bjsxt.struts.UserNotFoundException" handler="org.apache.struts.action.ExceptionHandler"/>
- <exception key="user.password.error" type="com.bjsxt.struts.PasswordErrorException" handler="org.apache.struts.action.ExceptionHandler"/>
- -->
- <!--
- <exception key="error.exception" type="com.bjsxt.struts.ErrorCodeException" handler="com.bjsxt.struts.ErrorCodeExceptionHandler"/>
- -->
- <!--
- <exception key="error.exception" type="com.bjsxt.struts.AppException" handler="com.bjsxt.struts.AppExceptionHandler"/>
- -->
- <exception key="error.exception" type="com.bjsxt.struts.AppException"/>
- </global-exceptions>
- <action-mappings>
- <action path="/login"
- type="com.bjsxt.struts.LoginAction"
- name="loginForm"
- scope="request"
- validate="false"
- input="/login.jsp"
- >
- <!--
- <exception key="user.not.found" type="com.bjsxt.struts.UserNotFoundException" path="/login_error.jsp"/>
- <exception key="user.password.error" type="com.bjsxt.struts.PasswordErrorException" path="/login_error.jsp"/>
- -->
- <forward name="success" path="/login_success.jsp"/>
- <forward name="error" path="/login.jsp"/>
- </action>
- <action path="/changelang"
- type="com.bjsxt.struts.ChangeLanguageAction"
- >
- <forward name="index" path="/index.jsp"/>
- </action>
- <action path="/login1"
- type="org.apache.struts.actions.ForwardAction"
- parameter="/login.jsp"
- ></action>
- </action-mappings>
- <message-resources parameter="res.MessageResources" />
- </struts-config>
login.jsp
- <%@ page language="java" contentType="text/html; charset=GB18030"
- pageEncoding="GB18030"%>
- <%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean"%>
- <%@ taglib prefix="html" uri="http://struts.apache.org/tags-html" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
- <title><bean:message key="user.title"/></title>
- </head>
- <body>
- <h1><bean:message key="user.title"/></h1>
- <hr>
- <!--
- <font color="red">
- <html:messages id="msg" property="error1">
- <bean:write name="msg"/>
- </html:messages>
- </font>
- <font color="blue">
- <html:messages id="msg" property="error2">
- <bean:write name="msg"/>
- </html:messages>
- </font>
- -->
- <html:errors/>
- <form action="login.do" method="post">
- <bean:message key="user.username"/>:<input type="text" name="username"><br>
- <bean:message key="user.password"/>:<input type="password" name="password"><br>
- <input type="submit" value="<bean:message key="user.button.login"/>">
- </form>
- </body>
- </html>
login_success.jsp
pageEncoding="GB18030"%>
<%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean"%>
<%@ taglib prefix="html" uri="http://struts.apache.org/tags-html" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title></title>
</head>
<body>
<html:messages id="msg" message="true" property="loginSuccess1">
<bean:write name="msg"/>
</html:messages>
</body>
</html>
login_error.jsp
- <%@ page language="java" contentType="text/html; charset=GB18030"
- pageEncoding="GB18030"%>
- <%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean"%>
- <%@ taglib prefix="html" uri="http://struts.apache.org/tags-html" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
- <title></title>
- </head>
- <body>
- <font color="red">
- <!--
- <li>
- <html:messages id="msg" property="error1">
- <bean:write name="msg"/>
- </html:messages>
- </li>
- </font>
- <font color="blue">
- <li>
- <html:messages id="msg" property="error2">
- <bean:write name="msg"/>
- </html:messages>
- </li>
- </font>
- -->
- <html:errors/>
- </body>
- </html>
login_jstl.jsp
- <%@ page language="java" contentType="text/html; charset=GB18030"
- pageEncoding="GB18030"%>
- <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
- <fmt:setLocale value="${header['accept-language']}"/>
- <fmt:setBundle basename="res.MessageResources"/>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
- <title><fmt:message key="user.title"/></title>
- </head>
- <body>
- <h1><fmt:message key="user.title"/></h1>
- <hr>
- <form action="login.do" method="post">
- <fmt:message key="user.username"/>:<input type="text" name="username"><br>
- <fmt:message key="user.password"/>:<input type="password" name="password"><br>
- <input type="submit" value="<fmt:message key="user.button.login"/>">
- </form>
- </body>
- </html>
UserManager.java
- package com.bjsxt.struts;
- public class UserManager {
- private static UserManager instance = new UserManager();
- private UserManager() {}
- public static UserManager getInstance() {
- return instance;
- }
- // public void login(String username, String password) {
- // if (!"admin".equals(username)) {
- // throw new UserNotFoundException(username);
- // }
- // if (!"admin".equals(password)) {
- // throw new PasswordErrorException();
- // }
- // }
- // public void login(String username, String password) {
- // if (!"admin".equals(username)) {
- // throw new ErrorCodeException("user.not.found", username);
- // }
- // if (!"admin".equals(password)) {
- // throw new ErrorCodeException("user.password.error");
- // }
- // }
- public void login(String username, String password) {
- if (!"admin".equals(username)) {
- throw new AppException("用户不能找到,用户=【" + username + "】");
- }
- if (!"admin".equals(password)) {
- throw new AppException("密码不正确!");
- }
- }
- }
AppException.java
- package com.bjsxt.struts;
- public class AppException extends RuntimeException {
- public AppException(String msg) {
- super(msg);
- }
- }
AppExceptionHandler.java
- package com.bjsxt.struts;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.struts.Globals;
- import org.apache.struts.action.ActionForm;
- import org.apache.struts.action.ActionForward;
- import org.apache.struts.action.ActionMapping;
- import org.apache.struts.action.ActionMessage;
- import org.apache.struts.action.ExceptionHandler;
- import org.apache.struts.config.ExceptionConfig;
- import org.apache.struts.util.ModuleException;
- public class AppExceptionHandler extends ExceptionHandler {
- public ActionForward execute(
- Exception ex,
- ExceptionConfig ae,
- ActionMapping mapping,
- ActionForm formInstance,
- HttpServletRequest request,
- HttpServletResponse response)
- throws ServletException {
- if (!(ex instanceof AppException)) {
- return super.execute(ex, ae, mapping, formInstance, request, response);
- }
- ActionForward forward = null;
- ActionMessage error = null;
- String property = null;
- // Build the forward from the exception mapping if it exists
- // or from the form input
- if (ae.getPath() != null) {
- forward = new ActionForward(ae.getPath());
- } else {
- forward = mapping.getInputForward();
- }
- // Figure out the error
- if (ex instanceof ModuleException) {
- error = ((ModuleException) ex).getActionMessage();
- property = ((ModuleException) ex).getProperty();
- } else {
- AppException appe = (AppException)ex;
- error = new ActionMessage(ae.getKey(), appe.getMessage());
- property = error.getKey();
- //error = new ActionMessage(ae.getKey(), ex.getMessage());
- //property = error.getKey();
- }
- this.logException(ex);
- // Store the exception
- request.setAttribute(Globals.EXCEPTION_KEY, ex);
- this.storeException(request, property, error, forward, ae.getScope());
- return forward;
- }
- }