• springmvc03-异常处理-静态文件


    1,一个简单的登录

      login.jsp页面

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    <!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=UTF-8">
    <title>登录页面</title>
    </head>
    <body>
        <!--  登录表单 -->
        <form action="user/login" method="post">
            name:<input type="text" name="name"><br/>
            password:<input type="password" name="password"><br/>
            <input type="submit" name="submit">
        </form>
    </body>
    </html>

      提交到UserAction的login方法

        
        @RequestMapping(value="login",method=RequestMethod.POST)
        //如何获取request,response,session?
        //直接在方法参数中声明即可使用
        public String login(String name,String password,HttpSession session){
            
            if(!users.containsKey(name)){
                throw new UserException("用户名不存在");
            }
            User u=users.get(name);
            if(!u.getPassword().equals(password)){
                throw new UserException("密码不正确");
            }
            
            session.setAttribute("loginUser", u);
            return "redirect:/user/users"; 
        }
        

      其中UserException是我们的自定义异常类,如下

    package com.yangw.springmvc.exception;
    
    public class UserException extends RuntimeException {
    
        private static final long serialVersionUID = 1L;
    
        public UserException() {
            super();    
        }
    
        public UserException(String message, Throwable cause) {
            super(message, cause); 
        }
    
        public UserException(String message) {
            super(message);
        }
    
        public UserException(Throwable cause) {
            super(cause);
        }
    }

      此时的登录,输入错误的用户名或者密码时,直接显示异常信息,显然是不合理的,我们需要处理

    异常的处理方式-(第一种 局部异常处理模式)

    /**
         * 局部异常处理模式,仅仅能处理这个控制器中的异常)
         */
        //value是一个数组
        @ExceptionHandler(value={UserException.class})
        public String handlerException(UserException e,HttpServletRequest req){
            //将异常对象存入Request中
            req.setAttribute("e", e);
            return "error";
        }

      然后在error.jsp页面显示错误信息

    <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
    <!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=utf-8">
    <title>错误信息</title>
    </head>
    <body>
     <h3>${e.message}</h3><br/>
    </body>
    </html>

    异常的处理方式-(第2种 全局异常处理模式),主要是在主配置文件中完成的

      为 hello-servlet.xml增加异常处理配置

    <!-- 全局异常处理-->
        <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
            <property name="exceptionMappings">
                <props>
                    <!-- key 是异常类 prop元素的值是异常页面-->
                    <prop key="com.yangw.springmvc.exception.UserException">error</prop>
                </props>
            </property>
        </bean>

      此时,需要把局部异常处理方法删掉,负责全局的不起作用,此外error.jsp页面需要稍微改动,它使用的是exception对象

    <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
    <!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=utf-8">
    <title>错误信息</title>
    </head>
    <body>
     <h3>${exception.message}</h3><br/>
    </body>
    </html>

      我们为list.jsp页面加入一个样式main.css文件,它属于一个静态文件,该如何处理呢?

       /resources/css/main.css文件内容:

    * {
        font-size: 14px;
        color: red;
    }

      在springmvc主配置文件中配置如下一行信息

       <!-- 静态文件,比如css文件等的处理方式-->
        <!-- 将静态文件指定到某个特殊的文件夹中统一处理 -->
        <!-- ** 表示所有文件包括子文件夹中的文件-->
        <mvc:resources location="/resources/" mapping="/resources/**"/>

      然后在list.jsp中引入这个css文件

    <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
    
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <!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=utf-8">
    <title>所有用户</title>

    <!-- 引入css文件 -->
    <link rel="stylesheet" href="<%=request.getContextPath() %>/resources/css/main.css" type="text/css">

    </head> <body> <h2> 当前登录用户--->${loginUser.name} </h2> <a href="add">添加用户</a><br/> <c:forEach items="${users }" var="um" > <!-- 这里的um是一个个的 键值对 --> ${um.key}---<a href="${um.value.name }">${um.value.name }</a>---${um.value.password }----${um.value.age } ---- <a href="${um.value.name }/update">更新</a>--- <a href="${um.value.name }/delete">删除</a><br/> </c:forEach> </body> </html>
    ----------- 赠人玫瑰,手有余香     如果本文对您有所帮助,动动手指扫一扫哟   么么哒 -----------


    未经作者 https://www.cnblogs.com/xin1006/ 梦相随1006 同意,不得擅自转载本文,否则后果自负
  • 相关阅读:
    装饰器
    静态文件---访问图片
    用户登录
    读写Session
    windows进入指定目录
    Response 与 Cookie
    处理HTTP请求
    pycharm中指定ip和端口
    python爬虫学习(三):使用re库爬取"淘宝商品",并把结果写进txt文件
    python爬虫学习(二):定向爬虫例子-->使用BeautifulSoup爬取"软科中国最好大学排名-生源质量排名2018",并把结果写进txt文件
  • 原文地址:https://www.cnblogs.com/xin1006/p/3457470.html
Copyright © 2020-2023  润新知