Announcement Announcement Module
Collapse
No announcement yet.
Access resources in WEB-INF Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Access resources in WEB-INF

    Hi, Good day to u.

    I am new to spring. I try its web module. I put all the jsp file under WEB-INF directory. However, the jsp file fail to access the resources such as css, images, js .... meaning, the jsp page always fail to locate the resources. for example,

    <img src="<c:url value='/WEB-INF/images/img1.gif' />" />
    <img src="images/img1.gif" />

    This two tag return no images.

    How to make the resources available in my web application?

  • #2
    Try
    Code:
    <img src="<c&#58;url value='/images/img1.gif' />" />
    If this doesn't work, just view your HTML source and check it is pointing to the correct location.

    Comment


    • #3
      <img src="<c:url value='/images/img1.gif' />" />

      I try this and it return no image too. By viewing the html source, it is:
      <img src='/app/images/img1.gif' />

      Where 'app' is the web application root.

      i store my image in d:/proj/app/WEB-INF/images/img1.gif.

      Any idea?

      Comment


      • #4
        i store my image in d:/proj/app/WEB-INF/images/img1.gif.
        resources stored under /WEB-INF/ can not be accessed directely from the web agent. You should consider moving the images folder to the application root d:/proj/app/images/img1.gif and then use katentim code:
        Code:
        <img src="<c&#58;url value='/images/img1.gif' />" />

        Comment


        • #5
          Originally posted by irbouho
          i store my image in d:/proj/app/WEB-INF/images/img1.gif.
          resources stored under /WEB-INF/ can not be accessed directely from the web agent. You should consider moving the images folder to the application root d:/proj/app/images/img1.gif and then use katentim code:
          Code:
          <img src="<c&#58;url value='/images/img1.gif' />" />
          However, i saw some commercial framework, which use webmacro as view technology, have achieved this: all the images is stored under WEB-INF directory. And it can access the image just like this:

          <img src="images/img1.gif" />

          So, i just want to know how to achieve this using spring. Is it possible?

          Comment


          • #6
            i saw some commercial framework, which use webmacro as view technology, have achieved this: all the images is stored under WEB-INF directory
            If it worked, it is in violation of the spec. and won't be portable - the whole point of putting JSPs there is so they can't be accessed directly - forcing you to go via a controller (i.e. helping enforce MVC). Static content such as images and CSS MUST be accessed directly (unless you want to read them in from a servlet and stream them out).

            Java™ Servlet Specification Version 2.3
            SRV.9.5 Directory Structure
            No file contained in the WEB-INF directory may be served directly to a client by the container. However, the contents of the WEB-INF directory are visible to servlet code using the getResource and getResourceAsStream method calls on the ServletContext.

            Comment


            • #7
              Originally posted by katentim
              i saw some commercial framework, which use webmacro as view technology, have achieved this: all the images is stored under WEB-INF directory
              If it worked, it is in violation of the spec. and won't be portable - the whole point of putting JSPs there is so they can't be accessed directly - forcing you to go via a controller (i.e. helping enforce MVC). Static content such as images and CSS MUST be accessed directly (unless you want to read them in from a servlet and stream them out).
              That's not quite true. It's quite possible to set up a filter (or a servlet for that matter) that is mapped with /images/* that forwards to /WEB-INF/images/ directory. While it's true that the browser can't directly access the /WEB-INF/ directory you can create code on the server to allow it. And yes it will work for images and CSS without violating the servlet spec.

              /Scott

              Comment


              • #8
                Scott,

                katentim talks about the "standard" behaviour, which means how a J2EE compliant Web Server should perform out of the box. Using Filters / Specific Servlets, you can, of course, do whatever you want, may be not everything...

                I do not see, however, why you need to configure a filter to serve images / css from /WEB-INF/. Unless you have a real need, this is just putting more complexity into your architecture.

                Comment


                • #9
                  REAL need

                  The commercial framework is Electronic Document Management System (EDMS), which used to manage scanned document (which is in jpeg, gif format).

                  So this framework allow to access the images in WEB-INF. And yes, they uses filter, after i have do a research to this framework and i found that.

                  REAL need, may be it protect the images from downloading? What is meant by REAL need? Any example? just curious...

                  Comment


                  • #10
                    Re: REAL need

                    Originally posted by dev001
                    REAL need, may be it protect the images from downloading? What is meant by REAL need? Any example? just curious...
                    Besides protecting images from unauthorized use, another potential reason might be do deliver a modified css depending on the who the user is.

                    But I agree with Omar. Unless there's a need for it, it's probably best to just place them in a images or css directory at the root of web application. You can always create a filter later that protects the directories from direct access (even if they aren't in the WEB-INF directory).

                    /Scott

                    Comment


                    • #11
                      Thanks for all the information provided!

                      Comment

                      Working...
                      X