SpringBoot---FreeMarker模板引擎

一.    spring boot的web应用开发,是基于spring mvc

二.    Spring boot 在spring默认基础上,自动配置添加了以下特性:
    1.    包含了ContentNegotiatingViewResolver和BeanNameViewResolver beans。
    2.    对静态资源的支持,包括对WebJars的支持。
    3.    自动注册Converter,GenericConverter,Formatter beans。
    4.    对HttpMessageConverters的支持。
    5.    自动注册MessageCodeResolver。
    6.    对静态index.html的支持。
    7.    对自定义Favicon的支持。
    8. 主动使用ConfigurableWebBindingInitializer bean


三、使用freemarker 模板引擎,直接在templates下创建xxx.ftl文件即可使用css、js、image静态资源文件

    1.引入freemarker引擎依赖在pom文件中:

<!--freemaker 模板引擎-->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

     2.在SpringBoot项目下,创建controllerwen文件夹,创建webController.class文件

package com.nyist.demo.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/web")
public class webController {

    private static final Logger logger = LoggerFactory.getLogger("webController.class");

    @RequestMapping(value="/index")
    public String index(ModelMap map) {
        logger.info("这里是controller");
        map.put("title", "hello world");
        //不能再index 前面加上 /  再Linux  平台下  不支持 /
        System.out.println("这是测试============================================");
        return "index";
    }
}

   3.在templates下创建css js images 文件夹,在css文件夹下下写入index.csswe文件,在images文件夹下导入logo.png

    index.css如下:

h1{
    color: blue;
}

   4.我们使用webjar 验证freemarker模板是否支持,需要在pom.xml中导入webjar依赖

<!--org.webjars 引入jquery库-->
<dependency>
   <groupId>org.webjars</groupId>
   <artifactId>jquery</artifactId>
   <version>2.1.4</version>
</dependency>

   5.在templates文件夹下bi'a编写index.ftl文件,index.ftl如下:

<!DOCTYPE html>
<html>
<head lang="en">
    <title>Spring Boot Demo - FreeMarker</title>
    <link href="/css/index.css"/>
    <#--<script type="text/javascript" src="/js/jquery-1.8.3.min.js"></script>-->
    <script type="text/javascript" src="/webjars/jquery/2.1.4/jquery.min.js"></script>
</head>
<body>
   <center>
       <h1>首页</h1>
       <img src="/images/logo.png" />
       <h1 id="title">${title}</h1>
   </center>

   <script>
       $(function(){
           $('#title').click(function(){
               alert('点击了');
           });
       })
   </script>
</body>
</html>

   6.在web界面访问http://localhost:8888/web/index,即可看到templates/index.ftl文件页面(freemarker模板自行资源请求查找到该文件,使用者无需担心)

   7.pom.xml文件如下:

    

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.nyist</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>spring-demo-6-1</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <!--热部署依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--freemaker 模板引擎-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>

        <!--org.webjars 引入jquery库-->
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery</artifactId>
            <version>2.1.4</version>
        </dependency>

        <!--web依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

application.properties Freemaker配置

########################################################
###FREEMARKER (FreeMarkerAutoConfiguration)
########################################################
spring.freemarker.allow-request-override=false
spring.freemarker.cache=false
spring.freemarker.check-template-location=true
spring.freemarker.charset=UTF-8
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-session-attributes=false
spring.freemarker.expose-spring-macro-helpers=false
#spring.freemarker.prefix=
#spring.freemarker.request-context-attribute=
#spring.freemarker.settings.*=
#spring.freemarker.suffix=.ftl
#spring.freemarker.template-loader-path=classpath:/templates/ #comma-separated list
#spring.freemarker.view-names= # whitelist of view names that can be resolved

    8.访问页面如下:

   看到该页面即可证明SpringBoot 引入freemarker模板引擎成功!

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值