I'm using spring boot with thymeleaf and all my resources are outside spring application on a path like "/../../css/main.css". On dev env should resolve the path using an url and live env go on the path.
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
String templates=env.getProperty("spring.thymeleaf.prefix");
registry.addResourceHandler("/../../css/**")
.addResourceLocations(templates);
}
// spring.thymeleaf.prefix = http://website.com/assets/
Why the resource handler is not handling these kind of resources, but if I "/**" is handling without problems? Am I missing something?
Edit: if resourceHandler is "/css/**" and location is an url is not being handled either
You can't reference to external resources as you want cause resolver looking for resources from classpath.
Try to do it like in this issue - Add external resources folder to Spring Boot or in this - How do I use Spring Boot to serve static content located in Dropbox folder?
I am pretty sure that using .. in the addResourceHandler is not valid but I do not have specific documentation to back it up. The path is describing a pattern that the server gets not what is listed in the browser. From the function doc: "based on the specified URL path patterns". If you reference .. from a browser that will still be changed to some absolute path to be sent to the server. This is why your other versions work without issue.
Related
I'm working on a web service using spring-boot-starter-jersey and spring-boot-starter-tomcat (v. 1.5.2) and as such, I'd rather not add spring-boot-starter-web and further complicate my configuration. I want to stick the Swagger UI static files in a folder and serve them from there.
What I'm wondering is, can I serve static files using just spring-boot-starter-tomcat? I've found Spring documentation saying that I can server static content from a variety of sources on the classpath, but the examples I've found seem to require Spring MVC. I've tried disabling Jersey and putting static files in src/main/resources/static to test just Tomcat, but when I go to localhost/index.html, I get a 404 not found error.
As you might be able to tell from my path, I'm using Maven.
Since you can serve static files with just Tomcat, it seems like I should be able to serve static files with spring-boot-starter-tomcat. If this is the case, where do I put those files?
To put this another way, say I have started with the Spring-provided
spring-boot-sample-jersey project. I have a requirement that the Jersey web service answer calls to the root address (/). How would I add some static content (HTML, CSS, JS) to be served from subdirectory called /swagger?
So the default servlet (which serves static content) is by default registered. But it will use only search specific paths as the document root. I had to go digging through source code to finally find it. If you look in the AbstractEmbeddedServletContainerFactory, you'll see
private static final String[] COMMON_DOC_ROOTS = {
"src/main/webapp", "public", "static" };
If we don't explicitly set the document root, the above three are the paths that will be searched. In order, the first directory found that exists, will be used as the document root. I've verified that all of these work.
If you want to set a different directory, you can use a customizer bean
#Bean
public EmbeddedServletContainerCustomizer tomcatCustomizer() {
return new EmbeddedServletContainerCustomizer() {
#Override
public void customize(ConfigurableEmbeddedServletContainer container) {
container.setDocumentRoot(new File("src/main/resources/static"));
}
};
}
The one thing I haven't figured out is if we can serve files as classpath resources, instead of file system resources. If you look at the source code I linked to, it has some code that looks for the existence of a META-INF/resources. I thought that might work, but unfortunately it didn't for me. Maybe some guys of the Spring Boot team can enlighten us.
This is the the picture of my project.
I have my css file in static/css folder and I am trying to link it with my index.jsp page but it's not working.
NB : Hard refresh, cache clear everything is done and I have tried more than 10 times. :( is there any other way to link up css with spring boot application?
In the Spring-boot documentation you can read where Spring-boot reads static resources by default.
By default Spring Boot will serve static content from a directory
called /static (or /public or /resources or /META-INF/resources) in
the classpath or from the root of the ServletContext. It uses the
ResourceHttpRequestHandler from Spring MVC so you can modify that
behavior by adding your own WebMvcConfigurerAdapter and overriding the
addResourceHandlers method.
If you change the location of your static files to the default one you should be able to reach them by /css/style.css
P.S. here you can find a simple good structured example.
For me now I just put my CSS, JS, images in src/main/resources/static folder and the link is like :
/css/style.css or /js/custom.js
is working fine for spring boot application.
The solution to your problem is very simple, just add to your application.properties file (your configuration file) the next 2 lines of code:
spring.resources.chain.strategy.content.enabled=true
spring.resources.chain.strategy.content.paths=/**
this will bust the cache in your browser, and when you'll make changes regarding to static content (like: css files, js files, html files), it will take place.
Spring Boot CSS Showing up blank / not loading after trying everything
Good Luck.
I've got some software out in the wild that needs to download some static file from a specific place on my domain: domain.net/somefolder/setup.exe. While I plan to change this, it's going to take a long time due to constraints out of my control.
In the meantime, how can I deal with this in my Angular2 router? Essentially, if Angular sees this specific url it should not try to resolve the url and instead serve the static file similar to how it would behave if you accessed static files from the assets folder.
{ path: 'somefolder/setup.exe', ... }
I ended up just doing a url rewrite on the web server level within a config.
I have found many posts very similar to this, but I didn't find any that worked for me.
I have an asp.net Web Api2 (not vnext) application, running under IIS, and using the Owin Startup class.
When installed, the root url to this will be something like
http://localhost/appvirtualdirectory
where appvirtualdirectory is the name of the virtual directory it is configured to run under in IIS.
IS there a way at startup where I have no Request property, ie in the Startup.Configure method, to get the root URL including the virtual directory being used?
Try this
HttpRuntime.AppDomainAppVirtualPath
This is valid in both global and owin startup
More details here https://msdn.microsoft.com/en-us/library/system.web.httpruntime(v=vs.110).aspx
I would use:
//you have some options but i will show you the easiest
var request = HttpContext.Current.GetOwinContext().Request;
var path = request.Scheme +
Uri.SchemeDelimiter +
request.Host +
request.PathBase;
Info about the properties I'm referencing.
https://msdn.microsoft.com/en-us/library/microsoft.owin.owinrequest(v=vs.113).aspx
We are building a internal static asset server. Each environment (dev, staging, prod) has its own asset server, and the asset is reference throughout the web application (html, aspx, ascx, css, javascript, etc...)
To reference the correct asset server in the correct environment, one solution is to write a http module to intercept response before it gets to the client and change the URL according. I am just thinking that this might not be the most scalable solution since this http module is going to get executed for every request and basically parse the whole response (some are huge) before the client gets it.
I am also thinking to use a client side javascript to change the reference on client side, but this might not work as nicely has a http module.
Any thoughts? What's the industries best practice in ASP.NET?
I might create my own LinkToStaticAsset control. It would only accept the path relative to the static asset server of each asset. I would have it generate the full URL by including the base path from configuration.
If you want your site to function (even if only partially) without a Javascript dependency then you should keep this server-side.
An alternative approach to what you have mentioned above is to do this at the application level, i.e. have a library method which generates your static asset URL's and is configured to point at particular server(s) via your web.config.
It would go something like this:-
(In your App_Code folder or a referenced assembly)
public static class Util
{
public static string AssetUrl(string relativePath)
{
// returns asset server address from web config with relative path appended
}
}
(In web.config)
<appSettings>
<add key="AssetServerBaseUrl" value="http://foo.bar" />
...
</appSettings>
(In your aspx file)
<img src='<%= Util.AssetUrl("img/myimage.jpg") %>' ... />