Reputation: 41
I am working on a Web Application using the Spring Framework in version 4.1.5
I have a resources folder configured and set up correctly and am already loading css, js and image files out of that.
<mvc:resources mapping="/images/**" location="/resources/" />
However, I am also using the resources folder for uploading images into it and displaying them in the frontend. And there's a point where I'm currently stuck and am not proceeding my understanding. I am getting 404 Error (resource not available), while others are working perfectly fine. The paths are all correct and the files are successfully uploaded, I am sure of that.
I have also seen the reverse effect - renaming or even removing resources out of the resources folder does not impact their accessibility over their web url. I am not copying any files to another folder during building the application, so I am working with those files in the resources folder. I also have looked for the filename on my filesystem and its not there anymore, so I assume there must be some cache in place.
Does Spring build an index or cache over the existing resource files at startup? Do I have to refresh any ApplicationContext after uploading files to notify the framework of changes in the file system and if so, how do I do that?
Hopefully someone can help me here :)
Thank you, Tom
EDIT: When calling an URL like: localhost:8080/EstelV2/images/testimage.jpg
I run into a 404 resource is not available, even though the testimage.jpg is in the correct folder structure on the file system and get the log output:
2015-04-30 22:13:05,035 - [DEBUG] - [http-nio-8080-exec-4] (DispatcherServlet.java:845) - DispatcherServlet with name 'spring-dispatcher' processing GET request for [/EstelV2/images/testimage.jpg]
2015-04-30 22:13:05,035 - [DEBUG] - [http-nio-8080-exec-4] (AbstractHandlerMethodMapping.java:297) - Looking up handler method for path /images/testimage.jpg
2015-04-30 22:13:05,041 - [DEBUG] - [http-nio-8080-exec-4] (AbstractHandlerMethodMapping.java:305) - Did not find handler method for [/images/testimage.jpg]
2015-04-30 22:13:05,041 - [DEBUG] - [http-nio-8080-exec-4] (AbstractUrlHandlerMapping.java:168) - Matching patterns for request [/images/testimage.jpg] are [/images/**]
2015-04-30 22:13:05,042 - [DEBUG] - [http-nio-8080-exec-4] (AbstractUrlHandlerMapping.java:193) - URI Template variables for request [/images/testimage.jpg] are {}
2015-04-30 22:13:05,042 - [DEBUG] - [http-nio-8080-exec-4] (AbstractUrlHandlerMapping.java:123) - Mapping [/images/testimage.jpg] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[ServletContext resource [/resources/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@280f257d]]] and 1 interceptor
2015-04-30 22:13:05,042 - [DEBUG] - [http-nio-8080-exec-4] (DispatcherServlet.java:931) - Last-Modified value for [/EstelV2/images/testimage.jpg] is: -1
2015-04-30 22:13:05,042 - [DEBUG] - [http-nio-8080-exec-4] (DispatcherServlet.java:1018) - Null ModelAndView returned to DispatcherServlet with name 'spring-dispatcher': assuming HandlerAdapter completed request handling
2015-04-30 22:13:05,042 - [DEBUG] - [http-nio-8080-exec-4] (FrameworkServlet.java:996) - Successfully completed request
Upvotes: 1
Views: 2300
Reputation: 41
Soo after hours of investigations I came to a conclusion:
My problem was not related to spring, it was a problem about my eclipse project. It seemed, as long as I didn't refresh my project in eclipse, the newly uploaded files were not accessible by the tomcat server - and the old ones which were removed from the file system but still were in the eclipse project kept being accessible.
I turned on both auto-refresh options at Preferences -> General -> Workspace -> "Refresh using native hooks and polling" and "Refresh on access", which solved my problem for now.
Thank you for your help!
Upvotes: 3
Reputation: 1009
servlet specification not mention that WEB_INF is Document root A special directory exists within the application hierarchy named WEB-INF. This directory contains all things related to the application that aren’t in the document root of the application. The WEB-INF node is not part of the public document tree of the application. 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, and may be exposed using the RequestDispatcher calls. so use it for secure your content only but for images put it in other folder in your web path
Upvotes: 1