一、现象
建立一个使用Freemarker的Web Project程序。
Product.ftl中的代码为:
 
- <!DOCTYPE html PUBLIC "-//W3C//DTDHTML 4.01Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
- <html>  
-   <head>  
-     <meta http-equiv="Content-Type"content="text/html; charset=UTF-8">  
-     <title>Insert title here</title>  
-   </head>  
-   <body>  
-     <h2>Hello World!</h2>  
-     <img src="/jade/images/a.jpg"/>  
-   </body>  
- </html>  
 
 
 
web.xml中的代码为:
 
- <?xml version="1.0"encoding="UTF-8"?>  
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
-     xmlns="http://xmlns.jcp.org/xml/ns/javaee"  
-     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaeehttp://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"  
-     id="WebApp_ID"version="3.1">  
-     <display-name>jade</display-name>  
-     <servlet>  
-         <servlet-name>spring</servlet-name>  
-         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
-         <load-on-startup>1</load-on-startup>  
-     </servlet>  
-     <servlet-mapping>  
-         <servlet-name>spring</servlet-name>  
-         <url-pattern>/</url-pattern>  
-     </servlet-mapping>  
- </web-app>  
 
 
 
运行结果:

 
这里图片无法正常显示。
 
二、原因分析
Web.xml中,Servlet的配置<url-pattern>/</url-pattern>,会对静态资源(比如jpg,css,js等)进行拦截。
 
三、解决方案
在web.xml中添加jsp相关的配置。添加后的完整内容如下:
 
- <?xml version="1.0"encoding="UTF-8"?>  
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
-     xmlns="http://xmlns.jcp.org/xml/ns/javaee"  
-     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaeehttp://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"  
-     id="WebApp_ID"version="3.1">  
-     <display-name>jade</display-name>  
-     <servlet>  
-         <servlet-name>spring</servlet-name>  
-         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
-         <load-on-startup>1</load-on-startup>  
-     </servlet>  
-     <servlet-mapping>  
-         <servlet-name>spring</servlet-name>  
-         <url-pattern>/</url-pattern>  
-     </servlet-mapping>  
-     <servlet-mapping>  
-         <servlet-name>default</servlet-name>  
-         <url-pattern>*.jpg</url-pattern>  
-     </servlet-mapping>  
- </web-app>  
 
 
 
运行结果:

Servlet拦截静态图片的解决方案
原文:http://www.cnblogs.com/grimm/p/6732763.html