Spring boot introduction template, springboot

Source: Internet
Author: User
Tags introduction template

Spring boot introduction template, springboot

Today, I will talk about how the content of the configuration in the resources file is referenced by spring boot.

  • Reference static template value
  • Integration and configuration of thymeleaf and spring boot

According to the recommended springboot format, we know that resources are stored in resources, including configuration of the entire project, custom configuration, and static resource files (js, css, etc.), ending with properties. Literally, This is the translation of attributes for the moment.

Application. properties is the configuration of project resources, such as the configuration of jar packages. For example, if the default port is 8080 but we want to change it to another port, what should we do. You only need to add

Server. port = port number

Of course, there are many other methods, but is this simple.

The following example shows how to map a value defined in a static resource file to an object;

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;
}
}

Simple. Three attributes are defined: name, website, and language;

2. Create a resource file under resources: resource. properties is defined as follows:

3. Add a new class member variable in HelloWorldController.

@Autowiredprivate Resource resource;

Write a method to view the returned results:

    @RequestMapping("/getResource")    public Object getResource(){    Resource bean=new Resource();    BeanUtils.copyProperties(resource, bean);        return bean;    }

Expected result:

This indicates that the content of the resource file has been successfully mapped to the class we have defined.

This is why, mainly because

@Configuration@ConfigurationProperties(prefix="com.xiaofeng.feng1")@PropertySource(value="classpath:response.properties")

Define Mappings

Resource bean=new Resource();BeanUtils.copyProperties(resource, bean);

Bind a specific value.

Here we will introduce the integration of freemarker and thymeleaf templates in spring boot.

Thymeleaf

Reference of the thymeleaf package needs to be introduced in pom. xml

<Dependency> <groupId> org. springframework. boot </groupId> <artifactId> spring-boot-starter-thymeleaf </artifactId> </dependency>Thymeleaf

 

The configuration in application. properties is as follows:

Spring. thymeleaf. prefix = classpath:/templates // * template location here is under resources/templates */spring. thymeleaf. cache = falsespring. thymeleaf. encoding = UTF-8spring.thymeleaf.mode = HTML5spring. thymeleaf. content-type = text/htmlspring.thymeleaf.suffix=.html

Create a ThymeleafController to implement the following:

@RequestMapping("/index")public String index(ModelMap map){map.addAttribute("name", "thymeleaf xiaofeng");return "thymeleaf/index";}

Create an index.html file under templates/thymeleaf.

<! DOCTYPE html> 

Note that the tag pair must be written, or an error may occur.

The running result is as follows:

 

Personally, thymeleaf is recommended. freemarker is similar to thymeleaf, but the coupling is higher than thymeleaf.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.