Today, basically, how the content configured in the resources file is referenced by spring boot.
- Reference the value of a static template
- Thymeleaf and spring boot integration, and related configurations
Based on the format recommended by Springboot, we know that resources are included in the resource, including the configuration of the entire project, custom configuration, static resource files (JS,CSS, etc.), ending in properties. The literal meaning is to translate the property for the moment.
Application.properties is the configuration of the project resources, such as the configuration of the jar package, such as the default port is 8080 but we want to change to another port, what to do. Just add it to this system configuration file.
server.port= Port number
Of course, there are a lot of other ways, but it's easy.
Here's an example to show how to map a value defined in a static resource file to an object;
- Create a new Com.xiaofeng.feng.pojo package and add the entity class Resource.java:
Package Com.xiaofeng.feng.pojo;
Import org.springframework.boot.context.properties.ConfigurationProperties;
Import org.springframework.context.annotation.Configuration;
Import Org.springframework.context.annotation.PropertySource;
@Configuration
@ConfigurationProperties (prefix= "Com.xiaofeng.feng")
@PropertySource (value= "Classpath:response.properties")
public class Resource {
private String name;
Private String website;
Private String language;
Public String GetName () {
return this.name;
}
Public String Getwebsite () {
return this.website;
}
Public String GetLanguage () {
return this.language;
}
public void SetName (String name) {
This.name=name;
}
public void Setwebsite (String website) {
This.website = website;
}
public void SetLanguage (String language) {
This.language = language;
}
}
It is relatively simple to define three properties: Name,website,language;
2. Then create a new resource file under resources: Resource.properties is defined as follows:
3. Add a new class member variable inside the Helloworldcontroller
@Autowiredprivate Resource Resource;
To write a method to look at the results returned:
@RequestMapping ("/getresource") public Object getresource () { Resource bean=new Resource (); Beanutils.copyproperties (resource, bean); return bean; }
Get results:
Description the contents of the resource file have been successfully mapped to our defined class.
This is why, mainly
@Configuration @configurationproperties (prefix= "com.xiaofeng.feng1") @PropertySource (value= "classpath: Response.properties ")
Defining Mapping relationships
Resource bean=new Resource (); Beanutils.copyproperties (resource, Bean);
The binding specific value.
Here by the way Freemarker and Thymeleaf 2 template presentation templates in spring boot integration.
Thymeleaf
A reference to the Thymeleaf package needs to be introduced in Pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId> Spring-boot-starter-thymeleaf</artifactid> </dependency>
Thymeleaf
In the Application.properties type configuration is as follows
spring.thymeleaf.prefix=classpath:/templates// * The location of the template is here under Resources/templates */spring.thymeleaf.cache= falsespring.thymeleaf.encoding=utf-8spring.thymeleaf.mode=html5spring.thymeleaf.content-type=text/ Htmlspring.thymeleaf.suffix=.html
Create a new Thymeleafcontroller implementation as follows:
@RequestMapping ("/index") public String Index (Modelmap map) {Map.addattribute ("name", "Thymeleaf Xiaofeng"); return " Thymeleaf/index ";}
Create a new index.html under the Templates/thymeleaf
<! DOCTYPE html>
Note that the label must be written well or it will go wrong.
The results of the operation are as follows:
Individuals are more recommended to use thymeleaf this template, Freemarker and it uses the same, but the coupling is higher than the thymeleaf.
Spring Boot Introduction Template