自从我开始使用Spring之后,就出现了这个错误,我无法处理静态内容
我的dispatcher-servlet配置
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<import resource="applicationContext.xml"/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<mvc:resources mapping="/captcha/*" location="/resources/captcha/" />
<mvc:annotation-driven />
<mvc:default-servlet-handler/>
</beans>我的网页配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" 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://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.gif</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.PNG</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>Login.jsp</welcome-file>
</welcome-file-list>
</web-app>最后,我要添加的图像是
<img src="<c:url value="/resources/captcha/13.PNG"/>"/>但是我的日志显示如下:
01:19:10,801调试RequestMappingHandlerMapping:220 -查找路径/资源/captcha/35.png的处理程序方法
01:19:10,803调试RequestMappingHandlerMapping:230 -找不到/resources/captcha/35.png的处理程序方法
01:19:10,803调试SimpleUrlHandlerMapping:169 -请求/资源/captcha/35.png的匹配模式是/**
01:19:10,804调试SimpleUrlHandlerMapping:请求/资源/captcha/35.png的194- URI模板变量为{}
01:19:10,805调试SimpleUrlHandlerMapping:124 - and /resources/captcha/35.png到带有处理程序org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler@eea824和一个拦截器的HandlerExecutionChain
01:19:10,805调试DispatcherServlet:912 - /project/resources/captcha/35.png的最后修改值为:-1
01:19:10,805调试DispatcherServlet :999-Null ModelAndView返回给DispatcherServlet,名为“dispatcher”:假设HandlerAdapter完成了请求处理
01:19:10,806调试DispatcherServlet:966 -成功完成请求
我是不是错过了更多的配置?
谢谢
发布于 2014-03-10 08:04:30
当您尝试访问/resources/captcha/13.PNG时
它通过以下方式映射到默认servlet:
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.PNG</url-pattern>
</servlet-mapping>它没有映射到/resources/captcha/,因为资源查找具有http://localhost:8080/captcha/*类型的路径,而不是具有http://localhost:8080/resources/captcha/*类型的路径,因此要处理此类请求,需要有一个视图解析器来响应/resources/*模式。
就像这样
@RequestMapping(path = '/resources/*')
public Object handler(){...}我的猜测是,您希望将PNG映像呈现为静态资源,您可能应该拥有<img src="<c:url value="/resources/captcha/13.PNG"/>"/>。
注:我不知道我的建议是否有效。我的回答完全是基于我的记忆,没有测试你提到的任何情况。
https://stackoverflow.com/questions/22294406
复制相似问题