Spring Boot Introductory Web chapter (II)

Source: Internet
Author: User
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>

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.