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>  

    运行结果:

  • 相关阅读:
    powerpc调试工具的使用
    微软的开发工具是我见过比较难用的
    Spring之jdbcTemplate:查询的三种方式(单个值、单个对象、对象集合)
    静态工厂方法和实例工厂方法
    理解.NET Framework
    .NET相关的概念简介
    二、文档工具 swagger
    Java Web学习(二)数据加密
    Java Web学习(一)Web基础
    Oracle学习(十二)标量函数
  • 原文地址:https://www.cnblogs.com/grimm/p/6732763.html
Copyright © 2011-2022 走看看