在SSM框架中替换原有的404页面
1.404的html文件所处项目位置:
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>
<display-name>Archetype Created Web Application</display-name>
<!-- 过滤器:作用:对一切的请求进行条件过滤,不管你发送什么请求,首先都会过滤掉,主要目前是国际标志,中文过滤器(能让中文通过)-->
<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>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 2.程序入口需要去找jsp页面,需要加载Spring MVC加载视图解析器-->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 告诉Spring MVC需要对哪些页面解析-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-*.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.gif</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<!-- 替换404页面 -->
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/jsp/404.html</location>
</error-page>
</web-app>
3.在sping-mvc.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 1.将Spring MVC加载进配置文件 -->
<!-- 开启Spring MVC注解模式:(1)自动注册 (2)可以提供一些数据格式化 -->
<mvc:annotation-driven />
<!-- 2.加载静态资源 -->
<mvc:default-servlet-handler />
<!-- 3.页面解析部分:找jsp页面 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 指明解析什么类型的页面 -->
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 4.页面之间的动作由controller层产生 -->
<context:component-scan base-package="com.ssm.controller" />
</beans>
4.运行结果截图: