freemarker 如何访问Static下bootstrap的静态目录css,js文件

一、配置application.yml文件下静态目录访问,index.html放在static目录下,自动获得地址http://localhost:8080访问。

spring:
  application:
    name: 项目名称
  resources:
    static-locations: classpath:/static/

二、配置freemarker引用

freemarker:
    charset: utf-8
    template-loader-path=classpath: /templates/
    request-context-attribute: request
    suffix: .ftl
    cache: false
    expose-request-attributes: true
    expose-session-attributes: true

其中配置 request-context-attribute: request(可自定义名)之后,在freemarker模板文件*.ftl里可以引用为 ${request.contextPath} 来获取相对路径地址。

三、将bootstrap目录拷贝到static目录下,模板页面引用bootstrap。

<link href="${request.contextPath}/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<script src="${request.contextPath}/bootstrap/js/bootstrap.min.js"></script>

四、以上步骤完成后,如果访问后无法出现bootstrap效果的话,按以下步骤重新编译。

 然后查看target目录里有没有bootstrap生成

按以上步骤成功引入bootstrap效果

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 Spring Boot 中的 Freemarker 模板引擎来生成静态 HTML 页面,然后让前端通过 HTTP 请求访问这些页面。 下面是一个简单的示例: 1. 在 Spring Boot 项目中添加 Freemarker 依赖,比如: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> ``` 2. 在 application.properties 文件中配置 Freemarker 相关属性,比如: ``` # 模板文件所在目录 spring.freemarker.template-loader-path=classpath:/templates/ # 静态文件生成目录 spring.freemarker.static-file-location=file:/path/to/static/files/ ``` 3. 创建一个 Freemarker 模板文件,比如 `/templates/index.ftl`: ``` <!DOCTYPE html> <html> <head> <title>My Page</title> </head> <body> <h1>Hello, ${name}!</h1> </body> </html> ``` 4. 创建一个 Spring Controller 类,生成静态 HTML 页面: ``` @Controller public class MyController { @Autowired private Configuration freemarkerConfig; @GetMapping("/generate") public void generateHtml(@RequestParam("name") String name, HttpServletResponse response) throws Exception { Template template = freemarkerConfig.getTemplate("index.ftl"); Map<String, Object> model = new HashMap<>(); model.put("name", name); StringWriter stringWriter = new StringWriter(); template.process(model, stringWriter); String html = stringWriter.toString(); response.setContentType("text/html;charset=UTF-8"); response.getWriter().write(html); response.getWriter().flush(); response.getWriter().close(); } } ``` 5. 启动 Spring Boot 应用,访问 `http://localhost:8080/generate?name=World`,即可生成静态 HTML 页面,并在浏览器中显示。 注意:在实际生产环境中,你需要将生成的静态 HTML 页面放到一个静态文件目录中(比如 `/path/to/static/files/`),然后让前端通过 HTTP 请求访问这些页面。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值