Spring Boot Introductory Web chapter (II)
Original address: Spring Boot Introductory Web chapter (II)
Blog address: http://www.extlight.com First, preface
The foundation of Spring Boot Primer (i) introduces the basics of Spring Boot's environment and project start-up, and this article continues to delve into spring boot's knowledge of Web development. second, integrated template engine
Since JSPs are not recommended for use by Springboot, the template engine mainly introduces Freemarker and Thymeleaf. 2.1 Integrated Freemarker 2.1.1 Adding freemarker dependencies
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId> Spring-boot-starter-freemarker</artifactid>
</dependency>
2.1.2 Add freemarker template configuration
In Application.properties, add the following:
Spring.freemarker.allow-request-override=false
spring.freemarker.cache=true
Spring.freemarker.check-template-location=true
Spring.freemarker.charset=utf-8
spring.freemarker.content-type=text/html
Spring.freemarker.expose-request-attributes=false
Spring.freemarker.expose-session-attributes=false
Spring.freemarker.expose-spring-macro-helpers=false
spring.freemarker.prefix=
SPRING.FREEMARKER.SUFFIX=.FTL
The above configurations are the default values. 2.1.3 Freemarker Case Demo
Create the Freemarkercontroller in the controller package:
@Controller
@RequestMapping ("Freemarker") public
class Freemarkercontroller {
@RequestMapping ("Hello" Public
String Hello (map<string,object> Map) {
map.put ("msg", "Hello Freemarker");
return "Hello";
}
}
Create a file named Hello.ftl in the Templates directory with the following contents:
<! DOCTYPE html>
The results are as follows:
2.2 Integrated Thymeleaf 2.2.1 Adding Thymeleaf dependencies
In the Pom.xml file, add:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId> Spring-boot-starter-thymeleaf</artifactid>
</dependency>
2.2.2 Add Thymeleaf template configuration
In Application.properties, add the following:
Spring.thymeleaf.cache=true
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=. HTML
spring.thymeleaf.mode=html5
spring.thymeleaf.encoding=utf-8
spring.thymeleaf.content-type= Text/html
The above configurations are the default values. 2.2.3 Thymeleaf Case Demo
Create the Thymeleafcontroller in the controller package:
@Controller
@RequestMapping ("thymeleaf") public
class Thymeleafcontroller {
@RequestMapping ("Hello") Public
String Hello (map<string,object> Map) {
map.put ("msg", "Hello Thymeleaf");
return "Hello";
}
}
Under the template directory, create a file named Hello.html, which reads as follows:
<! DOCTYPE html>