在做一個項目的時候,為了界面美觀及用戶體驗,我們往往會設計自己的錯誤跳轉頁面,而不是直接展示給用戶一堆錯誤碼,為此我們需要配置自己的錯誤跳轉頁面。
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、運行結果
訪問鏈接

訪問結果

