struts提供了一个更简单的方式来处理未捕获的异常,并将用户重定向到一个专门的错误页面。您可以轻松地struts配置到不同的异常有不同的错误页面。
struts的异常处理所使用的“exception”拦截容易。“exception”拦截器作为默认的栈的一部分,所以不必做任何额外的配置。它可为准备使用的盒。让我们看到了一个简单的hello world示例进行一些修改在helloworldaction.java文件。在这里,我们特意推出了一个空指针异常在我们helloworldaction动作代码。
package com.h3.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保持内容如下:
<%@ 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:
<%@ 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 应该像这样:
<?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.h3.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 如以下内容:
<%@ 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:
<?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.h3.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> </struts>
在上面的例子所示,现在我们已经配置 struts使用专用error.jsp的nullpointerexception异常。如果现在重新运行该程序,现在看到下面的输出:
此外,struts2 框架自带的“日志”拦截记录异常。使记录器记录的未捕获异常,我们可以很容易地看堆栈跟踪和工作出了什么错误。
我们已经看到了我们可以处理特定的异常行动。我们可以设置一个例外,全局将适用于所有的行动。例如,要捕获 nullpointerexception异常,我们可以添加<global-exception-mappings...>标签里面<package...>标签和其<result...>标签应加在<行动.. >标记在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.devmode" value="true" /> <package name="helloworld" extends="struts-default"> <global-exception-mappings> <exception-mapping exception="java.lang.nullpointerexception" result="error" /> </global-exception-mappings> <action name="hello" class="com.h3.struts2.helloworldaction" method="execute"> <result name="success">/helloworld.jsp</result> <result name="error">/error.jsp</result> </action> </package> </struts>