springboot入门(5)springboot和freema

2018-08-14  本文已影响0人  一凡的学习笔记

1、修改pom配置,添加依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

2、修改application.properties配置,添加freemarker的配置信息

spring.freemarker.template-loader-path=classpath:/templates/
spring.freemarker.suffix=.ftl

3、在resouces/templates下面,创建一个world.ftl

<!DOCTYPE html>
<head lang="en">
    <meta charset="UTF-8" />
    <title></title>
</head>
<body>
<h1>Hello World</h1>
123123123123
123132
<br/>
<span ${name}></span>
</body>
</html>

4、在com.example.demo/controller下TestController类中添加

@Controller

public class TestController {

    private static Logger logger = LoggerFactory.getLogger(TestController.class);

    @RequestMapping("/index")

    public String home() {

        logger.info(">>> index");

        return "index";

    }

    @RequestMapping("/test")

    public String testFtl(Map<String, Object> map){

        map.put("name", "ftl");

        map.put("sj", "123456789");

        return "test";

    }

    @RequestMapping("/world")

    public String world(HttpServletRequest request) {

        request.setAttribute("name", "xxxxxxxxx");

        return "world";

    }

}
``
5、页面预览


![image.png](https://img.haomeiwen.com/i234148/307ba7a17226e967.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
上一篇 下一篇

猜你喜欢

热点阅读