Spring 3 MVC resources and tag

2019-01-04 01:59发布

I'm having some problems with the tag (Spring 3.0.5). I want to add images to my web application, but it doesnt work.

Here is part of my beans config:

<mvc:annotation-driven/>
<mvc:default-servlet-handler default-servlet-name="ideafactory"/>
<mvc:resources mapping="/resources/**" location="/, classpath:/WEB-INF/public-resources/" cache-period="10000" />

Trying to add an image in a jsp file:

<img src="<c:url value="/resources/logo.png" />" alt="Idea Factory" />

First of all, I don't know really where to store the resources (src/main/resources/public-resources? src/main/webapp/WEB-INF/public-resources?). Secondly, this config does not work, I can't see the image. What's wrong?

Thanks!

EDIT: the solution given here: Spring Tomcat and static resources and mvc:resources doesn't work either... Added without success.

EDIT 2: I tried to remove the mvc:resource tag and let only the mvc:default-servlet-handler> one, gave me infinite loop and stackoverflow... o_O (Serving static content with Spring 3)

9条回答
聊天终结者
2楼-- · 2019-01-04 02:26

Recommendations for resources in order to handle HTTP GET requests for /resources/** by offering static resources in the ${webappRoot}/resources directory is to simply add the following line in the configuration file:

<resources mapping="/resources/**" location="/resources/" />

It has worked for me.

Sources (Spring in Action book and http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html)

查看更多
来,给爷笑一个
3楼-- · 2019-01-04 02:27

Found the error:

Final xxx-servlet.xml config:

<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/" />

Image in src/webapp/resources/logo.png

Works!

查看更多
戒情不戒烟
4楼-- · 2019-01-04 02:30

It works for me:

<mvc:resources mapping="/static/**" location="/static/"/>
<mvc:default-servlet-handler />
<mvc:annotation-driven />
查看更多
Fickle 薄情
5楼-- · 2019-01-04 02:33
<mvc:resources mapping="/resources/**"
               location="/, classpath:/WEB-INF/public-resources/"
               cache-period="10000" />

Put the resources under: src/main/webapp/images/logo.png and then access them via /resources/images/logo.png.

In the war they will be then located at images/logo.png. So the first location (/) form mvc:resources will pick them up.

The second location (classpath:/WEB-INF/public-resources/) in mvc:resources (looks like you used some roo based template) can be to expose resources (for example js-files) form jars, if they are located in the directory WEB-INF/public-resources in the jar.

查看更多
仙女界的扛把子
6楼-- · 2019-01-04 02:36

@Nanocom's answer works for me. It may be that lines have to be at the end, or could be because has to be after of some the bean class like this:

<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />
<bean class="org.springframework.web.servlet.resource.ResourceHttpRequestHandler" />    

<mvc:resources mapping="/resources/**" 
               location="/resources/" 
               cache-period="10000" />
查看更多
等我变得足够好
7楼-- · 2019-01-04 02:39

As said by @Nancom

<mvc:resources location="/resources/" mapping="/resource/**"/>

So for clarity lets our image is in

resources/images/logo.png"

The location attribute of the tag defines the base directory location of static resources that you want to serve. It can be images path that are available under the src/main/webapp/resources/images/ directory; you may wonder why we have given only /resources/ as the location value instead of src/main/webapp/resources/images/. This is because we consider the resources directory as the base directory for all resources, we can have multiple subdirectories under resources directory to put our images and other static resource files.

The second attribute, mapping, just indicates the request path that needs to be mapped to this resource directory. In our case, we have assigned /resources/** as the mapping value. So, if any web request starts with the /resource request path, then it will be mapped to the resources directory, and the /** symbol indicates the recursive look for any resource files underneath the base resource directory.

So for url like http://localhost:8080/webstore/resource/images/logo.png. So, while serving this web request, Spring MVC will consider /resource/images/logo.png as the request path. So, it will try to map /resource to the resource base directory, resources. From this directory, it will try to look for the remaining path of the URL, which is /images/logo.png. Since we have the images directory under the resources directory, Spring can easily locate the image file from the images directory.

So

 <mvc:resources location="/resources/" mapping="/resource/**"/>

gives us for given [requests] -> [resource mapping]:

http://localhost:8080/webstore/resource/images/logo.png -> searches in resource/images/logo.png

http://localhost:8080/webstore/resource/images/small/picture.png -> searches in resource/images/small/picture.png

http://localhost:8080/webstore/resource/css/main.css -> searches in resource/css/main.css

http://localhost:8080/webstore/resource/pdf/index.pdf -> searches in resource/pdf/index.pdf

查看更多
登录 后发表回答