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