Spring Boot如何整合FreeMarker模板引擎

网友投稿 246 2022-11-18


Spring Boot如何整合FreeMarker模板引擎

POM

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-freemarker

项目结构

src/

+- main/

+- java/

| +- com

| +- controller/

| | +- IndexController.class

| +- Application.class

+- resoFDuOFNUDurces/

+- templates/

+- index.ftlh

Applhttp://ication为应用程序启动类

IndexController为控制器,里面含有一个index请求处理方法,它返回index字符串,表示渲染模板文件index.ftlh。

index.ftlh为freemarker模板文件

Applciation.class

@SpringBootApplication

public class Application {

public static void main(String[] args) {

SpringApplication.run(Application.class, args);

}

}

IndexCohttp://ntroller.class

@Controller

public class IndexController {

@GetMapping("/index")

public String index(Model model) {

model.addAttribute("name", "Alice");

return "index";

}

}

注意@ResponseBody注解不能和freemarker一起使用,所以此处不能标注@RestController注解。

index.ftlh

hello ${name}!

运行

运行Application类里的main方法。

然后访问localhost:8080/index,结果展示为:

hello Alice!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:SpringBoot结合ProGuard实现代码混淆(最新版)
下一篇:详解JUC 常用4大并发工具类
相关文章

 发表评论

暂时没有评论,来抢沙发吧~