For the routing problem is I've been feeling the pain of things, first of all because of my unrealistic expectations, feel the path that simple did not need to see, but yesterday the project leader cynicism makes me ashamed: Path "You are not even linux and window I do not know the difference, huh, huh. " It began when I configure the log file, I do not know what the root directory of the window path, so I opened the cmd, want to enter cd /, but found the path cd / root directory after a letter, woc this is not the root directory, then it was merciless mockery. Closer to home, for the resource directory springboot I have been only a Resources directory inexplicable, its directory structure is what is it? I put all I encountered are recorded as memory.
First, the classification of the resource directory SpringBoot
-1. Static storage static resources, such as static pictures, images, js, etc.
For example, here we create a static.html, we can have direct access to static directory of resources
You can also use Controller Jump
@Controller public class RedirectController { @RequestMapping("/stc") public String stc(){ return "static.html"; } }
-2. Templates stored dynamic images, etc.
The following dynamic page templates stored in the url can not directly access the input .html, you need to request the server to the dynamic picture.
We need a way to achieve the purpose, just like jsp, using a form of data encryption template rendering, but jsp is not conducive to the development of separate front and rear end (though I feel very comfortable using java to write front-end, but not recommended jsp Springboot )
The official recommendation is to use dynamic interface Thymeleaf to do, I'm using here is freemaker, springboot also integrates freemaker of.
This is my templates below
First introduced dependence
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
Then a simple configuration application.yml, the role of this configuration will speak later
spring: freemarker: suffix: .html
After configuration is complete, we are the first to test the success of direct access
Here is the 404 reported, apparently did not find
By way controller that under our experimental
@RequestMapping("tmplts") public String tmplts(){ return "template"; }
Note that the return can not bring .html, in front of him we configure freemaker be added the suffix .html
Now visit a success. So far, most have mastered the basic use. Then explain some of the others.
Second, the custom static resource access paths and springboot default configuration
First, we explain under several static directory under springboot resource directory,
/META-INF/resources
resources
static
public
What role do these types? These directories are springboot default static resource access path provided, their root path is "/"
What does that mean? That is when accessing the directory files, directory access their directories and access to resources following files are the same, like on our visit earlier catalog do not like plus static
Promising for so long, we assign to different directories different functions, make the code look more clarity
That being the case, if these files the same name as the directory will be how to implement it? There is a limited order between them, this is the order of the default / META-INF / resources> resources> static> public, but goes on to say how the change
Then there is a problem is if I think I can also create a directory access to resources in the directory by means of access to the root path, then how can I do? Then there is the question.
First, we have two ways to configure, but their essence is the same, I've created a test directory
-1 manner arranged application.yml
spring: mvc: static-path-pattern: /** resources: static-locations: classpath:/test/,classpath:/static/js/,classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/
Here we configure two parts,
First: static-path-pattern, this configuration item is to tell springboot, in what should be a way to find resources. The default configuration is / *. In other words, what kind of match condition only static Resources, Spring Boot will handle static resource requests, we can also be configured / user / **, so we must enter /user/index.html to access to these directories resource of,
Second: spring.resources.static-locations, this configuration item is springboot tell where to find resources, prioritize, based on the context, that is to say if the / META-INF / resources / directory and / resources have a index.html , then according to the default resource priority will go to visit / test / under. (Tips: When you have changed this configuration, the default project also need to add, or will be covered, details visible source)
So we have to experiment a bit: I wrote the following in each directory index.html
When I visited index.html, the resources under the highest priority is the test directory
-2 manner using the configuration class
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class MyWebMvcConfigurer extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("classpath:/test/");
super.addResourceHandlers(registry);
}
}
This approach requires WebMvcConfigurerAdapter class inheritance, and override addResourceHandlers methods provided herein with a substantially similar configuration yml
https://blog.csdn.net/qq_34797335/article/details/80194137 this article you can see a more detailed explanation of the source code