zoukankan      html  css  js  c++  java
  • springmvc之默认错误页面跳转

    在做一个项目的时候,为了界面美观及用户体验,我们往往会设计自己的错误跳转页面,而不是直接展示给用户一堆错误码,为此我们需要配置自己的错误跳转页面。

    1、项目结构

      

    2、web.xml

      

    <!DOCTYPE web-app PUBLIC
     "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
     "http://java.sun.com/dtd/web-app_2_3.dtd" >
    
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns="http://java.sun.com/xml/ns/javaee"
             xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
             version="3.0">
      <display-name>Archetype Created Web Application</display-name>
    
      <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
      </servlet>
      <!-- 设置dispatchservlet的匹配模式,通过把dispatchservlet映射到/,默认servlet会处理所有的请求,包括静态资源 -->
      <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>
    
      <!-- 字符集过滤器 -->
      <filter>
      <filter-name>encodingFilter</filter-name>
      <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
      <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
      </init-param>
      <init-param>
      <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
      </init-param>
      </filter>
      <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>
      
      <!--错误跳转页面-->
      <error-page>
        <exception-type>java.lang.Throwable</exception-type>
        <location>/WEB-INF/views/error/404.jsp</location>
      </error-page>
      <error-page>
        <error-code>404</error-code>
        <location>/WEB-INF/views/error/404.jsp</location>
      </error-page>
      <error-page>
        <error-code>500</error-code>
        <location>/WEB-INF/views/error/500.jsp</location>
      </error-page>
    </web-app>

    注:其实只是在原有的springmvc配置的基础上增加<error-page>标签项即可。

    3、dispatcher-servlet.xml

      

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xmlns:p="http://www.springframework.org/schema/p"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/mvc
                http://www.springframework.org/schema/mvc/spring-mvc.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context.xsd"
           default-lazy-init="true">
    
        <!-- 通过mvc:resources设置静态资源,这样servlet就会处理这些静态资源,而不通过控制器 -->
        <!-- 设置不过滤内容,比如:css,jquery,img 等资源文件 -->
        <mvc:resources location="/*.html" mapping="/**.html" />
        <mvc:resources location="/css/*" mapping="/css/**" />
        <mvc:resources location="/js/*" mapping="/js/**" />
        <mvc:resources location="/images/*" mapping="/images/**" />
    
        <!-- 添加注解驱动 -->
        <mvc:annotation-driven />
        <!-- 默认扫描的包路径 -->
        <context:component-scan base-package="com.*" />
    
        <!-- mvc:view-controller可以在不需要Controller处理request的情况,转向到设置的View -->
        <!-- 像下面这样设置,如果请求为/,则不通过controller,而直接解析为/index.jsp -->
        <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
            <property name="viewClass"
                      value="org.springframework.web.servlet.view.JstlView" />
            <property name="prefix" value="/WEB-INF/views/" />
            <property name="suffix" value=".jsp" />
        </bean>
    
        <!--  日志自定义注解拦截器 -->
        <mvc:interceptors>
            <mvc:interceptor>
                <mvc:mapping path="/**"/>
                <bean class="com.log.SystemLogAspect"></bean>
            </mvc:interceptor>
        </mvc:interceptors>
    
    </beans>

    4、TestController.java

    package com.controller;
    
    import com.log.Log;
    import com.remote.RemoteDemo;
    import net.sf.json.JSONObject;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    /**
     * Created by sunjf on 2016/6/26.
     */
    @Controller
    public class TestController {
    
        @Log(desc = "log测试")
        @RequestMapping(value = "test1")
        public String test1(){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("username", "sun");
            jsonObject.put("password", "123");
           // String result = RemoteDemo.remoteJsonRequest("http://localhost:8080/test2?username=sun&password=123", 5000, jsonObject);
            //System.out.println(result);
            return "error/success1";
        }
    
    }

    注:该方法跳转页面为success1.jsp,从项目结构中可以看出,我们的项目中是没有这个页面的

    5、index.jsp 

    <html>
    <body>
    <h2>Hello World!</h2>
    <form action="test1" method="post" id="form1">
        <input type="submit" name="submit" value="button">
    </form>
    </body>
    </html>

    6、404.jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>404</title>
    </head>
    <body>
        <h1 align="center" style="margin-top:100px;font-size: 200px">404</h1>
        <h3 align="center">未能找到您访问的资源。</h3>
    </body>
    </html>

    7、运行结果

      访问链接

      

      访问结果

      

  • 相关阅读:
    Win7电脑无法安全删除硬件并弹出媒体的解决方法
    Linux环境变量及其设置
    Linux环境变量文件environment, profile, bashrc含义
    Linux下设置和查看环境变量
    [Android Traffic] 让android应用在传输网络数据的时候更省电
    [Android Traffic] 看无线电波如何影响网络操作]
    [Android Pro] Android的5个进程等级
    [编码解码] Base64 编码换行和+号遍空格的处理
    [Android Traffic] Android网络开启、关闭整理
    [Android Traffic] android 流量计算方法
  • 原文地址:https://www.cnblogs.com/sunjf/p/springmvc_error_view.html
Copyright © 2011-2022 走看看