2.请求 url-pattern 设置

1.tomcat下面的web.xml文件配置信息

  <!-- ================ Built In Servlet Mappings ========================= -->


  <!-- The servlet mappings for the built in servlets defined above.  Note  -->
  <!-- that, by default, the CGI and SSI servlets are *not* mapped.  You    -->
  <!-- must uncomment these mappings (or add them to your application's own -->
  <!-- web.xml deployment descriptor) to enable these services              -->

    <!-- The mapping for the default servlet -->
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- The mappings for the JSP servlet -->
    <servlet-mapping>
        <servlet-name>jsp</servlet-name>
        <url-pattern>*.jsp</url-pattern>
        <url-pattern>*.jspx</url-pattern>
    </servlet-mapping>

 

2.项目中 web.xml配置文件

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0" metadata-complete="true">
    
    <servlet>
        <servlet-name>springMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springMVC.xml</param-value>
        </init-param>
    </servlet>
    <!-- 
        / 这种方式会导致我们的静态资源被拦截,不能成功解析显示出来
              其原因是我们的tomcat服务器中也有使用 / 来做为servlet的映射路径
             而我们的加载顺序是 
          1.tomcat服务器中的 web.xml
          2.项目中的 web.xml
              则我们的后者把前者覆盖,所以静态资源得不到解析处理
     -->
    <servlet-mapping>
        <url-pattern>/</url-pattern>
        <servlet-name>springMVC</servlet-name>
    </servlet-mapping>
    
</web-app>

 

 

从以上两段配置文件中可以看出 我们开发的项目 web.xml确实把 tomcat /conf/web.xml 中的 <url-pattern>/</url-pattern> 覆盖

从而导致图片 js  css 等文件不能正常显示

 

解决方案

<?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx" 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/jee http://www.springframework.org/schema/jee/spring-jee.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
">

    <!--@RequestMapping标签MVC注解驱动 -->
    <mvc:annotation-driven />

    <!--spring组件扫描哪些包 IOC组件扫描器 -->
    <context:component-scan base-package="com.gzcgxt.erp" />

    <!-- 配置静态资源文件 原理:将SpringMVC上下文环境中定义一个DefaultServletHttpRequestHandler,对所有前端控制器的请求进行过滤筛选 
        如果发现当前的请求没有经过映射器,则就把此请求重新交还给Tomcat的默认servlet来处理解析 -->
    <mvc:default-servlet-handler />

    <!--配置视图解析器 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

 

posted @ 2019-07-16 15:03  往事只能回味---  阅读(329)  评论(0)    收藏  举报