Spring Static Resource Mapping

2019-08-19 04:09发布

I have been followed by this error since I started using Spring I can't handle static content

My dispatcher-servlet configuration

    <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>

My web configuration

<?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>

Finally the image I want to add is

<img src="<c:url value="/resources/captcha/13.PNG"/>"/>

But my log displays this:

01:19:10,801 DEBUG RequestMappingHandlerMapping:220 - Looking up handler method for path /resources/captcha/35.png

01:19:10,803 DEBUG RequestMappingHandlerMapping:230 - Did not find handler method for [/resources/captcha/35.png]

01:19:10,803 DEBUG SimpleUrlHandlerMapping:169 - Matching patterns for request [/resources/captcha/35.png] are [/**]

01:19:10,804 DEBUG SimpleUrlHandlerMapping:194 - URI Template variables for request [/resources/captcha/35.png] are {}

01:19:10,805 DEBUG SimpleUrlHandlerMapping:124 - Mapping [/resources/captcha/35.png] to HandlerExecutionChain with handler [org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler@eea824] and 1 interceptor

01:19:10,805 DEBUG DispatcherServlet:912 - Last-Modified value for [/project/resources/captcha/35.png] is: -1

01:19:10,805 DEBUG DispatcherServlet:999 - Null ModelAndView returned to DispatcherServlet with name 'dispatcher': assuming HandlerAdapter completed request handling

01:19:10,806 DEBUG DispatcherServlet:966 - Successfully completed request

Am I missing more configuration?

Thanks

1条回答
你好瞎i
2楼-- · 2019-08-19 04:48

When you try to access /resources/captcha/13.PNG

It is mapped to default servlet by virtue of

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.PNG</url-pattern>
</servlet-mapping>

It is not mapped to /resources/captcha/ because the resources looks for paths which have the pattern of type http://localhost:8080/captcha/* and not the ones with http://localhost:8080/resources/captcha/* so to handle such requests, you need to have a view resolver to respond to /resources/* pattern

something like

@RequestMapping(path = '/resources/*')
public Object handler(){...}

My guess is you want to render the PNG image as a static resource and you should probably have <img src="<c:url value="/resources/captcha/13.PNG"/>"/>.

NOTE: I am not sure what I suggested works or not. I am answering solely based on my memory and have not tested any of the situations you mentioned.

查看更多
登录 后发表回答