zoukankan      html  css  js  c++  java
  • springboot中模板 freemark,thymeleaf,jsp

    freemark模板和thymeleaf模板

    1.在pom.xml中引入thymeleaf和freemark;

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

      

    2.在application.xml中配置

    ########################################################
    ###THYMELEAF (ThymeleafAutoConfiguration)
    ########################################################
    #spring.thymeleaf.prefix=classpath:/templates/
    #spring.thymeleaf.suffix=.html
    #spring.thymeleaf.mode=HTML5
    #spring.thymeleaf.encoding=UTF-8
    # ;charset=<encoding> is added
    #spring.thymeleaf.content-type=text/html 
    # set to false for hot refresh
    spring.thymeleaf.cache=false 
    
    ########################################################
    ###FREEMARKER (FreeMarkerAutoConfiguration)
    ########################################################
    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.request-context-attribute=
    #spring.freemarker.settings.*=
    #spring.freemarker.suffix=.ftl
    #spring.freemarker.template-loader-path=classpath:/templates/ #comma-separated list
    #spring.freemarker.view-names= # whitelist of view names that can be resolved
    

      

    3.编写模板

    //注意路径是在配置中默认的,也是可以修改的
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
          xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
        <head>
            <title>Hello World!</title>
        </head>
        <body>
            <h1 th:inline="text">Hello.v.2</h1>
            <p th:text="${hello}"></p>
        </body>
    </html>
    --------freemark模板入下
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
          xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
        <head>
            <title>Hello World!</title>
        </head>
        <body>
            <h1>Freemark</h1>
            <p>${hello}</p>
        </body>
    </html>
    

      

    4.controller层

    package com.ithuan.demo.controller;
    
    import java.util.Map;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    //@RequestMapping("/templates")
    public class TemplateController {
    	@RequestMapping("/hello1111")
    	public String hello1(Map<String,Object> map){
    		System.err.println("----");
    		map.put("hello","from TemplateController.helloHtml");
    
    		return "hello1";
    	}
    	@RequestMapping("/hello22")
    	public String hello22(Map map){
    		System.err.println("2222");
    		map.put("hello", "最終的大boss");
    		return "freemakr";
    	}
    }
    

      

    jsp页面展自带的解析不再展示

  • 相关阅读:
    Mybatis的XML中数字不为空的判断
    初步使用VUE
    Vue中实现菜单下拉、收起的动画效果
    Docker For Windows时间不对的问题
    freemarker使用自定义的模板加载器通过redis加载模板
    .net core 打印请求和响应的内容
    codedecision P1113 同颜色询问 题解 线段树动态开点
    洛谷P2486 [SDOI2011]染色 题解 树链剖分+线段树
    洛谷P3150 pb的游戏(1)题解 博弈论入门
    codedecision P1112 区间连续段 题解 线段树
  • 原文地址:https://www.cnblogs.com/liushisaonian/p/9350222.html
Copyright © 2011-2022 走看看