0
点赞
收藏
分享

微信扫一扫

java 模板

1. 添加依赖:

<dependencies>
  <!-- 支持模板 -->
  <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-freemarker</artifactId>
  </dependency>
</dependencies>

注:
不需要写版本信息,因为在parent jar包中已经引入统一管理版本信息.

2. 配置application.yml:

spring:
  # 模板配置选项
  freemarker:
    request-context-attribute: req
    suffix: .ftl
    content-type: text/html
    cache: false
    template-loader-path: classpath:/templates
    charset: UTF-8
    check-template-location: true
    expose-request-attributes: false
    expose-session-attributes: false

3. controller使用:

package com.shop.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.Map;
@Controller
@RequestMapping("/pay")
public class PayController {
    @GetMapping("/create")
    public ModelAndView create(Map<String, Object> map) {
        map.put("data", "1111");
        return new ModelAndView("pay/create", map);
    }
}

注:
①. 使用freemarker注解要用@Controller返回视图页面.
   a. @RestController返回的是json字符串.

4. 新建模板文件:

<h1>${data}</h1>

注:
①. 新建src/templates/pay/create.ftl文件.
②. 默认后缀是ftl.

5. 模板调试:

①. 只是修改模板文件,只需要"Build" -> "Build Project".

举报

相关推荐

0 条评论