• (八)SpringBoot之freeMarker基本使用


    一、案例

      1.1  pom.xml

    <dependencies>
            <!-- 除去logback支持 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <exclusions>
                    <exclusion>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-logging</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <!-- 使用log4j2,该包paren标签中已经依赖 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-log4j2</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
            </dependency>
    
            <!-- 引入freemarker包 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-freemarker</artifactId>
            </dependency>
    
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>

      1.2  application.properties

    #主配置文件,配置了这个会优先读取里面的属性覆盖主配置文件的属性
    spring.profiles.active=dev
    server.port=8888
        
    logging.config=classpath:log4j2-dev.xml

      1.3  编写控制器

    package com.shyroke.controller;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    
    @Controller
    @RequestMapping(value = "/index")
    public class IndexController {
    
    
        @RequestMapping(value = "/login")
        public ModelAndView index(ModelAndView modelAndView) {
            modelAndView.setViewName("index");
            
            List<String> userList=new ArrayList<String>();
            userList.add("admin");
            userList.add("user1");
            userList.add("user2");
            
            modelAndView.addObject("userList", userList);
            return modelAndView;
        }
        
    }

      1.5  编写index.ftl

    <!DOCTYPE html>
    <html>
    <head lang="en">
    <title>Spring Boot Demo - FreeMarker</title>
    <link href="/css/index.css" rel="stylesheet">
    <script type="text/javascript" src="/jars/jquery-3.2.1.min.js"></script>
    <script type="text/javascript" src="/js/index.js"></script>
    </head>
    <body>
        <h2>首页<h2>
        
        <font> 
            <#list userList as item> 
                ${item!}<br />
            </#list>
        </font>
        
        <button class="a"> click me</button>
    </body>
    </html>

      1.6  index.js

    $(function() {
        $('.a').click(function() {
            alert('hello world');
        });
    })

       1.7  index.css

    h2{color:red}

      1.8  目录结构

    •  结果

     

  • 相关阅读:
    opencv-识别手写数字
    opencv-图像遍历
    533. Lonely Pixel II
    531. Lonely Pixel I
    495. Teemo Attacking
    370. Range Addition
    487. Max Consecutive Ones II
    485. Max Consecutive Ones
    414. Third Maximum Number
    90. Subsets II
  • 原文地址:https://www.cnblogs.com/shyroke/p/8022940.html
Copyright © 2020-2023  润新知