Spring Boot Dry series: (vi) static resources and interceptor processingoriginal 2017-04-05 toot md du ye Java Super Theological hall
Objective
In this chapter we introduce the Springboot support for static resources and a very important class webmvcconfigureradapter.
Body
We also have a brief introduction to the default support for static resources in Springboot, which is described in detail today for default support, and how custom extensions are implemented.
Default Resource Mappings
Spring Boot defaults to provide us with static resource handling, using the configuration of various properties in Webmvcautoconfiguration.
We recommend that you use the default configuration of spring boot to provide a static resource mapping as follows:
Inside the project The path is this:
These are the mapping paths for static resources, with priority order: meta-inf/resources > Resources > Static > Public
You can put a picture of the same name on each of the above 4 paths, and then verify it by visiting it.
Also, you can randomly place index.html on the top of a path, and when we access the app root http://lcoalhost:8080, it maps directly to the Index.html page.
The corresponding configuration file is configured as follows:
We can modify the default mappings by modifying the Spring.mvc.static-path-pattern, for example, I change to/dudu/**, which accesses http://lcoalhost:8080/dudu/index.html when running. Corresponds to the index.html page.
Take over the web configuration of Spring boot
If the sping MVC provided by spring Boot does not meet the requirements, it is possible to implement a fully self-contained MVC configuration by adding @enablewebmvc annotations to a configuration class (a class annotated with @configuration).
Of course, the automatic configuration of Spring boot is usually in line with most of our needs. You can define a configuration class and inherit webmvcconfigureradapter without using @enablewebmvc annotations when you need to keep the convenience provided by spring boot and need to add your own additional configuration.
Here we mention this webmvcconfigureradapter class, overriding the methods in this class allows us to add additional configuration, here we introduce a few common.
Custom Resource Mapping Addresourcehandlers
For example, if we want to customize the static resource mapping directory, we just need to rewrite the Addresourcehandlers method.
Add a mapping path through Addresourcehandler, and then specify the path through Addresourcelocations. The address of the elephant.jpg picture that we accessed in the custom my folder is Http://localhost:8080/my/elephant.jpg
If you want to specify the external directory is also very simple, directly addresourcelocations specified, the code is as follows:
Addresourcelocations refers to the directory where the files are placed, Addresourehandler refers to the access paths that are exposed externally
Page Jump Addviewcontrollers
Before writing SPRINGMVC, if you need to access a page, you must write the controller class, and then write a method to jump to the page, feel good trouble, In fact, rewrite Webmvcconfigureradapter in the Addviewcontrollers method can achieve the effect of
The value indicates that the Addviewcontrollers method is overridden here and does not overwrite the addviewcontrollers in Webmvcautoconfiguration (in this method, Spring boot will "/" mapping to index.html), which means that our own configuration and the automatic configuration of spring boot are also valid, which is how we recommend adding our own MVC configuration.
Interceptor Addinterceptors
Interceptors are often used in our projects, here is the simplest to determine whether or not to use the login.
The following 2 steps are required to implement the Interceptor function:
Create our own interceptor class and implement the Handlerinterceptor interface
In fact, rewrite the Addinterceptors method in Webmvcconfigureradapter to add the custom interceptor class.
First, customize the Interceptor code:
Here we simply implement the session in accordance with whether the user object to determine whether to log in, empty to jump to the login page, not empty on the pass.
Next, rewrite the Addinterceptors method in Webmvcconfigureradapter as follows:
Addpathpatterns ("/**") intercepts all requests, but excludes interception of/tologin and/login requests.
Page Login Key code:
Controller code:
When this is accessed, it jumps to the login.html page without logging in, and access to Http://localhost:8080/toLogin and Http://localhost:8080/login is not intercepted.
More configuration can view the API of the Webmvcconfigureradapter class. Because it is an implementation of the Webmvcconfigurer interface, the Webmvcconfigurer API method can also be used to configure MVC.
Just to implement this interface, to implement all the methods, this is embarrassing.
Therefore, it is recommended to use the inherited Webmvcconfigureradapter class for processing.
Summarize
Static resources and interceptors are often used in regular projects, and it is useful to know how to handle them. This is the end of the day, and the next one will explain how the logs are used in the project.
Spring Boot Dry series: (vi) static resources and interceptor processing