freemarker include js文件

  1. 文件目录结构
    在这里插入图片描述
  2. maven 配置
    应用 使用spring boot构建
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

    </dependencies>

  1. 配置 文件
#设定ftl文件路径
spring.freemarker.template-loader-path=classpath:/templates
#设定静态文件路径,js,css等
spring.mvc.static-path-pattern=/static/**
server.servlet.context-path=/freemarker
  1. controller
@Controller
public class IndexController {

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String init(ModelMap model) {
        model.addAttribute("message", "hello freemarker");
        return "index";
    }

}
  1. freemarker文件
    代码注释中说明了怎么include js文件,以及遇到的问题
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html">
<head>
    <meta charset="utf-8"/>
    <title>FreeMarker</title>
</head>
<#-- html  引入-->
<script src="static/test.js"></script>
<#-- include 引入  也不知道什么原因要放到sub目录下才可以引入,和 index.ftl同级目录就会报错-->
<script>
    <#include "sub/hello.js">
</script>
<body>
<h1>${message}</h1>
<h1>
    <#include "sub/include.ftl">
</h1>
<h1>
    <#include "sub/include.html">
</h1>
</body>
</html>
  1. 效果图
    在这里插入图片描述
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值