struts2的异常
index.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>测试异常</title>
</head> <body>
<form action="lee/login.action">
用户名:<input type="text" name="username" /><br/>
密码:<input type="password" name="password" /><br />
<input type="submit" value="提交" />
</form>
</body>
</html>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<package name="lee" extends="struts-default">
<!-- 此处定义所有的全局结果 -->
<global-results>
<!-- 逻辑名为sql的结果,映射到/sqlexception.jsp页面 -->
<result name="sql">/sqlexception.jsp</result>
<!-- 逻辑名为root的结果,映射到/exception.jsp -->
<result name="root">/exception.jsp</result>
</global-results>
<!-- 此处定义所有的全局异常映射 -->
<global-exception-mappings>
<!-- Action抛出SqlException异常时,转入名为sql的结果 -->
<exception-mapping result="sql" exception="java.sql.SQLException"></exception-mapping>
<exception-mapping result="root" exception="java.lang.Exception"></exception-mapping>
</global-exception-mappings>
<!-- 下面配置本系统的Action -->
<action name="login" class="lee.LoginAction">
<!-- 下面配置了一个局部异常映射,当Action抛出lee.MyException时,转入名为my的结果 -->
<exception-mapping result="my" exception="lee.MyException"></exception-mapping>
<result name="my">/exception.jsp</result>
<result name="error">/error.jsp</result>
<result name="success">/welcome.jsp</result>
</action>
</package>
</struts>
MyExction.java
package lee;
public class MyException extends Exception {
public MyException(String mess){
super(mess);
}
}
LoginAction.java
package lee;
import java.sql.SQLException;
import com.opensymphony.xwork2.Action;
public class LoginAction implements Action{
private String username;
private String password;
//封装服务器处理后的结果
private String tip;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getTip() {
return tip;
}
public void setTip(String tip) {
this.tip = tip;
}
@Override
public String execute() throws Exception {
String result="";
if(getUsername().equalsIgnoreCase("user")){
throw new MyException("自定义异常");
}
if(getUsername().equalsIgnoreCase("sql")){
throw new SQLException("用户名不能为sql");
}
if(getUsername().equals("scott")&&getPassword().equals("tiger")){
setTip("哈哈,服务器提示!");
result=SUCCESS;
}
else {
result=ERROR;
}
return result;
}
}
sqlexception.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> </head> <body>
<h1>sqlexception</h1>
<h2><s:property value="exception.message" /> </h2>
<h3>
<s:property value="exceptionStack" />
</h3>
</body>
</html>
在登陆页用户名框中输入sql,然后提交
跳转到sqlexception.jsp页面

struts2的异常的更多相关文章
- struts2学习笔记之十二:struts2对异常的自动处理
在UserAction类中引发异常,但是不处理 package com.djoker.struts2; import java.util.Date; import org.apache.struts2 ...
- struts2 进阶--异常捕获机制
在SpringMvc中有自己的异常处理机制,struts2当然会有此功能,主要是在struts.xml中配置: <bean type="com.opensymphony.xwork2. ...
- struts2的异常配置
1:当我们出现异常我们浏览器会直接暴露我们的技术结构,会给我们的项目带来一些安全隐患.2:当这种错误出现,给用户感觉是非常不友好.3:怎么解决 1:如果处理找不到action方法的错误呢? 在Stru ...
- struts2 笔记03 异常支持、防止页面刷新和后退、方法验证
Struts2对异常支持(声明式异常.自动的异常处理), 异常处理(运行期异常事务自动回滚) 1. 自定义异常类,继承RuntimeException或Exception实现构造方法. 2. 配置异常 ...
- struts2笔记12-声明式异常
1.配置异常处理 <action name="save" class="com.test.actions.ProductAction" method=&q ...
- Struts2中的异常处理
因为在Action的execute方法声明时就抛出了Exception异常,所以我们无需再execute方法中捕捉异常,仅需在struts.xml 中配置异常处理. 为了使用Struts2的异常处理机 ...
- struts2异常处理及类型转换
一.struts2对异常的处理 1.自定义局部异常: <action> <exception-mapping result="sonException" exce ...
- Struts2 一张图片引发的bug
今天如常的打开项目开放.写了一会保存测试.在登录时出了个错误当不影响正常使用.丫的昨天还好好的.行下手上的工作 开始找bug 错误核心代码如下: 10:34:46,442 WARN OgnlValu ...
- java struts2入门学习---异常处理和类型转换
一.struts2对异常的处理 1.自定义局部异常: <action> <exception-mapping result="sonException" exce ...
随机推荐
- noip2014普及组 比例简化
题目描述 在社交媒体上,经常会看到针对某一个观点同意与否的民意调查以及结果.例如,对某一观点表示支持的有1498 人,反对的有 902人,那么赞同与反对的比例可以简单的记为1498:902. 不过,如 ...
- java 获取实体类对象属性值的方法
在java中我们要获得实体类对象的属性,一般情况是将实体类中的属性私有化,然后再对外提供get()与set()方法,然后再获取实体类对象的属性的时候先把对象new出来,再用变量名.get()的方法得到 ...
- ASP.NET MVC 程序 报错“CS0012: 类型“System.Data.Objects.DataClasses.EntityObject”在未被引用的程序集中定义”的解决办法
运行MVC程序,具体报错信息如下: 解决方法: 打开Web.config在assemblies下加入<add assembly="System.Data.Entity, Version ...
- Webproject 每次运行都停到workerDone(this); tomcat调试
那是因为你是Debug调试,你要将Dubug的时间设置长一些; 设置步骤: window-> preferences -> java -> debug -> commu ...
- Google Play开发者账号注册与失败申诉攻略
Google Play开发者账号注册与失败申诉攻略 这篇文章我在网上找了好久,是在Google play进行开发者账号注册方法,介绍的很详细.现在分享一下.[原文地址] 为了方便开发者们注册谷歌的官方 ...
- ckeditor中“浏览服务器”的后台操作
此博文,基于CKeditor 4.5.6版本测试通过. 原创博文,转载请注明出处 参考官方文档,以及网络上的一些帖子.经过调试得到正确的期待中的结果. [网络上的一些所谓的帖子,不知道是故意将上传的代 ...
- CUICatalog: Invalid asset name supplied: (null) _configureCellForDisplay:forIndexPath
1.CUICatalog: Invalid asset name supplied: (null) 如果连续出现几个这样的错误,表示UIImageView为空 那么就需要检查UIImageView是否 ...
- 【jmete】Bean shell使用(一)
一.什么是Bean Shell BeanShell是一种完全符合Java语法规范的脚本语言,并且又拥有自己的一些语法和方法; BeanShell是一种松散类型的脚本语言(这点和JS类似); BeanS ...
- makefile详解 嵌套执行make,定义命令包
嵌套执行make 在一些大的工程中,我们会把我们不同模块或是不同功能的源文件放在不同的目录中,我们可以在每个目录中都书写一个该目录的Makefile,这有利于让我们的Makefile变得更加地简洁,而 ...
- 【Linux】系统 之 RAID
本人从事DBA相关的工作,最近遇到了IO抖动伴随shread running抖动的情况,主机宕机重启后备库及下游解析binlog出现损坏的案例,向一些有经验的同事咨询学习,其中最大的嫌疑是:raid卡 ...