关于freemarker:Spring-Boot-引入freemarker

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;
    }
}

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理