美文网首页
springboot入门(5)springboot和freema

springboot入门(5)springboot和freema

作者: 一凡的学习笔记 | 来源:发表于2018-08-14 11:24 被阅读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)
    

    相关文章

      网友评论

          本文标题:springboot入门(5)springboot和freema

          本文链接:https://www.haomeiwen.com/subject/gfimbftx.html