SpringBoot入门 - 使用Freemaker模板跳转页面

版权声明:《==study hard and make progress every day==》 https://blog.csdn.net/qq_38225558/article/details/85761281

1.项目导入FreeeMarker模板引擎所需依赖  (springboot所需依赖以及其他的web支持等依赖就根据自己的项目来导入即可)

<!-- FreeeMarker模板引擎所需依赖 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

2.配置 application.properties

# FreeeMarker 模板引擎配置
spring.freemarker.allow-request-override=false
spring.freemarker.cache=false
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.request-context-attribute=
#spring.freemarker.settings.*=
#spring.freemarker.suffix=.ftl
#spring.freemarker.template-loader-path=classpath:/templates/ #comma-separated list
#spring.freemarker.view-names= # whitelist of view names that can be resolved

3.模板页面 

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
             xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
    <title>Hello World!</title>
</head>
<body>
    Hello : ${msg}
</body>
</html>

4.编写controller类,跳转页面

@Controller
public class WebController {
    @RequestMapping("/index")
    public String index(Model model){
        model.addAttribute("msg", "后台传的数据...");
        return "index";
    }
}  

 或

@RestController //@RestController=@Controller+@ResponseBody 官方推荐使用
public class WebController2 {

    @RequestMapping("/index2")
    public ModelAndView index(Model model){
        model.addAttribute("msg", "后台传的数据...");
        return new ModelAndView("index");
    }

}              

5.编写启动类

@SpringBootApplication
public class WebApplication {
    public static void main(String[] args) {
        SpringApplication.run(WebApplication.class);
    }
}

 6.运行测试

<--最后附上项目结构-->

猜你喜欢

转载自blog.csdn.net/qq_38225558/article/details/85761281