spring - disable all caching for static content - spring

how can I disable caching of the static content?
I tried to put this in my applications.properties:
spring.cache.type=NONE
This is my config:
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/**")
.addResourceLocations("classpath:/static/")
.resourceChain(true).addResolver(
new VersionResourceResolver().addContentVersionStrategy("/**"));
}
Still when I change something in the css file I have to reload the page with the developer console opened in order for it to show.
Thanks!

Move all the resources out of classpath. To replace something loaded to classpath you may need something complex like own class loader etc. Try to move to a separate folder all the resources you need to change.

Related

Spring MVC + Thymeleaf: not loading some resources

I am new to Thymeleaf and Spring MVC.
I have been dealing with the following problem: some resources (css or images) don't get loaded by my webpage while other do. They are in the same path and folder, the syntax is the same (i have checked by just switching the name of the resource and it worked).
For example, my Thymeleaf Template can find and read my own css files, but it won't read the bootstrap-4 one.
Here is my project structure:
And here an example of the code trying to read bootstrap.css:
The same problem happens with images of the same format.
Any ideas of what could be causing the issue?
Thank you in advance
You can register a resource handler by extending a WebMvcConfigurerAdapter.
Something like this.
#EnableWebMvc
#Configuration
public class SpringWebConfig extends WebMvcConfigurerAdapter {
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/css/**").addResourceLocations("/css/");
registry.addResourceHandler("/styles/**").addResourceLocations("/styles/");
registry.addResourceHandler("/js/**").addResourceLocations("/js/");
registry.addResourceHandler("/images/**").addResourceLocations("/images/");
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
}
Anyway, the css files should be in the css directory
Okay so I understood what the problem was: intelliJ.
It didn't see the new files added to the project.
Running mvn clean install fixed the problem

How to map index.html to root context in spring boot application?

I have created a folder static inside the resources folder of my Spring Boot application. Inside the static folder I have an index.html file. Then I added this to my configuration:
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**")
.addResourceLocations("classpath:/static/");
}
Now I can access index.html at localhost:8080/index.html, but what I want to do is to access it at localhost:8080. I have tried this already:
#Override
protected void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/login").setViewName("forward:/index.html");
}
But I get that "forward:/index.html"is not recognized by ServletDispatcher. Can someone help me with this? Thanks in advance.
EDIT
I solved the problem by deleting all my custom configuration, so the default configuration is available to my app. In my particular case, this works for me, but it wouldn't be bad to know how to achieve this with custom configurations. So this question is still open, everybody is welcome to give some input on the subject.
I think it is not necessary to specify the extension and forward. You can try this:
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/login").setViewName("index");
}

Static resources arent loaded in thymeleaf template

I've gone ahead and tried almost any tutorial with the hopes it wouldfix this.
I'm new to spring boot.
So I have a spring boot web application setup, but css, jscript and any other static content won't be loaded in template. It's not a problem with the css or jscript as implementing them directly into the html file will make it work.
This (http://prntscr.com/lk6f6q) is how my project looks like. "test".js just includes a simple alert call.
Html: https://hastebin.com/ixejakiqev.xml
Pom: https://hastebin.com/vakinawuva.xml
What am I doing wrong? I'm trying to solve this since a week and nothing seems to work. Am I maybe missing a library?
Check the exact issue using network tab of the browser.
and also ensure that you have a class something like below to handle static resources.
#Configuration
#EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler(
"/webjars/**",
"/img/**",
"/css/**",
"/js/**")
.addResourceLocations(
"classpath:/META-INF/resources/webjars/",
"classpath:/static/img/",
"classpath:/static/css/",
"classpath:/static/js/");
}
}
The security config did not allow unverified access to the static resources.

Unable to access the html file in template folder of spring boot application

html files are placed under resources/templates/login.html directory of spring boot application(show in the screenshot), deployed it in the weblogic server and when I try to access the login.html with the below URL, it gives The webpage cannot be found message
http://localhost:7001/demo/login.html
below is the screenshot
In one of the post I found the below code snippet and tried, but it didn't work
#Configuration
public class StaticResourceConfiguration extends WebMvcConfigurerAdapter {
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
"classpath:/META-INF/resources/", "classpath:/resources/",
"classpath:/static/", "classpath:/public/" };
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS);
}
I am not getting what mistake I did, Could some one help me regarding this ...?
Spring Boot by default serves all content found under /static, /public, /resources or /META-INF/resources, see docs. So all content in your static folder should be served well (check that). But the templates folder is not a sub-folder of the static, so it will not be served. If I get you right, the templates is not supposed to be part of the URL path, right? So you could either move your login.html to the static folder, or you could add the templates folder to the classpath resource locations. Either programmatically (as you did for the other locations), or by setting the corresponding property:
spring.resources.static-locations=classpath:/templates/,classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/

Spring MVC: Is regular expression possible to map static contents in WebMvcConfigurerAdapter

I want to make a dynamic key for my static resource, here is my application
I have a web application (Spring MVC), most of its jsp pages serve static resource like images, js and css etc, These resources are cached. So whenever I modify css or js, I just change that key so that web page request for files on different new path.
#Configuration
#EnableWebMvc
#ComponentScan(basePackages = { "com.my.package" })
public class AppConfig extends WebMvcConfigurerAdapter {
//... some code ...
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/here-is-a-key/resource/**")
.addResourceLocations("/static/")
.setCachePeriod(2592000);//30 days
}
//... some code ...
}
This will serve static resource like
/static/here-is-a-key/resource/js/app.js
Whenever I make changes in static resources (images,css, js), I also change the key
This helps browser or webpage to request fresh resources. (not cached)
It works perfect,
but issue comes for the servers or browsers that caches the html page(google etc), in that cached html page the key saved was old-key which does not map anything anymore, and 404 exception is thrown.
I want this part to be ignored some wild-card functionality
registry.addResourceHandler("/static/(.*?)/resource/**")
.addResourceLocations("/static/")
so that it matches cached path as well as new path.
So any contents like
/static/key001/resources/** get mapped to /static/
/static/key002/resources/** also mapped on /static/

Resources