如何在Spring MVC中投放静态内容? [英] How to serve static content in spring mvc?

查看:108
本文介绍了如何在Spring MVC中投放静态内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在当前项目中使用spring-mvc框架. ROOT文件夹有很多包含html,jsp,css,js等的Web文件夹.我的spring mvc配置如下:

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:

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

我在Java控制器中定义了映射.但这会导致像任何图像一样的静态内容消失.如果我通过访问ROOT//.jpg在任何文件夹中请求任何图像,尽管存在图像,但它会给出404响应代码. 如果删除spring-mvc,则会显示图像.

And I have mapping defined in my java controller. 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. If I remove spring-mvc, image gets displayed.

请注意,spring-mvc的resources属性适用于静态内容,但需要将我的静态内容显示在特定的文件夹中,例如重新分配文件夹.因此,这种方法对我没有用.

Note that spring-mvc's resources attribute works for static content but that need my static content to be present inside particular folder e.g. resouce folder. So this approach is not useful for me.

推荐答案

假设您有一个包含某些产品图片的目录(/resources/my_images/),并且您想根据要求提供这些图片.例如,如果请求的URL是http://localhost:8080/ mystore/resource/my_images/P123.png,则您想使用P123.png名称提供图像.同样,如果请求的URL为http://localhost:8080/mystore/resource/images/P1234.png,则需要提供名称为P1234.png的图像.

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.

现在如何使用Spring MVC提供静态图像?

  1. 将一些图像放置在src/main/webapp/resources/my_images/目录下;
  2. 在我们的Web应用程序上下文的配置中添加以下标记 DispatcherServlet-context.xml文件:<mvc:resources location="/resources/" mapping="/resource/**"/>
  3. 运行您的应用程序并输入 http://localhost:8080/mystore/resource/images/P123.png(更改 网址中基于您放置的图像的图像名称
  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:Web应用程序上下文配置中的<mvc:resources>标记可以告诉Spring这些图像文件在我们项目中的位置,以便Spring可以根据请求提供这些文件.

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.

这篇关于如何在Spring MVC中投放静态内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆