zoukankan      html  css  js  c++  java
  • Servlet拦截静态图片的解决方案

    一、现象

    建立一个使用Freemarker的Web Project程序。

    Product.ftl中的代码为:

     

    [html] view plain copy
     
    1. <!DOCTYPE html PUBLIC "-//W3C//DTDHTML 4.01Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
    2. <html>  
    3.   <head>  
    4.     <meta http-equiv="Content-Type"content="text/html; charset=UTF-8">  
    5.     <title>Insert title here</title>  
    6.   </head>  
    7.   <body>  
    8.     <h2>Hello World!</h2>  
    9.     <img src="/jade/images/a.jpg"/>  
    10.   </body>  
    11. </html>  

    web.xml中的代码为:

     

    [html] view plain copy
     
    1. <?xml version="1.0"encoding="UTF-8"?>  
    2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    3.     xmlns="http://xmlns.jcp.org/xml/ns/javaee"  
    4.     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaeehttp://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"  
    5.     id="WebApp_ID"version="3.1">  
    6.     <display-name>jade</display-name>  
    7.     <servlet>  
    8.         <servlet-name>spring</servlet-name>  
    9.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    10.         <load-on-startup>1</load-on-startup>  
    11.     </servlet>  
    12.     <servlet-mapping>  
    13.         <servlet-name>spring</servlet-name>  
    14.         <url-pattern>/</url-pattern>  
    15.     </servlet-mapping>  
    16. </web-app>  

    运行结果:

    这里图片无法正常显示。

    二、原因分析

    Web.xml中,Servlet的配置<url-pattern>/</url-pattern>,会对静态资源(比如jpg,css,js等)进行拦截。

    三、解决方案

    在web.xml中添加jsp相关的配置。添加后的完整内容如下:

     

    [html] view plain copy
     
    1. <?xml version="1.0"encoding="UTF-8"?>  
    2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    3.     xmlns="http://xmlns.jcp.org/xml/ns/javaee"  
    4.     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaeehttp://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"  
    5.     id="WebApp_ID"version="3.1">  
    6.     <display-name>jade</display-name>  
    7.     <servlet>  
    8.         <servlet-name>spring</servlet-name>  
    9.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    10.         <load-on-startup>1</load-on-startup>  
    11.     </servlet>  
    12.     <servlet-mapping>  
    13.         <servlet-name>spring</servlet-name>  
    14.         <url-pattern>/</url-pattern>  
    15.     </servlet-mapping>  
    16.     <servlet-mapping>  
    17.         <servlet-name>default</servlet-name>  
    18.         <url-pattern>*.jpg</url-pattern>  
    19.     </servlet-mapping>  
    20. </web-app>  

    运行结果:

  • 相关阅读:
    Android studio 报错 installation failed with message failed to finalize session:INSTALL_FAILED_INVALID_APK 解决方法
    Python 爬虫入门实例(爬取小米应用商店的top应用apk)
    解决jenkins控制台中文乱码问题
    四.Jenkins的授权和访问控制
    三.jenkins 在windows上配置master 和 agent(slave)
    Jenkins 默认没有Launch agent via Java Web Start,该如何配置
    二.jenkins构建自动化任务
    一.Jenkins的windows安装
    maven仓库地址配置
    jvm linux 时区设置
  • 原文地址:https://www.cnblogs.com/grimm/p/6732763.html
Copyright © 2011-2022 走看看