Not displaying images in Spring MVC

后端 未结 6 700
闹比i
闹比i 2020-12-16 04:07

I know this question has been asked many times, but I\'m not able to figure out what the problem is. I have the images folder under the src/main/webapp folder (this a maven

相关标签:
6条回答
  • 2020-12-16 04:32

    The above suggestions worked for me as well. But if anyone else has trouble with the associated namespace, I had to add the mvc portion to mvc-dispatcher-serlvet.xml

    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd ">
    
    ...
    
    <mvc:annotation-driven /> 
    <mvc:resources mapping="/images/**" location="/images/" />
    
    0 讨论(0)
  • 2020-12-16 04:34

    Try adding the following resources declaration to your Spring configuration:

    <!-- Handles HTTP GET requests for /images/** by efficiently serving up static resources in the ${webappRoot}/images directory -->
    <resources mapping="/images/**" location="/images/" />    
    

    Alternatively, and more common, is to have a resources folder which contains all your resources (images, css, js, etc...), broken out by sub-directories.

    Your configuration would then look like:

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />
    

    And your resources would be referenced as follows:

    <link rel="stylesheet" type="text/css" href="<c:url value="/resources/css/screen.css" />" />
    <script type="text/javascript" src="<c:url value="/resources/js/jquery-1.6.4.min.js" />"></script>
    <img src="<c:url value="/resources/images/left_arrow.png" />" alt="" />
    
    0 讨论(0)
  • 2020-12-16 04:34

    Please follow the steps in this picture.. :)

    Step 1: Create a folder in webapp but not in WEB-INF

    Create Resources then images then store your image. webapp/resources/images/fileName.jpg

    Step 2: Now that you have created your folders

    Let us map the path you created in the servlet configuration file where we deal with mapping of the paths Add this code : <mvc:resources mapping="/resources/*" location="/resources/" />

    Step 3: Add the code for accessing the image resource from the location you created in step 1: <img src="/attendance/resources/images/logo.png" width="100px" height="100px">

    0 讨论(0)
  • 2020-12-16 04:37

    You just need to add a reference your image folder on Spring MVC configuration file

    WEB-INF/spring-context.xml:

    <mvc:resources mapping="/images/*" location="/images/" />
    
    0 讨论(0)
  • 2020-12-16 04:41

    If Using annotation then make sure to user

    <mvc:annotation-driven/>
    

    with resources

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

    else annotaion controller will not work

    0 讨论(0)
  • 2020-12-16 04:48

    If you want to keep the static resources outside the WEB-INF folders in your web root and want the container to handle the static resource requests, you should add this to your application context file:

    <mvc:default-servlet-handler />
    

    The suggestion by @BeauGrantham to add resources mapping will also work.

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