How handle resources likes images, *.css with Spring Mvc - spring

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.

Related

As i ahve not kept any file in resource folder why we need to map to resource folder to load all static Resource?

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/");
}

Thymeleaf +Spring Boot can't find files (FileNotFoundException)

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)

Static resource cache issue - Spring MVC

I have a spring MVC based application, where I have used spring MVC caching for static resource.
<mvc:resources location="/, classpath:/META-INF/web-resources/" mapping="/resources/**"
cache-period="${RESOURCE_CACHE_PERIOD}"/>
The RESOURCE_CACHE_PERIOD is property in a properties file with value as 2 (for development purpose).
The static resources such as .js files remain in cache after even change and I have to publish my application again to see the changes.
What might be the issue?
I suspect you are changing the resources in your local code but not within the web container that is running your app.
Can you try altering the js from within the webapp that is actually running?

How to import css/js in spring MVC application jsp page

I am new to spring mvc , I am creating a user registration page , there I need to put external css and import external js .
I put the script tag with src and link tag with href. But i think in Spring MVC application they will not work straight forward.
For this I added resourceservlet mapping but still it is not working
I am not able to find any way how I csn include these assets in my jsp file.
Please help me to get this one.
Regards,
Pranav
Put the CSS, JS, image files, etc., into a directory (which may contain subdirectories) in your web site, and than map the requests to that directory:
<mvc:resources mapping="/resources/**" location="/public-resources/"/>
In this example the files are in the public-resources directory, and you refer to them using /resources in the link in your JSP.
For more information see Spring docs

Spring : serving static resources outside context root

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.

Resources