springmvc-210804-01---解决在web.xml中url-pattern使用斜杠访问静态资源

springmvc-210804-01—解决在web.xml中url-pattern使用"/"访问静态资源


我的工程目录


演示

web.xml(在url-pattern使用 / )

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0"
         metadata-complete="true">

  <!-- 配置过滤器,解决post请求中文乱码问题 -->
  <filter>
    <filter-name>characterEncodingFilter</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>

    <!-- 强制请求对象(HttpServletResponse)使用encoding编码的值 -->
    <init-param>
      <param-name>forceResponseEncoding</param-name>
      <param-value>true</param-value>
    </init-param>

    <!-- 强制应答对象(HttpServletRequest)使用encoding编码的值 -->
    <init-param>
      <param-name>forceRequestEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>


  <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:springmvc02.xml</param-value>
    </init-param>

    <load-on-startup>1</load-on-startup>
  </servlet>

  
  <servlet-mapping>
    <!--
        当项目中使用斜杠 “ / ” ,它会替代tomcat中的default。
        导致所有的静态资源都交给DispatcherServlet处理,
        默认情况下,DispatcherServlet没有处理静态资源的能力。所以导致访问静态资源(html,js,图片,css等)都是404。
    -->
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

</web-app>

springmvc.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.xsd">

    <!-- 开启组件扫描 -->
    <context:component-scan base-package="com.bgy.controller"></context:component-scan>

    <!-- 声明springmvc框架中的视图解析器,帮助开发人员设置视图文件的路径 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 前缀 -->
        <property name="prefix" value="/WEB-INF/view/"></property>
        <!-- 后缀 -->
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!-- 注册驱动 -->
    <mvc:annotation-driven/>

    <!--
        第一种解决静态资源处理方式:
            需要在springmvc配置文件加入<mvc:default-servlet-handler>
            原理:
                加入这个标签后,框架会创建控制器对象DefaultServletRequestHandler(类似我们自己创建的MyController),
                DefaultServletRequestHandler这个对象再把请求转发给tomcat的default这个servlet

        这种方式的好处就是方便,不过它归根还是依赖于tomcat等服务的default的servlet。有局限性。
    -->
    <mvc:default-servlet-handler/>
</beans>

springmvc02.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.xsd">

    <!-- 开启组件扫描 -->
    <context:component-scan base-package="com.bgy.controller"></context:component-scan>

    <!-- 声明springmvc框架中的视图解析器,帮助开发人员设置视图文件的路径 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 前缀 -->
        <property name="prefix" value="/WEB-INF/view/"></property>
        <!-- 后缀 -->
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!-- 注册驱动 -->
    <mvc:annotation-driven/>

    <!--
        第二种解决静态资源处理方式:
           使用<mvc:resources mapping="" location=""/>,框架会创建ResourceHttpRequestHandler处理器对象,
           让这个处理器处理静态资源的访问,不依赖于tomcat,
           mapping:访问静态资源的uri地址,通配符 **,
           location:静态资源在你的项目中目录位置。
    -->
    <!--
        "/images/**"  :
            意思就是http://localhost:8080/springmvc_21080401/images/img01.jpg  中的所有images请求

        location="/images/"   :
            就是:工程下的images文件夹
    -->
    <mvc:resources mapping="/images/**" location="/images/"/>
    <mvc:resources mapping="/js/**" location="/js/"/>
    <mvc:resources mapping="/html/**" location="/html/"/>

    <!-- 如果嫌一个一个弄太麻烦,可以建一个static静态资源文件夹,来存放这些静态资源 -->
    <mvc:resources mapping="/static/**" location="/static/"/>
</beans>

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>springmvc</title>
    <script type="text/javascript" src="js/jquery-3.4.1.js"></script>
</head>
<body>
    <form action="doSome.do" method="post">
        <span>测试处理器方法的返回值----返回String----逻辑路径</span>
        <br/>
        <span>姓名:</span><input type="text" name="name"/>
        <br/>
        <span>年龄:</span><input type="text" name="age"/>
        <br/>
        <input type="submit" value="提交"/>
    </form>

    <img src="images/img01.jpg">

    <a href="http://localhost:8080/springmvc_21080401/static/html/test.html">第二种方法</a>
</body>
</html>

MyController.java

package com.bgy.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
//@RequestMapping("/test")
public class MyController {
    @RequestMapping(value = "/doSome.do")
    public ModelAndView doSome(String name , Integer age){

        ModelAndView mv = new ModelAndView();
        mv.addObject("u_name",name);
        mv.addObject("u_age",age);
        mv.addObject("","message");

        mv.setViewName("show");

        System.out.println("doReturnStringView---->"+name+" : "+age);

        return mv;
    }
}

show.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>show</title>
</head>
<body>
    <span>message:${message}</span>
    <br/>
    <span>姓名:${u_name}</span>
    <br/>
    <span>年龄:${u_age}</span>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值