编程式异常 -声明式异常

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


[quote]<%@ 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>
<html:messages id="msg" message="true" property="loginSuccess1">
<bean:write name="msg"/>
</html:messages>
</body>
</html>[/quote]


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;

}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值