Add Static File
Dynamic web
applications also require static files, typically CSS
and JavaScript
files. Ideally your server is already configured to serve your static files. Blade can do the job well during the development process. We create a static resource file in the resources
directory called static
, which is located in the /static
of the application.
Blade defaults to
static
,assets
,webjars
,upload
folders as static resource directories. You can also customize a directory, here we use thestatic
directory as the storage.
Prepare a few pictures or put css
, js
files in this directory to see my directory structure.
I saved an image in the static
directory and we started the service access http://127.0.0.1:9000/static/1bd163bc88d4.png
A little excited, we don’t have to configure anything, Blade
has helped me complete the mapping of static resources.
Custom resource directory
A brother said, I want to try to customize a directory, the name static
is too low `23333. There are two types of postures for custom static resource directories:
- Set by encoding:
blade.addStatics("/zhuangbi")
- Via configuration file:
mvc.statics=/zhuangbi
Let’s try:
public static void main(String[] args) {
Blade.me().addStatics("/zhuangbi").start();
}
In fact, Blade
provides a small function inside, the default is off, if you want to see the list in the static resource directory can open this skill, there are two ways:
- Set by encoding:
blade.showFileList(true)
- Via configuration file:
mvc.statics.list=true
What it looks like after it is turned on
What is webjars?
Classmates who have used SpringBoot
may have used this thing. In fact, the way we refer to static resources can be a jar
package.
The method of use is very simple, you need to add a dependency in maven
, such as:
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.7</version>
</dependency>
Start service access at this time http://127.0.0.1:9000/webjars/bootstrap/3.3.7/css/bootstrap.css
Come, the time to witness the miracle is here. If you are interested in webjars
, you can find more at https://www.webjars.org/.
We need to enter http://127.0.0.1:9000/static/t2.png
to access static resources.
Used in the template is /static/index.css
.