自从我开始使用Spring以来,我遇到了这个错误 我无法处理静态内容
我的调度程序 - 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 DEBUG RequestMappingHandlerMapping:220 - 查找path /resources/captcha/35.png的处理程序方法
01:19:10,803 DEBUG RequestMappingHandlerMapping:230 - 找不到[/resources/captcha/35.png]的处理程序方法
01:19:10,803 DEBUG SimpleUrlHandlerMapping:169 - 请求的匹配模式[/resources/captcha/35.png]是[/ **]
01:19:10,804 DEBUG SimpleUrlHandlerMapping:194 - 请求[/resources/captcha/35.png]的URI模板变量是{}
01:19:10,805 DEBUG SimpleUrlHandlerMapping:124 - 使用handler [org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler@eea824]和1个拦截器将[/resources/captcha/35.png]映射到HandlerExecutionChain
01:19:10,805 DEBUG DispatcherServlet:912 - [/project/resources/captcha/35.png]的Last-Modified值为:-1
01:19:10,805 DEBUG DispatcherServlet:999 - Null ModelAndView返回DispatcherServlet,名称为'dispatcher':假设HandlerAdapter已完成请求处理
01:19:10,806 DEBUG DispatcherServlet:966 - 已成功完成请求
我错过了更多配置吗?
由于
答案 0 :(得分:1)
当您尝试访问时
/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"/>"/>
。
注意:我不确定我的建议是否有效。我的回答完全取决于我的记忆,并没有测试你提到的任何情况。