Java SpringBoot 页面渲染

    xiaoxiao2025-01-20  3

    我们的静态页面全部存放在templates目录下 src/main/resources/templates

    SpringBoot默认有以下几个模板引擎: Thymeleaf FreeMarker Velocity Groovy Mustache

    引入freeMarker依赖包

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

    控制器

    @Controller//模板渲染用Controller,restfull返回用RestController @RequestMapping("/user") public class UserController{ @RequestMapping("info") public String info(Model model){ model.addAttribute("username","wang"); Map<String Object> map = new Map<String, Object>(); map.puut("age",22); model.addAttribute("map",map); return "user/info";//会从templates目录下找user目录下的info模板 } }

    模板

    ${username} ${map.age}
    最新回复(0)