• Spring Boot☞ 使用freemarker模板引擎渲染web视图


    效果图

     

    代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    package com.wls.integrateplugs.hello.controller;
     
    /**
     * Created by wls on 2017/8/24.
     */
    import java.util.Locale;
    import java.util.UUID;
     
    import javax.servlet.http.HttpSession;
     
    import com.sun.org.apache.regexp.internal.RE;
    import org.springframework.ui.Model;
    import org.springframework.ui.ModelMap;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.servlet.ModelAndView;
    import springfox.documentation.annotations.ApiIgnore;
     
    @RestController
    public class HelloController {
     
        @RequestMapping(value = "/hello",method = RequestMethod.GET)
        public String hello(Locale locale, Model model) {
            return "hello world";
        }
     
        @RequestMapping("/helloWorld")
        public String index() {
            return "Hello World";
        }
     
     
        /**
         * 使用@RestController时,则使用ModelAndView显示页面
         * @param map
         * @return
         */
        @ApiIgnore
        @RequestMapping(value = "/helloThymeleaf",method = RequestMethod.GET)
        public ModelAndView indexThymeleaf(ModelMap map) {
            ModelAndView mv = new ModelAndView("indexThymeleaf");
            map.addAttribute("name","王老师");
            map.addAttribute("host", "http://blog.didispace.com");
            return mv;
        }
     
        @RequestMapping(value = "/helloFreeMarker",method = RequestMethod.GET)
        public ModelAndView indexFreeMarker(ModelMap map) {
            ModelAndView mv = new ModelAndView("indexFreeMarker");
            map.addAttribute("name","王老师");
            map.addAttribute("host", "http://blog.didispace.com");
            return mv;
        }
     
        /**
         * 共享session
         * @param session
         * @return
         */
        @RequestMapping(value = "/uid",method = RequestMethod.GET)
        String uid(HttpSession session) {
            UUID uid = (UUID) session.getAttribute("uid");
            if (uid == null) {
                uid = UUID.randomUUID();
            }
            session.setAttribute("uid", uid);
            return session.getId();
        }
     
    }

      indexFreeMarker.ftl

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    <!DOCTYPE html>
    <html>
    <head lang="en">
        <meta charset="UTF-8" />
        <title></title>
    </head>
    <body>
    FreeMarker模板引擎
    <h1>${host}</h1>
    </body>
    </html>

      

    1
    2
    3
    4
    5
    <!--    freemarker      -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-freemarker</artifactId>
            </dependency>

      

  • 相关阅读:
    AAPay v1.5使用介绍
    我的第一款实用工具眼保程序(EyesBaby)
    C/C++中const关键字详解
    go 中flag模块
    go语言的一些基础知识
    12.Redis6中的新的数据类型
    性能测试实战30讲笔记——2.性能分析思路
    11.redis6的发布与订阅(编号大小无关内容)
    k8s——1.k8s介绍
    Docker——容器数据卷
  • 原文地址:https://www.cnblogs.com/jpfss/p/8310060.html
Copyright © 2020-2023  润新知