java - How to serve static content in spring mvc? -


i using spring-mvc framework in current project. root folder has lots of web folders containing html, jsp, css, js etc. spring mvc configuration follows:

<context:annotation-config />      <bean id="comiccontrollerbean" class="tv.cinemacraft.videogramwebapp.springmvc.controllers.comiccontroller" />     <bean id="dashboardcontrollerbean" class="tv.cinemacraft.videogramwebapp.springmvc.controllers.dashboardcontroller" />     <bean id="genericcontrollerbean" class="tv.cinemacraft.videogramwebapp.springmvc.controllers.genericcontroller" />     <bean id="channelcontrollerbean" class="tv.cinemacraft.videogramwebapp.springmvc.controllers.channelcontroller" />      <!-- <context:component-scan base-package="tv.cinemacraft.videogramwebapp.springmvc.controllers" /> -->      <bean class="org.springframework.web.servlet.view.internalresourceviewresolver">         <property name="prefix" value="/" />         <property name="suffix" value=".jsp" />         <property name="order" value="1" />     </bean>      <bean class="org.springframework.web.servlet.view.resourcebundleviewresolver">         <property name="basename" value="views" />         <property name="order" value="0" />     </bean> 

and have mapping defined in java controller. causing static content image vanish. if request image inside folder accessing root//.jpg, gives me 404 response code although image exists. if remove spring-mvc, image gets displayed.

note spring-mvc's resources attribute works static content need static content present inside particular folder e.g. resouce folder. approach not useful me.

let’s have directory (/resources/my_images/) contains product images, , want serve these images upon request. example, if requested url http://localhost:8080/ mystore/resource/my_images/p123.png, serve image p123.png name. similarly, if requested url http://localhost:8080/mystore/resource/images/p1234.png, image name p1234.png needs served.

now how can serve static images spring mvc ?

1. place images under src/main/webapp/resources/my_images/ directory; 2. add following tag in our web application context’s configuration dispatcherservlet-context.xml file: <mvc:resources location="/resources/" mapping="/resource/**"/> 3 run application , enter http://localhost:8080/mystore/resource/images/p123.png (change image name in url based on images placed

fyi: <mvc:resources> tag in web application context configuration tell spring these image files located in our project spring can serve files upon request.


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -