stuts异常处理实例

  • struts2异常处理机制

struts2的框架中采用声明式异常处理机制。开发者只需要在struts.xml文件中进行配置,struts便能够处理异常,然后响应相应的视图,在Action中无须编写任何异常处理代码。

  • 配置异常处理

struts.xml中的配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
	<constant name="struts.i18n.encoding" value="UTF-8" />
	<constant name="struts.custom.i18n.resources" value="globalMessages" />
	<constant name="" value=""></constant>
	<package name="default" extends="struts-default">
		<!-- 全局异常映射 异常为sqlException,异常类型为java.sql.SQLException -->
		<global-results>
			<result name="sqlException">/sqlException.jsp</result>
		</global-results>
		<global-exception-mappings>
			<exception-mapping result="sqlException" exception="java.sql.SQLException" />
		</global-exception-mappings>
		<action name="showWords" class="action.ShowWords">
			<!-- 局部异常映射 异常类型为java.lang.Exception -->
			<exception-mapping result="myException" exception="java.lang.Exception" />
			<result name="myException">/myException.jsp</result>
		</action>
	</package>
</struts>

  • 输出异常

<s:property value="exception.message"/>
或者
  	<s:property value="exceptionStack"/>


  • 异常处理实例

1.创建register.jsp

<%@ page language="java" pageEncoding="GB2312"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>Struts 2异常处理</title>

  </head>
  
  <body>
  <center>
	<jsp:include page="index.html"/>
	<strong>用户注册</strong><br>
	<!-- 打印异常的消息 -->
	<s:property value="exception.message"/>
	<s:form action="register">
		<s:textfield name="userName" label="姓名"/>
		<s:textfield name="userAge" label="年龄"/>
		<s:submit value="提交"/>
	</s:form>
  
  </center>

  </body>
</html>

2.创建Action


RegisterException.java

package action;

import java.sql.SQLException;

import com.opensymphony.xwork2.ActionSupport;

public class RegisterException extends ActionSupport{


	/**
	 * 
	 */
	private static final long serialVersionUID = -4731851652462341159L;
	private String userName;
	private int userAge;
	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	
	
	public int getUserAge() {
		return userAge;
	}

	public void setUserAge(int userAge) {
		this.userAge = userAge;
	}
	
	@Override
	public String execute() throws Exception {
		if (userName == null || "".equals(userName)) {
			throw new Exception("姓名不能为空");
		} 
		if (userAge < 0 || userAge > 100) {
			throw new SQLException("SQL异常,年龄有问题");
		}
		if (userName.length() > 10) {
			throw new SQLException("SQL 异常,姓名太长了");
		}
		return SUCCESS;
	}
}


3配置struts.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
	<constant name="struts.i18n.encoding" value="UTF-8" />
	<constant name="struts.custom.i18n.resources" value="globalMessages" />

	<package name="default" extends="struts-default">
		<!-- 全局异常映射 异常为showException,异常类型为java.sql.SQLException -->
		<global-results>
			<result name="showException">/register.jsp</result>
		</global-results>
		<global-exception-mappings>
			<exception-mapping result="showException" exception="java.lang.Exception" />
		</global-exception-mappings>
		<action name="register" class="action.RegisterException">
			<!-- 局部异常映射 异常类型为java.lang.Exception -->
			<exception-mapping result="showException" exception="java.sql.SQLException" />
			<result name="myException">/register.jsp</result>
			<result >/register.jsp</result>
		</action>
	</package>
</struts>



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值