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>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>org.springframework.boot<span style="color: #0000ff">&lt;/</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>spring-boot-starter-test<span style="color: #0000ff">&lt;/</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">scope</span><span style="color: #0000ff">&gt;</span>test<span style="color: #0000ff">&lt;/</span><span style="color: #800000">scope</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #008000">&lt;!--</span><span style="color: #008000"> 使用log4j2,该包paren标签中已经依赖 </span><span style="color: #008000">--&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>org.springframework.boot<span style="color: #0000ff">&lt;/</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>spring-boot-starter-log4j2<span style="color: #0000ff">&lt;/</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>

    <span style="color: #0000ff">&lt;</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>org.springframework.boot<span style="color: #0000ff">&lt;/</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>spring-boot-devtools<span style="color: #0000ff">&lt;/</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>

    <span style="color: #008000">&lt;!--</span><span style="color: #008000"> 引入freemarker包 </span><span style="color: #008000">--&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>org.springframework.boot<span style="color: #0000ff">&lt;/</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>spring-boot-starter-freemarker<span style="color: #0000ff">&lt;/</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>

<span style="color: #0000ff">&lt;/</span><span style="color: #800000">dependencies</span><span style="color: #0000ff">&gt;</span>

<span style="color: #0000ff">&lt;</span><span style="color: #800000">build</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">plugins</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">plugin</span><span style="color: #0000ff">&gt;</span>
            <span style="color: #0000ff">&lt;</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>org.springframework.boot<span style="color: #0000ff">&lt;/</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>
            <span style="color: #0000ff">&lt;</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>spring-boot-maven-plugin<span style="color: #0000ff">&lt;/</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #0000ff">&lt;/</span><span style="color: #800000">plugin</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">plugins</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">build</span><span style="color: #0000ff">&gt;</span></pre>
复制代码

 

  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 </span>= "/login"<span style="color: #000000">)
</span><span style="color: #0000ff">public</span><span style="color: #000000"> ModelAndView index(ModelAndView modelAndView) {
    modelAndView.setViewName(</span>"index"<span style="color: #000000">);
    
    List</span>&lt;String&gt; userList=<span style="color: #0000ff">new</span> ArrayList&lt;String&gt;<span style="color: #000000">();
    userList.add(</span>"admin"<span style="color: #000000">);
    userList.add(</span>"user1"<span style="color: #000000">);
    userList.add(</span>"user2"<span style="color: #000000">);
    
    modelAndView.addObject(</span>"userList"<span style="color: #000000">, userList);
    </span><span style="color: #0000ff">return</span><span style="color: #000000"> 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>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">font</span><span style="color: #0000ff">&gt;</span> 
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">#list </span><span style="color: #ff0000">userList as item</span><span style="color: #0000ff">&gt;</span><span style="color: #000000"> 
        ${item!}</span><span style="color: #0000ff">&lt;</span><span style="color: #800000">br </span><span style="color: #0000ff">/&gt;</span>
    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">#list</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">font</span><span style="color: #0000ff">&gt;</span>

<span style="color: #0000ff">&lt;</span><span style="color: #800000">button </span><span style="color: #ff0000">class</span><span style="color: #0000ff">="a"</span><span style="color: #0000ff">&gt;</span> click me<span style="color: #0000ff">&lt;/</span><span style="color: #800000">button</span><span style="color: #0000ff">&gt;</span>

</body>
</html>

复制代码

 

  1.6  index.js

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

 

   1.7  index.css

h2{color:red}

 

  1.8  目录结构

 

 

  •  结果

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值