• struts2学习笔记之十二:struts2对异常的自动处理


    在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>
  • 相关阅读:
    如何实现Iframe透明
    ListView(未完)
    我又回来了
    前言
    代码重用and思路重用
    图片上传
    千万数量级分页存储过程
    MSSQL中,将text,ntext转换为int型数据
    优秀的回复,来自圣殿骑士
    SqlDataSource控件
  • 原文地址:https://www.cnblogs.com/djoker/p/6219445.html
Copyright © 2020-2023  润新知