在UserAction类中引发异常,但是不处理

package com.djoker.struts2;

import java.util.Date;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.Action;

public class UserAction {

    private String username;

    private String password;

    private Date createDate;

    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 Date getCreateDate() {
return createDate;
} public void setCreateDate(Date createDate) {
this.createDate = createDate;
} public String addUser() throws Exception {
if("admin".equals(username)){
throw new UserNotFoundException("账号没有找到");
}
ServletActionContext.getRequest().setAttribute("username", username);
ServletActionContext.getRequest().setAttribute("password", password);
ServletActionContext.getRequest().setAttribute("createDate", createDate);
return Action.SUCCESS;
}
}
UserNotFoundException.java类
package com.djoker.struts2;

public class UserNotFoundException extends RuntimeException {

    public UserNotFoundException() {
super();
// TODO Auto-generated constructor stub
} public UserNotFoundException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
} public UserNotFoundException(String message) {
super(message);
// TODO Auto-generated constructor stub
} public UserNotFoundException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
} }
配置文件中可以配置全局异常和局部异常
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd"> <struts>
<package name="user" extends="struts-default" namespace="/user">
<global-exception-mappings>
<exception-mapping result="error" exception="com.djoker.struts2.UserNotFoundException"></exception-mapping>
</global-exception-mappings>
<action name="*User" class="com.djoker.struts2.UserAction" method="{1}User">
<exception-mapping result="error" exception="com.djoker.struts2.UserNotFoundException"></exception-mapping>
<result>/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
错误后的跳转页面error.jsp,可以使用el表达式取得异常信息
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
<h1>登录失败</h1>
<!-- 获取异常信息 -->
${exception.message }<br>
<!-- 获取异常堆栈信息 -->
${exceptionStack }<br>
</body>
</html>

最新文章

  1. C++ 判断字符串是否全是数字
  2. Linux文件的建立、复制、删除和移动命令
  3. 用CSS和jQuery制作简单的下拉框
  4. python - easy_install的安装和使用
  5. 转:ASP.NET MVC利用TryUpdateModel来做资料更新 (一)
  6. wxpython 中 用鼠标拖动控件 总结
  7. Java基础知识强化之集合框架笔记74:各种集合常见功能 和 遍历方式总结
  8. 一个IT男的创业感言
  9. 在Debian9(linux)上使用 的 python 3 IDLE(已经安装了python 2.7 的情况下)
  10. MyEclipse使用(一)
  11. object-fit?
  12. 房上的猫:java中的包
  13. 【基础】使用cookies,实现免登陆(七)
  14. spring aop 加在Controller层造成类初始化两遍
  15. qemu对虚拟机的内存管理(一)
  16. java十进制转换成二进制数
  17. float,double等精度丢失问题 float,double内存表示
  18. django 模板语言入门详解
  19. C# AtomicBoolean
  20. java学习笔记----@Override的作用

热门文章

  1. 写了一个基于React+Redux的仿Github进度条
  2. Ubuntu MYSQL和Windows MYSQL (非C盘安装)
  3. 手拼SQL小技巧,WHERE 1=1
  4. Sublime无法使用package control安装插件
  5. 在 Mac OS 上创建并运行 ASP.NET Core 1.0 网站
  6. angular中的 ng-change
  7. Android中 int 和 String 互相转换的多种方法
  8. SIFT中的尺度空间和传统图像金字塔
  9. AndroidManifest.xml中&lt;activity&gt;&lt;/activity&gt;相关属性说明
  10. [Java基础]字符串