in a web app, I need to serve static contents (images) located outside the application context directory. The overall application architecture requires me to use Tomcat to perform this. I thought I could benefit from Spring's <mvc:resources> to configure a mapping between application URLs and directory contents. But AFAIK it's mapping attribute only handles context relative, or classpath mappings. Hence, what I'd like to use :
<mvc:resources location="/images/**" mapping="/absolute/path/to/image/dir"/>
doesn't work. As I'd rather avoid writing a simple file transfer servlet, I'd be glad if anyone could give me some pointers on existing Spring based solutions/workarounds.
Many thanks.
Homer
<mvc:resources> can serve resources from the outside, you need to use the usual Spring resource path syntax:
<mvc:resources mapping="/images/**" location="file:/absolute/path/to/image/dir/"/>
There is one more simple correction
the code should be
<mvc:resources mapping="/images/**" location="file:/absolute/path/to/image/dir/"/>
Did you notice the difference ?
You need to put '/' at the end of the absolute path.
or you can use the java configuration
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
String rootPath = System.getProperty("user.home");
String imagePath = "file:"+rootPath + File.separator + "tmpFiles/";
System.out.println(imagePath);
registry.addResourceHandler("/resources/**").addResourceLocations("resources/");
registry.addResourceHandler("/tmpFiles/**").addResourceLocations(imagePath);
}
Its working for me.
Related
its contains a tag mvc:resource tagI am developing a spring mvc application in which iam using xml based configuration.I want to load my static resource.To do this i am using a tag <mvc:resources location="/assets/" mapping="/resources/**"/>.
All my static resources is present in assets folder so i am giving the loaction. but in mapping why it is mapping="/resources/**"? I have not kept anything in resource folder and what does ** represents?
Can anyone clear all my doubts here?
<mvc:resources location="/assets/" mapping="/assets/**"/>
Update mapping="/assets/**", It will work fine. In Mapping attribute you should keep the matching url pattern to access the assets folder.
** :- Means multiple directories in this path or Zero directories after assets directory.
Take an example:-
<script type="text/javascript" src="${contextRoot}/assets/js/login.js"></script>
You want to add a java script file in your page. To add this js file, the server request become : http://localhost:8090/Aashayein/assets/js/login.js . As you are using spring-mvc so the request goes through dispatcher servlet(front controller). For this request Uri there is no corresponding handler marked (#RequestMapping("/assets/js/login.js")), so it results 404 error. So you have to tell spring that if any request comes with uri pattern "/assets/**" then directly looks to the assets folder present in the webapp directory. To do so in xml we have to write:-
<mvc:resources location="/assets/" mapping="/assets/**"/>
In annotation:-
/*
* Any request with url mapping /assets/** will directly look for /assets
* folder.
*/
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
registry.addResourceHandler("/assets/**").addResourceLocations("/assets/");
}
This is a bit of a silly and frustrating one:
The #Configuration is taken from a tutorial website or forum and in it a
ServletContextTemplateResolver thymeleafTemplateResolver
is created using the ServletContext provided by spring boot.
When requested, a FileNotFoundException is thrown, despite the file being in the configured resources folder.
How do I get it to find the file / load it from the resources?
For thymeleaf to resolve the classpath resources, you need to configure a ClassLoaderTemplateResolver. (You were using a ServletContextTemplateResolver)
Also check that setPrefix is set to the correct folder, eg. "/thymeleaf/" if your documents are in resources/thymeleaf/ and that setSuffix is set to ".html" (or whatever your preferred file suffix is)
To also serve static content, you can extend WebMvcConfigurer and override addResourceHandlers, to then do e.g.
registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
assuming a static folder in your resources.
(Spring controllers take precedence here)
I'm trying to use the Application Security on Cloud provided by Bluemix for a Spring application.
I'm relatively new to Spring and I'm having difficulty serving the requested verification file with the requested url. Bluemix wants the html file to be available at /IBMDomainVerification.html
However, I can't figure out how to serve the html file given that exact url using Spring. I can serve it without the .html at the end of the url but that's not what it needs.
If anyone can let me know how I can serve the html file given the specified url with the .html extension on the end that'd be great!
Thanks!
Spring 4.1 introduced the ResourceResolvers, that can resolve resources, given their URL path. In particular you could use the simplest: PathResourceResolver. This is the simplest resolver and its purpose is to find a resource given a public URL pattern. In fact, if no ResourceResolver is added to the ResourceChainRegistration, this is the default resolver.
Consider the following example:
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/resources/**")
.addResourceLocations("/resources/","/other-resources/")
.setCachePeriod(3600)
.resourceChain(true)
.addResolver(new PathResourceResolver());
}
The above code serves anything in either the webapp/resources of the webapp/other-resources folder, required with URLs /resources/** (e.g. /resources/foo.js). Note the two asterisks, it means that it will match everything after resources/, even '/'.
In your case using /IBMDomainVerification.html (/**) it should work.
An alternative way could be using *<mvc:resources/>* (Spring 3) which can be used to serve static resources while still using the DispatchServlet on root. Please refer to this SO answer.
I have an existing mvc application that works fine but when I add
<bean class="org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration"></bean>
to enable spring-data-rest then static resources which are served via
<mvc:resources mapping="/resources/**" location="/resources/" />
are not accessible.
Actually I get this in logs when requesting a static resource
2013-06-06 15:33:21,035 DEBUG DispatcherServlet.doService(823) - DispatcherServlet with name 'eips-databus' processing GET request for [/eips-databus/scripts/easyUI/themes/gray/easyui_custom.css]
2013-06-06 15:33:21,035 DEBUG RequestMappingHandlerMapping.getHandlerInternal(226) - Looking up handler method for path /scripts/easyUI/themes/gray/easyui_custom.css
2013-06-06 15:33:21,035 DEBUG ExceptionHandlerExceptionResolver.resolveException(132) - Resolving exception from handler [null]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
Any advice?
Thanks in advance
I'm faced with same with some differences. In my case I add my handler for static resources:
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("/WEB-INF/static/");
}
After that some static files with long path (like /a/b/c/d.css or /a/b/c/d.js) served as static content but some files (like /a/b/c.js) is not.
I found that in my case RepositoryRestHandlerMapping registers itself using Ordered.HIGHEST_PRECEDENCE and most of CSS and JavaScript requests handled into this handler by rules like [/{repository}/{id}/{property}].
This is a bug in Spring-Data, now this fixed (thanks to Oliver Gierke), you can get fixed version 2.0.0.BUILD-SNAPSHOT from snapshot repo and I hope this can help.
I've a problem with handle static resources.
It's my web-app
WebAppRoot
-resources
-css
-style.css
-imahes
-jsp
-template.jsp
-secure
-template_secure.jsp
so
myLocation=resources
How can I handle resource in a central way, for example I put location in a variable and
use this in tag
I tried with but I don't understand how have to use.
If you are using Spring 3.0.4 or later, you can use the < mvc:resources ...> element in your application configuration, and you will not need to create a Controller to handle static content for you.
It is as simple as specifying the location of your static content, and the path from which to access them:
<mvc:resources mapping="/resources/**" location="/public-resources/"/>
This uses the mvc namespace found http://static.springsource.org/schema/mvc/spring-mvc.xsd.