How to serve static content in spring mvc?

前端 未结 1 348
囚心锁ツ
囚心锁ツ 2021-01-17 01:22

I am using spring-mvc framework in my current project. ROOT folder has lots of web folders containing html, jsp, css, js etc. My spring mvc configuration is as follows:

相关标签:
1条回答
  • 2021-01-17 01:47

    Let’s say you have a directory (/resources/my_images/) that contains some product images, and you want to serve these images upon request. For example, if the requested URL is http://localhost:8080/ mystore/resource/my_images/P123.png, then you would like to serve the image with the P123.png name. Similarly, if the requested URL is http://localhost:8080/mystore/resource/images/P1234.png, then an image with the name P1234.png needs to be served.

    Now how you can serve static images with Spring MVC?

    1. Place some images under the src/main/webapp/resources/my_images/ directory;
    2. Add the following tag in our web application context’s configuration DispatcherServlet-context.xml file: <mvc:resources location="/resources/" mapping="/resource/**"/>
    3. run your application and enter http://localhost:8080/mystore/resource/images/P123.png (change the image name in the URL based on the images you placed

    FYI: <mvc:resources> tag in the web application context configuration to tell Spring where these image files are located in our project so that spring can serve those files upon request.

    0 讨论(0)
提交回复
热议问题