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的本意就是不让当前页面发生跳转,仅局部刷新, ...
随机推荐
- [转] Matlab与C++混合编程,添加OpenCV库
原文地址 峰回璐转 最近在做运动医学软件优化工作,此款软件框架及算法语言全由matlab实现,虽然matlab矩阵运算.数值计算能力强大,但速度让人难以忍 受.软件立刻移植到C++上又不太实际,故采用 ...
- weblogic下同域不同端口下的跨域问题解决
环境:同一台服务器,同一个Weblogic应用程序,分别建两个域,两个域IP一样,端口不同.一个域里放Web应用A,一个放Web应用B. 操作:用户访问A程序的时候,A程序会返回一个链接,让用户去 ...
- Fatal error: Maximum execution time of 30 seconds exceeded in
Fatal error: Maximum execution time of 30 seconds exceeded in C:\Program Files\Apache Software Found ...
- telnet命令
详细资料 telnet命令使用方法详解-telnet命令怎么用-win7没有telent怎么办 2017年07月26日 15:37:36 阅读数:1010 什么是Telnet? 对于Telnet的认识 ...
- 学习实践:使用模式,原则实现一个C++数据库訪问类
一.概述 在我參与的多个项目中.大家使用libMySQL操作MySQL数据库,并且是源代码级复用,在多个项目中同样或相似的源代码.这种复用方式给开发带来了不便. libMySQL的使用比較麻烦.非常e ...
- Python扫描指定文件夹下(包含子文件夹)的文件
扫描指定文件夹下的文件.或者匹配指定后缀和前缀的函数. 假设要扫描指定文件夹下的文件,包含子文件夹,调用scan_files("/export/home/test/") 假设要扫描 ...
- 安装完office2016 64位后,在安装visio时,报错,无法安装,
安装环境要求: 系统要求:win8,win10等: office要求:sw(批量版)不能和cn(零售版).365版混装.-------重点注意事项 一定要注意批量版和零售版的区别,各版本之间绝对不允许 ...
- DNN CMS Platform
http://dotnetnuke.codeplex.com/downloads/get/1458710
- pngCanvas 是一个使用纯Python代码的生成png图像的工具
#!/usr/bin/env python """Simple PNG Canvas for Python - updated for bytearray()" ...
- 通过Spring使用远程访问和web服务
http://docs.huihoo.com/spring/zh-cn/remoting.html Spring2 提供的remote包学习笔记