Where to place images/CSS in spring-mvc app?

后端 未结 7 1266
情话喂你
情话喂你 2020-12-05 08:14

I used Netbeans to create a Spring MVC 3.0 app. I have a simple controller and JSP view. The JSP view appears correctly except for an image that doesn\'t render. My direc

相关标签:
7条回答
  • 2020-12-05 08:46

    I believe it can only reference images/css in the WEB-INF folder. Moving your Images folder to WEB-INF should fix your problem.

    0 讨论(0)
  • 2020-12-05 08:47

    anything under Web-Inf folder will be marked as private and can not be access by the browser. You suppose to move the image folder outside Web-Inf and under webapp.

    0 讨论(0)
  • 2020-12-05 08:51

    Alter your web.xml file:

    <servlet>
        <servlet-name>spring-servlet</servlet-name>
        <servlet-class>com.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring-servlet</servlet-name>
        <url-pattern>/<url-pattern>
    </servlet-mapping>
    

    and add below configuration after it:

    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.css</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.js</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.gif</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.jpg</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.png</url-pattern>
    </servlet-mapping>
    

    If you have a better solution please share with me.

    0 讨论(0)
  • 2020-12-05 08:53

    For set image in jsp file in Spring MVC framework :

    You can simply set your image by following steps:

    Step 1. Place images folder in resources

    Step 2. write image path like : src="${pageContext.request.contextPath}/resources/images/logo.jpg"

    0 讨论(0)
  • 2020-12-05 08:57

    A bit old, but let me add my bit here. It is generally preferred to hide the .css/ .js files from direct public access by placing them into the main/java/webapp folder instead of the WEB-INF.

    The reasons are explained very well in the posts below:

    • What is WEB-INF used for in a Java web application?
    • How to access css/js files in jsps, all of them are placed in WEB-INF?? (spring forum)
    0 讨论(0)
  • 2020-12-05 08:59

    I think you can put all the static data such as image, css, javascripts etc out side the WEB-INF and then call it the normal way :)

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