springboot整合freemarker

1.在pom.xml引入freemarker包

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

2.在springboot的属性配置文件(application.properties)里面加入freemarker的相关属性配置

spring.freemarker.allow-request-override=false  
spring.freemarker.cache=true  
spring.freemarker.check-template-location=true  
#spring.freemarker.charset=UTF-8  
spring.freemarker.content-type=text/html  
spring.freemarker.expose-request-attributes=false  
spring.freemarker.expose-session-attributes=false  
spring.freemarker.expose-spring-macro-helpers=false  
spring.freemarker.prefix=  
spring.freemarker.suffix=.ftl  

ps:这里有个问题,spring.freemarker.charset=UTF-8 本人本机会报错,还不知道原因.

3.编写控制类

package com.test.springboot.control;

import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class freemarkerControl {
    @RequestMapping(value="/hellofreemarker")//直接访问路径,不需要加项目名称
    public ModelAndView sayFreemarkerHello(Map<String,Object> map){
        ModelAndView mv = new ModelAndView("hello"); //实例化模板视图
        map.put("msg", "hello freemarker XXX");//给参数赋值
        return mv;//返回模板视图对象
    }
}

ps:@RequestMapping(value="/hellofreemarker")还可以写成@RequestMapping("/hellofreemarker")这样的方式

4.编写ftl页面

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <div >
        <h2>${msg}</h2>
    </div>
</body>

5.启动springboot的启动文件,启动文件如下

package com.test.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootApplication.class,args);
    }

}

6.启动成功之后,访问路径

http://localhost:8088/hellofreemarker

到这里基本配置成功.

遇到一个问题:当目录结构如下左图所示,即白色包图标样式,参照https://blog.csdn.net/line_to_sea/article/details/44859223改方法正常显示如下右图

但是不能访问,总是404错误如下图所示,还没有找到原因,后续更新!!

转载于:https://www.cnblogs.com/jzhxhs/p/9050201.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Freemarker是一种模板引擎,可以将数据和模板进行整合生成输出内容。SpringBoot提供了对Freemarker的支持,可以很方便地整合Freemarker。 1. 添加依赖 在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> ``` 2. 配置文件 在application.properties文件中添加以下配置: ```properties spring.freemarker.template-loader-path=classpath:/templates/ spring.freemarker.cache=false ``` - template-loader-path:模板文件的路径,这里设置为classpath:/templates/,表示在项目的classpath下的templates目录中查找模板文件。 - cache:是否开启模板缓存,这里设置为false,表示关闭缓存。 3. 创建模板文件 在classpath:/templates/目录下创建一个名为index.ftl的模板文件,内容如下: ```html <!DOCTYPE html> <html> <head> <title>SpringBoot整合Freemarker</title> </head> <body> <h1>${message}</h1> </body> </html> ``` 4. 创建控制器 创建一个名为IndexController的控制器,代码如下: ```java @Controller public class IndexController { @RequestMapping("/") public String index(Model model) { model.addAttribute("message", "Hello, World!"); return "index"; } } ``` 该控制器中,使用@RequestMapping注解指定了请求路径为/,并将一个名为message的属性值设置为“Hello, World!”,然后返回了index作为视图名称。由于配置了spring.freemarker.template-loader-path=classpath:/templates/,所以SpringBoot会在classpath:/templates/目录下查找名为index的模板文件,并将模板文件中的${message}替换为“Hello, World!”。 5. 运行程序 启动应用程序,访问http://localhost:8080/,可以看到页面中显示了“Hello, World!”的字样。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值