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上进行微博OAuth登录
    Android应用集成支付宝接口的简化
    Tomcat启动时报 java.lang.OutOfMemoryError: Java heap space
    Myeclipse中 Exploded location overlaps an existing deployment解决办法
    SVN提交项目时版本冲突解决方案
    Jsp过滤器Filter配置过滤类型汇总
    myeclipse修改jsp文件的名称之后,再也打不开的解决方案
    文件上传之 commons-fileupload(二)
    文件上传之 commons-fileupload(一)
    常见池化技术
  • 原文地址:https://www.cnblogs.com/grimm/p/6732763.html
Copyright © 2011-2022 走看看