Struts提供了一个更简单的方式来处理未捕获的异常,并将用户重定向到一个专门的错误页面。您可以轻松地Struts配置到不同的异常有不同的错误页面。
Struts的异常处理所使用的“exception”拦截容易。“exception”拦截器作为默认的栈的一部分,所以不必做任何额外的配置。它可为准备使用的盒。让我们看到了一个简单的Hello World示例进行一些修改在HelloWorldAction.java文件。在这里,我们特意推出了一个空指针异常在我们HelloWorldAction动作代码。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package com.yiibai.struts2;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorldAction extends ActionSupport{
private String name;
public String execute(){
String x = null ;
x = x.substring( 0 );
return SUCCESS;
}
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
}
|
让我们 helloWorld.jsp保持内容如下:
1
2
3
4
5
6
7
8
9
10
|
<%@ page contentType= "text/html; charset=UTF-8" %>
<%@ taglib prefix= "s" uri= "/struts-tags" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
Hello World, <s:property value= "name" />
</body>
</html>
|
以下是内容index.jsp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<%@ page language= "java" contentType= "text/html; charset=ISO-8859-1"
pageEncoding= "ISO-8859-1" %>
<%@ taglib prefix= "s" uri= "/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd" >
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h1>Hello World From Struts2</h1>
<form action= "hello" >
<label for = "name" >Please enter your name</label><br/>
<input type= "text" name= "name" />
<input type= "submit" value= "Say Hello" />
</form>
</body>
</html>
|
struts.xml 应该像这样:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<? 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.devMode" value = "true" />
< package name = "helloworld" extends = "struts-default" >
< action name = "hello"
class = "com.yiibai.struts2.HelloWorldAction"
method = "execute" >
< result name = "success" >/HelloWorld.jsp</ result >
</ action >
</ package >
</ struts >
|
现在右击项目名称,并单击Export > WAR File创建一个WAR文件。然后部署此WAR在Tomcat的webapps目录下。最后,启动Tomcat 服务器和尝试访问URL http://localhost:8080/HelloWorldStruts2/index.jsp。这会给出以下画面:
输入一个值“Struts2”,并提交页面。应该看到以下页面:
在上面的例子所示,默认的异常拦截器做了非常出色的处理异常。现在,让我们创建一个专用的错误页面,我们的例外。创建一个文件名为error.jsp 如以下内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<%@ page language= "java" contentType= "text/html; charset=ISO-8859-1"
pageEncoding= "ISO-8859-1" %>
<%@ taglib prefix= "s" uri= "/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd" >
<html>
<head>
<title></title>
</head>
<body>
This is my custom error page
</body>
</html>
|
Let us now configure Struts to use this this error page in case of an exception. Let us modify thestruts.xml as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<? 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.devMode" value = "true" />
< package name = "helloworld" extends = "struts-default" >
< action name = "hello"
class = "com.yiibai.struts2.HelloWorldAction"
method = "execute" >
< exception-mapping exception = "java.lang.NullPointerException"
result = "error" />
< result name = "success" >/HelloWorld.jsp</ result >
< result name = "error" >/Error.jsp</ result >
</ action >
</ package >< br ></ struts >
|
在上面的例子所示,现在我们已经配置 Struts使用专用error.jsp的NullPointerException异常。如果现在重新运行该程序,现在看到下面的输出:
根据<exception-mapping…../>元素出现位置的不同,异常映射又可分为两种:
局部异常映射:将<exception-mapping… />元素作为<action…/>元素的子元素配置;
全局异常映射:将<exception-mapping… />元素作为<global-exception-mappings… />元素的子元素配置;
全局异常映射对所有的Action都有效,但局部异常映射仅对该异常映射所在的Action有效。
如果局部异常映射和全局异常映射配置了同一个异常类型,在<action…./>元素内的局部异常映射将覆盖全局异常映射。
Struts.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
< package name = "ssh2" extends = "struts-default" >
< global-results >
< result name = "sql" >/exception.jsp</ result >
< result name = "root" >/exception.jsp</ result >
</ global-results >
< global-exception-mappings >
< exception-mapping exception = "java.sql.SQLException" result = "sql" />
< exception-mapping exception = "java.lang.Exception" result = "root" />
</ global-exception-mappings >
< action name = "login" class = "loginAction" >
< result >/welcome.jsp</ result >
< result name = "nullPointer" >/nullPointer.jsp</ result >
< exception-mapping exception = "java.lang.NullPointerException" result = "nullPointer" />
</ action >
</ package >
|
Action
1
2
3
4
5
6
7
|
public class loginAction extends ActionSupport
{
public String add() throws SQLException
{
return "toadd" ;
}
}
|
有异常往外抛即可。你也可以在方法里面抛,比如throw SQLException。
我们可以使用Struts2的标签输出异常信息:
输出异常的message属性信息:<s:property value="exception.message" />
输出异常堆栈信息:<s:property value="exceptionStack" />。
有了处理系统异常的基础,我们来看一看自定义异常:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
package com.exception ;
public class MyException extends Exception
{
private String message;
public MyException(String message)
{
super (message);
this .message = message ;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this .message = message;
}
}
public String execute() throws Exception
{
if (! "hello" .equals(usename) || ! "world" .equals(password))
{
throw new MyException( "用户名或密码错误,您发现了吧!" );
}
return "success" ;
}
|
在action配置中的异常处理
1
2
3
4
5
6
7
8
9
10
|
<struts>
< package name= "struts2" extends = "struts-default" >
<action name= "login" class = "com.struts2.LoginAction" >
<exception-mapping result= "myex" exception= "com.exception.MyException" >
</exception-mapping>
<result name= "myex" >/error.jsp</result>
<result name= "success" >/result.jsp</result>
</action>
</ package >
</struts>
|
在全局配置中的异常处理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<struts>
< package name= "struts2" extends = "struts-default" >
<global-results>
<result name= "myexception1" >/error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping result= "myexception1"
exception= "com.exception.MyException" >
</exception-mapping>
</global-exception-mappings>
<action name= "login" class = "com.struts2.LoginAction" >
<result name= "success" >/result.jsp</result>
</action>
</ package >
</struts>
|
错误页面error.jsp
1
2
3
4
5
6
7
8
|
<%@ page language= "java" import = "java.util.*" pageEncoding= "UTF-8" %>
<%@ taglib prefix= "s" uri= "/struts-tags" %>
<html>
<body>
<!-- 这个exception 是 exception= "com.exception.MyException" -->
<s:property value= "exception.message" />
</body>
</html>
|
总结
局部异常处理比全局异常处理高,并且可覆盖全局异常处理,如果定义了全局异常映射,那么会对所有的Action生效,反之定义了局部异常映射则会对当前Action生效,
如果在全局区域和局部区域定义了相同的异常映射,首先去局部异常区域找result结果页面,如果找到了,则直接跳转到错误结果页面,不管全局有没有相同的结果,都被局部所覆盖,如果在局部区域没找到,则去全局区域找。