当前位置 博文首页 > 快乐的小三菊的博客:springboot集成freemarker

    快乐的小三菊的博客:springboot集成freemarker

    作者:[db:作者] 时间:2021-07-29 09:37

    背景:

    ? ? ? ?springboot支持JSP.jsp)、thymeleaf.html)和freemarker.ftl)这三种前端界面展示形式。这篇文章简单讲述下springboot如何集成freemarker

    第一步:添加maven依赖

        <!-- 继承springboot,不加这个parent标签就会报错 -->
    	<parent>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-parent</artifactId>
    		<version>1.5.8.RELEASE</version>
    	</parent>
    
    	<dependencies>
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter</artifactId>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-test</artifactId>
    			<scope>test</scope>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-web</artifactId>
    		</dependency>
    		<!-- 添加freemarker的maven依赖 -->
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-freemarker</artifactId>
    		</dependency>
    	</dependencies>
    	<build>
    		<plugins>
    			<plugin>
    				<groupId>org.apache.maven.plugins</groupId>
    				<artifactId>maven-compiler-plugin</artifactId>
    				<configuration>
    					<source>1.8</source>
    					<target>1.8</target>
    				</configuration>
    			</plugin>
    		</plugins>
    	</build>

    第二步:配置application.properties

    # 是否允许HttpServletRequest属性覆盖(隐藏)控制器生成的同名模型属性。
    spring.freemarker.allow-request-override=false
    
    # 是否允许HttpSession属性覆盖(隐藏)控制器生成的同名模型属性。
    spring.freemarker.allow-session-override=false
    
    # 是否启用模板缓存。
    spring.freemarker.cache=false
    
    # 模板编码。
    spring.freemarker.charset=UTF-8
    
    # 是否检查模板位置是否存在
    spring.freemarker.check-template-location=true
    
    # Content-Type value.
    spring.freemarker.content-type=text/html
    
    # 是否启用freemarker
    spring.freemarker.enabled=true
    
    # 设定所有request的属性在merge到模板的时候,是否要都添加到model中.
    spring.freemarker.expose-request-attributes=false
    
    # 是否在merge模板的时候,将HttpSession属性都添加到model中
    spring.freemarker.expose-session-attributes=false
    
    # 设定是否以springMacroRequestContext的形式暴露RequestContext给Spring’s macro library使用
    spring.freemarker.expose-spring-macro-helpers=true
    
    # 是否优先从文件系统加载template,以支持热加载,默认为true
    spring.freemarker.prefer-file-system-access=true
    
    # 设定模板的前缀.
    spring.freemarker.prefix=
    
    # 是否在FreeMaker中使用 request.contextPath
    spring.freemarker.request-context-attribute=true
    
    # 设定FreeMarker keys.
    spring.freemarker.settings.*=
    
    # 设定模板的后缀.
    spring.freemarker.suffix=.ftl
    
    # 设定模板的加载路径,多个以逗号分隔,默认:
    spring.freemarker.template-loader-path=classpath:/templates/
    
    # 设定默认的视图解析地址
    spring.freemarker.view-names=

    ? ? ? ?工程的目录结构如下:

    ?第三步:编写相关代码和文件

    ? ? ? ?TestController代码如下:

    @Controller
    @RequestMapping("/html")
    public class TestController {
    
    	@RequestMapping("/hello")
    	public String hello(Model model) {
    		model.addAttribute("name", "张三");
    		model.addAttribute("age", "30");
    		return "hello";
    	}
    }

    ? ? ? ?App代码如下:

    @SpringBootApplication
    public class App 
    {
        public static void main( String[] args )
        {
        	//将springboot应用驱动起来
            SpringApplication.run(App.class, args);
        }
    }

    ? ? ? ?hello.ftl代码如下:

    <!DOCTYPE html>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
        name:${name}
        age:${age}
    </body>
    </html>

    ? ? ? ?在界面请求http://localhost:8080/html/hello即可:

    ?

    cs