简体   繁体   English

如何在Spring MVC中投放静态内容?

[英]How to serve static content in spring mvc?

I am using spring-mvc framework in my current project. 我在当前项目中使用spring-mvc框架。 ROOT folder has lots of web folders containing html, jsp, css, js etc. My spring mvc configuration is as follows: ROOT文件夹有很多包含html,jsp,css,js等的网络文件夹。我的spring mvc配置如下:

<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 I have mapping defined in my java controller. 我在Java控制器中定义了映射。 But this is causing static content like any image to vanish. 但这会导致像任何图像一样的静态内容消失。 If I request for any image inside any folder by accessing ROOT//.jpg, it gives me 404 response code although image exists. 如果我通过访问ROOT //。jpg在任何文件夹中请求任何图像,尽管存在图像,但它会给出404响应代码。 If I remove spring-mvc, image gets displayed. 如果删除spring-mvc,则会显示图像。

Note that spring-mvc's resources attribute works for static content but that need my static content to be present inside particular folder eg resouce folder. 请注意,spring-mvc的resources属性适用于静态内容,但是需要我的静态内容出现在特定的文件夹(例如resouce文件夹)中。 So this approach is not useful for me. 因此,这种方法对我没有用。

Let's say you have a directory (/resources/my_images/) that contains some product images, and you want to serve these images upon request. 假设您有一个包含一些产品图片的目录(/resources/my_images/) ,并且您想根据请求提供这些图片。 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. 例如,如果所请求的URL是http://localhost:8080/ mystore/resource/my_images/P123.png ,那么您希望使用P123.png名称提供该图像。 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. 同样,如果请求的URL为http://localhost:8080/mystore/resource/images/P1234.png ,则需要提供名称为P1234.png的图像。

Now how you can serve static images with Spring MVC? 现在如何使用Spring MVC提供静态图像?

  1. Place some images under the src/main/webapp/resources/my_images/ directory; 将一些图像放在src/main/webapp/resources/my_images/目录下;
  2. Add the following tag in our web application context's configuration DispatcherServlet-context.xml file: <mvc:resources location="/resources/" mapping="/resource/**"/> 在我们的Web应用程序上下文的配置DispatcherServlet-context.xml文件中添加以下标记: <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 运行您的应用程序,然后输入http://localhost:8080/mystore/resource/images/P123.png (根据您放置的图像在URL中更改图像名称

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. 仅供参考:Web应用程序上下文配置中的<mvc:resources>标记告诉Spring这些图像文件在我们的项目中的位置,以便Spring可以根据请求提供这些文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM