I have an integration test (annotated with #WebAppConfiguration) that needs some additional javascript/html files to be served by the application. I'd like to put those files under src/test/webapp but the content of that directory is not loaded.
I've seen that the WebAppConfiguration annotation allow to override the default src/main/webapp path, but I don't need to completely replace that path.
Is there a way to get what I need?
By default, Spring Boot will serve static files from a folder called /static (or /public or /resources or /META-INF/resources) on the classpath. Placing your additional JavaScript and HTML files in src/test/resources/static should make them available during your integration tests.
Related
I have a WAR (being served through Embedded Jetty) that I'd like to include static files outside the WAR. My goals are:
Serve static files from a path outside the WAR, relative to the directory the Embedded Jetty is running. So, if I invoke the jar in /srv/app, I'd like to configure a URL of http://myapp.com/static/js/my.js to serve /src/app/public/js/my.js.
Secure: No directory listings or ../ path escapes
Not reinvent the wheel: I don't want to write a new Servlet, but rather harness existing capabilities
Ideally (not required): Shadow the static content in the WAR, so that if I have a /src/app/public/index.html, that file will be served, and, if not existent, the WAR's index.html will be served.
Here's what I examined, and where I'm stuck:
DefaultServlet: Seems ideal, but, I can't find any way to have it serve files outside the WAR. Is there a way for the DefaultServlet to serve static content outside the WAR, with paths relative to the app's pwd?
Configuring Jetty, as described in https://www.eclipse.org/jetty/documentation/current/static-content-deployment.html ; this would seem to be ideal, but, when copied in, didn't seem to do anything (it was a NOOP).
ResourceHandler: Is this the right way to do it? I wasn't able to figure out how to use this to make my goals
ResourceHandler: Is this the right way to do it? I wasn't able to figure out how to use this to make my goals
Don't use ResourceHandler, its inferior to DefaultServlet.
Configuring Jetty, as described in https://www.eclipse.org/jetty/documentation/current/static-content-deployment.html ; this would seem to be ideal, but, when copied in, didn't seem to do anything (it was a NOOP).
This only works if you don't share the same contextPath as your deployed webapp.
Is there a way for the DefaultServlet to serve static content outside the WAR, with paths relative to the app's pwd?
Yes, and this has been documented on stackoverflow in a few different ways.
Key is, you'll use extra DefaultServlet entries, defined with their own "Resource Base Path" (which has to be a fully qualified path entry, no relative paths), on different url-patterns.
See:
Serving static files from alternate path in embedded Jetty
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 have some folders in www/web/ which is the root.
It's the following folder: assets/exports/
And it contains a file export.xsl
When I do in javascript:
window.open('/assets/exports/export.xsl');
I'm going to the following link:
http://mywebsite/assets/exports/export.xsl
But I get a: 404 not found
Is symfony somehow protecting this link?
So, my question is, how can I access this file, so it starts downloading for the visitor?
From Symfony Documentation:
Keep in mind that web/ is a public directory and that anything stored here will be publicly accessible, including all the original asset files (e.g. Sass, LESS and CoffeeScript files).
Make sure you put the files in a proper directory: <symfony_root_dir>/web. See below.
Then accessing the http://mywebsite/assets/exports/export.xsl returns the file's content.
Check also your server configuration, virtual host config and read web server configuration guide from Symfony to see if you configured it properly.
I have symfony web files in web root folder where the project is running. I have site files such as index.html, aboutus.html and contactus.html files. I have put these files into public_html/site/ How to access these files by default using routing.yml? also project should run in index.php
Hm...If you want to add to your project some static pages, I would recommend you this method.I think this is the right way. For index.php ,well read this
Why don't you make a new module for static pages and move the html content into it?
You have to understand that routing system in symfony is used for connect requested urls to controllers. So you can't use it to provide generating urls for static pages outside of your symfony application. That files are just like files of foreign site.
So you can just use link_to('about us', $this->getRequest()->getHost().'/about_us.html');
I am looking for a code based solution to only serving static files from a specified directory using ASP.NET routing rather than specify a HttpHandler in the Web.config.
For example:
I have the following partial directory layout
/
/public
/public/hello.txt
/public/css
/public/css/base.css
/readme.txt
If any files are requested from the /public directory they should be served as-is. If any other static files are requested for example /readme.txt it should be served as a 404. The directory which allows static files to be served from should be easily specified as a string (var publicDir = "public";).
I am looking to do this with little to no configuration required in the Web.config file, and most of it configured in code.
I don't think it would be possible to achieve a solution using a class library. In IIS 6 environment files with .TXT or .JPG extension are served without going through .NET Framework. The only way is changing IIS configuration.
In IIS 7 things are handled differently but I have no idea if it is possible to find a solution that works in all environments or not.
I'll be pleased to hear what others say about this and correct me if i'm worng.