SpringBoot之freeMarker基本使用

一、案例

  1.1  pom.xml

复制代码
<dependencies>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-starter-loggingartifactId>
                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 - FreeMarkertitle>
<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  目录结构

SpringBoot之freeMarker基本使用_第1张图片

 

 

  •  结果

 SpringBoot之freeMarker基本使用_第2张图片

 

你可能感兴趣的:(SpringBoot之freeMarker基本使用)