struts2之声明式异常捕捉

Struts2的异常处理机制是通过struts.xml文件中配置<exception-mapping...>元素完成的,配置该元素时,需要指定如下两个属行
[color=blue]
exception:此属性指定该异常映射所设置异常类型
result:此属性指定action出现异常时,系统返回result类型对应的逻辑视图名
[/color]
根据exception-mapping元素出现的位置的不同,异常映射又可分为如下两种
局部异常映射,将exception-mapping元素作为action元素的子元素配置
全局异常映射:将exception-mapping元素作为global-exception-mappings元素的子元素配置
全局异常映射对所有的action都有效,但局部异常映射仅对该异常所在的action内有效。如果局部异常映射和全局异常映射配置了同一个异常类型,在该action内局部异常映射会覆盖全局异常映射。
下面的应用是一个简单的登陆应用,在登陆页面输入用户名和密码后,用户提交请求将被action类处理。

login.jsp登陆页面

<%@ 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>

</head>

<body>
<form action="login.action" method="post">
<div><input type="text" name="username" id="username"></div>
<div><input type="password" name="password" id="password"></div>
<div><input type="submit" value="提交"></div>
</form>
</body>
</html>



action登陆页面

package com.exception.demo;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

public class LoginAction implements Action{

private String username;
private String password;
private String tip;

@Override
public String execute() throws Exception {
if(getUsername().equalsIgnoreCase("user")){
throw new Exception("Exception异常");
}
if(getUsername().equalsIgnoreCase("sql")){
throw new java.sql.SQLException("用户名不能为SQL");
}
if(getUsername().equals("fuchangle")
&& getPassword().equals("admin")){
ActionContext.getContext().getSession().put("user", getUsername());
setTip("哈哈!服务器提示!");
return SUCCESS;
}
return ERROR;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getTip() {
return tip;
}

public void setTip(String tip) {
this.tip = tip;
}


}



struts.xml配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="main" extends="struts-default">
<!-- 定义全局结果映射 -->
<global-results>
<result name="sql">/exception.jsp</result>
<result name="root">/exception.jsp</result>
</global-results>

<!-- 定义全局异常映射 -->
<global-exception-mappings>
<!-- 当action中遇到SQLExeption异常时,系统将转入name为sql的结果中 -->
<exception-mapping result="sql" exception="java.sql.SQLException"/>
<!-- 当action中遇到Exception异常时,系统将转入name为root的结果中 -->
<exception-mapping result="root" exception="java.lang.Exception"/>
</global-exception-mappings>

<action name="login" class="com.exception.demo.LoginAction">
<result name="success">./welcome.jsp</result>
<result name="error">./error.jsp</result>
</action>

</package>
</struts>


详细代码见附件
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值