且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

spring+springMVC+mybatis的整合 part3

更新时间:2022-08-12 16:14:51

配置自定义的404页面,替换Tomcat不友好的404页面

有时候我们想替换掉tomcat自带的404页面
如图:

Paste_Image.png

404也就是说找不到当前资源或者资源不存在

 The origin server did not find a current representation for the target resource 
or is not willing to disclose that one exists.
替换思路:错误404这种常出现的页面,我们可以设置为静态资源,以加快网页访问。
替换项目的404页面
第一步:我们需要先把WEB-IN\Fweb.xml下面的mvc-dispatcher更改为全局配置。
<servlet-mapping>
      <servlet-name>mvc-dispatcher</servlet-name>
      <!-- 默认匹配所有的请求 -->
      <!-- 我们默认配置这个是为了让我们的Spring框架接管Servelt,实现Spring控制所有站点请求 -->
      <url-pattern>/</url-pattern>
      <!--<url-pattern>/css/*</url-pattern>-->
      <!--<url-pattern>/images/*</url-pattern>-->
      <!--<url-pattern>/fonts/*</url-pattern>-->
  </servlet-mapping>

错误404的页面是常用页面之一,所以我们在项目的资源目录(webapp)下创建一个static目录,专门用来存放静态资源,如js、css、错误提示页面、登录、注册页面等等。页面都存放在view中
建立完目录如下

Paste_Image.png

PS:你可以上网找一些好看的404页面,在上面的相关目录存相关资源,例如一些CSS,JS的资源,HTML页面的话就存在view中
第二步:在web.xml中添加错误页面的资源
<error-page>
    <error-code>404</error-code>
    <location>/static/view/404.html</location>
  </error-page>

不过配好后启动项目输入错误页面还是不能显示自己配置的404页面,而是导致服务走丢了

第三步:在spring目录下写spring-web.xml,用于控制哪些资源被拦截。

spring-web.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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       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">
    <!-- 配置SpringMVC -->
    <!-- 1.开启SpringMVC注解模式 -->
    <!-- 简化配置:
        (1)自动注册DefaultAnootationHandlerMapping,AnotationMethodHandlerAdapter
        (2)提供一些列:数据绑定,数字和日期的format @NumberFormat, @DateTimeFormat, xml,json默认读写支持
    -->
    <mvc:annotation-driven/>
    <!-- 2.静态资源默认servlet配置
        (1)加入对静态资源的处理:js,gif,png
        (2)允许使用"/"做整体映射
     -->
    <mvc:resources mapping="/css/**" location="/static/css/" />
    <mvc:resources mapping="/js/**" location="/static/js/"/>
    <mvc:resources mapping="/images/**" location="/static/images/" />
    <mvc:resources mapping="/view/**" location="/static/view/" />
    <mvc:default-servlet-handler/>

    <!-- 3.配置jsp 显示ViewResolver -->
    <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.扫描web相关的bean -->
    <context:component-scan base-package="pjb.ssm.mvc">
        <!-- 制定扫包规则 ,只扫描使用@Controller注解的JAVA类 -->
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
</beans>

以上文件配置好后,重启服务器,并输入错误地址,现在插入的404页面正常显示了。
这个是我的404页面

Paste_Image.png

主要参考于大牛Clone丶记忆的SSM集成之路