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:/templatesspring.freemarker.cache=falsespring.freemarker.charset=UTF-8spring.freemarker.check-template-location=truespring.freemarker.content-type=text/htmlspring.freemarker.expose-request-attributes=truespring.freemarker.expose-session-attributes=truespring.freemarker.request-context-attribute=requestspring.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; }}