Struts2 异常处理
Struts提供了一个更简单的方式来处理未捕获的异常,并将用户重定向到一个专门的错误页面。您可以轻松地Struts配置到不同的异常有不同的错误页面。
Struts的异常处理所使用的“exception”拦截容易。“exception”拦截器作为默认的栈的一部分,所以不必做任何额外的配置。它可为准备使用的盒。让我们看到了一个简单的Hello World示例进行一些修改在HelloWorldAction.java文件。在这里,我们特意推出了一个空指针异常在我们HelloWorldAction动作代码。
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保持内容如下:
<%@ 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.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 如以下内容:
<%@ 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.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>
</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.yiibai.struts2.HelloWorldAction"
method="execute">
<result name="success">/HelloWorld.jsp</result>
<result name="error">/Error.jsp</result>
</action> </package>
</struts>
Struts2 异常处理的更多相关文章
- 16.怎样自学Struts2之Struts2异常处理[视频]
16.怎样自学Struts2之Struts2异常处理[视频] 之前写了一篇"打算做一个视频教程探讨怎样自学计算机相关的技术",优酷上传不了,仅仅好传到百度云上: http://pa ...
- struts2 异常处理3板斧
板斧1:找不到action的错误 在struts.xml中参考如下配置 <struts> ... <package name="default" namespac ...
- struts2异常处理及类型转换
一.struts2对异常的处理 1.自定义局部异常: <action> <exception-mapping result="sonException" exce ...
- struts2异常处理机制
一.处理一般异常(javaBean异常) struts2进行异常处理首先需要添加exception拦截器,而默认拦截器栈已经加入了这个拦截器,所以不用特意的声明.在Struts 2框架中,采用声明式异 ...
- Struts2 - 异常处理: exception-mapping 元素
异常处理: exception-mapping 元素 在action方法中添加 int i=1/0; 请求action后,结果为: 在struts.xml中添加异常处理:exception-mappi ...
- struts2异常处理,global-results定义全局结果处理
<global-results>定义全局结果处理 一般发生异常之后 结果返回errHandler 因为errHandler是由<global-exception-mappings&g ...
- Struts2异常处理配置
<package name="lee" extends="struts-default"> <!--定义全局结构映射 --> <g ...
- struts2异常处理
<global-results> <result name="nullException">/WEB-INF/exception/nullException ...
- Struts2、Spring MVC4 框架下的ajax统一异常处理
本文算是struts2 异常处理3板斧.spring mvc4:异常处理 后续篇章,普通页面出错后可以跳到统一的错误处理页面,但是ajax就不行了,ajax的本意就是不让当前页面发生跳转,仅局部刷新, ...
随机推荐
- getopt使用
参考: http://www.gnu.org/software/libc/manual/html_node/Example-of-Getopt.html http://en.wikipedia.org ...
- 支付宝签名验证实现-Delphi版
支付宝签名验证实现-Delphi版 首先介结下支付宝签名验证流程: 一 支付宝密钥生成 支付宝提供秘钥生成工具https://docs.open.alipay.com/291/105971/ 用此下 ...
- 【js】判断浏览器是否IE浏览器
搜罗各种方法来判断浏览器是否为IE浏览器 1.最简单的[来自:http://www.cnblogs.com/heganlin/p/5889743.html] if(!+[1,]){ layer.msg ...
- ios滚动UIScrollView的setContentOffset方法
在UIScrollView,setContentOffset方法的功能是跳转到你指定内容的坐标,[self.scroview setContentOffset:CGPointMake(0, 50) a ...
- Oracle审计--AUD$占用空间较大处理方案
Oracle 11G以后,数据库默认是开启审计功能的,因此有时候我们忘记了关闭该功能导致SYSTEM表空间暴满,但由于关闭审计功能需要重启数据库,此类操作生产环境下是不允许的,因此我们需要找出哪类审计 ...
- ASP.NET Core 1.0基础之诊断
来源https://docs.asp.net/en/latest/fundamentals/diagnostics.html ASP.NET Core 1.0包含了一些新的特性来辅助诊断问题.可以在S ...
- Swift,闭包
闭包(相当于匿名函数)的几种情况 利用sorted()排序方法来进行示例 1.sorted()正常引用的情况 var str=["d","a","c& ...
- java获取桌面路径的方法
FileSystemView fsv = FileSystemView.getFileSystemView(); File com=fsv.getHomeDirectory(); System.out ...
- CSS中样式
CSS是Cascading Style Sheets的简称,中文称为层叠样式表,用来控制网页数据的表现,可以使网页的表现与数据内容分离.要想让CSS对网页内容有效果,必须将CSS代码引入网页,通常有四 ...
- 怎样在caffe中添加layer以及caffe中triplet loss layer的实现
关于triplet loss的原理.目标函数和梯度推导在上一篇博客中已经讲过了.详细见:triplet loss原理以及梯度推导.这篇博文主要是讲caffe下实现triplet loss.编程菜鸟.假 ...