关于freemarker:Spring-Boot-引入freemarker

10次阅读

共计 1230 个字符,预计需要花费 4 分钟才能阅读完成。

1 Maven 引库

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
    <version>2.5.5</version>
</dependency>

application.properties 增加配置

# 模板文件门路,当初配置在 resources 下的 templates 文件夹中
spring.freemarker.tempalte-loader-path=classpath:/templates
spring.freemarker.cache=false
spring.freemarker.charset=UTF-8
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=true
spring.freemarker.expose-session-attributes=true
spring.freemarker.request-context-attribute=request
spring.freemarker.suffix=.ftl

应用示例

@RestController
@RequestMapping("admin/user")
public class AdminUserController extends BaseController{

    @Autowired
    private UserService userService;

    @GetMapping("/userView")
    public ModelAndView userView() {ModelAndView modelAndView = new ModelAndView("user/user");
        return modelAndView;
    }

    @GetMapping("/addUserView")
    public ModelAndView addUserView() {ModelAndView modelAndView = new ModelAndView("user/userAdd");
        return modelAndView;
    }

    @GetMapping("/updateUserView")
    public ModelAndView updateUserView(@RequestParam Long id) {ModelAndView modelAndView = new ModelAndView("/admin/userUpdate");
        modelAndView.addObject("user", userService.getUserDetail(id));
        return modelAndView;
    }
}

正文完
 0