`

18.[视频]使用模板(thymeleaf-freemarker)【从零开始学Spring Boot】

阅读更多

 

【视频&交流平台】

à SpringBoot视频

http://study.163.com/course/introduction.htm?courseId=1004329008&utm_campaign=commission&utm_source=400000000155061&utm_medium=share

à SpringCloud视频

http://study.163.com/course/introduction.htm?courseId=1004638001&utm_campaign=commission&utm_source=400000000155061&utm_medium=share

à Spring Boot源码

https://gitee.com/happyangellxq520/spring-boot

à Spring Boot交流平台

http://412887952-qq-com.iteye.com/blog/2321532

 

 

 

整体步骤:

(1)            pom.xml中引入thymeleaf;

(2)            如何关闭thymeleaf缓存

(3)            编写模板文件.html

     

Spring Boot默认就是使用thymeleaf模板引擎的,所以只需要在pom.xml加入依赖即可:

<dependency>

         <groupId>org.springframework.boot</groupId>

         <artifactId>spring-boot-starter-thymeleaf</artifactId>

</dependency>

 

Thymeleaf缓存在开发过程中,肯定是不行的,那么就要在开发的时候把缓存关闭,只需要在application.properties进行配置即可:

########################################################

###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

 

编写模板文件src/main/resouces/templates/helloHtml.html

<!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>

编写访问路径(com.kfit.test.web.TemplateController)

package com.kfit.test.web;

 

import java.util.Map;

 

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

 

/**

 * 模板测试.

 * @author Administrator

 *

 */

@Controller

publicclass TemplateController {

   

    /**

     * 返回html模板.

     */

    @RequestMapping("/helloHtml")

    public String helloHtml(Map<String,Object> map){

       map.put("hello","from TemplateController.helloHtml");

       return"/helloHtml";

    }

   

}

 

启动应用,输入地址:http://127.0.0.1:8080/helloHtml 会输出:

Hello.v.2

from TemplateController.helloHtml

 

 

18.2 使用freemarker

使用freemarker也很简单,

pom.xml加入freemarker的依赖:

<dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-freemarker</artifactId>

</dependency>

剩下的编码部分都是一样的,说下application.properties文件:

########################################################

###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-separatedlist

#spring.freemarker.view-names= #whitelistofviewnamesthatcanberesolved

 

 com.kfit.test.web.TemplateController

/**

     * 返回html模板.

     */

    @RequestMapping("/helloFtl")

    public String helloFtl(Map<String,Object> map){

       map.put("hello","from TemplateController.helloFtl");

       return"/helloFtl";

    }

 

src/main/resouces/templates/helloFtl.ftl

<!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>Hello.v.2</h1>

        <p>${hello}</p>

    </body>

</html>

 

访问地址:http://127.0.0.1:8080/helloFtl

Hello.v.2

from TemplateController.helloFtl

 

thymeleaffreemarker是可以共存的。

 

 

 Spring Boot 系列博客】

视频&交流平台

à Spring Boot网易云课堂视频

http://study.163.com/course/introduction.htm?courseId=1004329008

à Spring Boot交流平台

http://412887952-qq-com.iteye.com/blog/2321532

 

网易云课堂视频最新更新

第十一章 Spring Boot 日志

1、spring boot日志—理论

2、Spring Boot日志-logback

3、Spring Boot日志-log4j2

第十二章 Spring Boot 知识点2

1、spring boot 服务配置和部署

2、Spring Boot 定制URL匹配规则

 

 

历史章节

 

第一章 快速开始

1、Spring Boot之Hello World

2、Spring Boot之Hello World访问404

 

第二章 Spring Boot之JSON

1、spring boot返回json数据

2、Spring Boot完美使用FastJson解析JSON数据

 

第三章 Spring Boot热部署

1、Spring Boot热部署(springloader)

2、springboot + devtools(热部署)

 

第四章 Spring Boot数据库

1、Spring Boot JPA/Hibernate/Spring Data概念

2、Spring Boot JPA-Hibernate

3、Spring Boot Spring Data JPA介绍

4、Spring Boot JdbcTemplate

5、Spring Boot集成MyBatis

 

第五章 web开发

1、全局异常捕捉

2、配置server信息

3、spring boot使用thymeleaf

4、Spring Boot 使用freemarker

5、Spring Boot添加JSP支持

 

第六章 定时任务

1、Spring Boot定时任务

2、Spring Boot 定时任务升级篇(动态修改cron参数)

3、Spring Boot 定时任务升级篇(动态添加修改删除定时任务)

4、Spring Boot 定时任务升级篇(集群/分布式下的定时任务说明)

5、Spring Boot Quartz介绍

6、Spring Boot Quartz在Java Project中使用

7、Spring Boot 集成Quartz普通使用

8、Spring Boot 集成Quartz升级版

9、Spring Boot 集成Quartz二次升级版

10、Spring Boot 集成Quartz-Job如何自动注入Spring容器托管的对象

 

第七章 Spring Boot MyBatis升级篇

1、Spring Boot MyBatis升级篇-注解

2、Spring Boot MyBatis升级篇-注解-自增ID

3、Spring Boot MyBatis升级篇-注解-增删改查

4、Spring Boot MyBatis升级篇-注解-分页查询

5、Spring Boot MyBatis升级篇-注解-分页PageHelper不生效

6、Spring Boot MyBatis升级篇-注解- mybatic insert异常:BindingException: Parameter 'name' not found

7、Spring Boot MyBatis升级篇-注解- #和$符号特别篇

8、Spring Boot MyBatis升级篇-注解-@Result

9、Spring Boot MyBatis升级篇-注解-动态SQL(if test)-方案一:<script>

10、Spring Boot MyBatis升级篇-注解-动态SQL(if test)-方案二:@Provider

11、Spring Boot MyBatis升级篇-注解-动态SQL-参数问题

12、Spring Boot MyBatis升级篇-注解-特别篇:@MapperScan和@Mapper

13、Spring Boot MyBatis升级篇-XML

14、Spring Boot MyBatis升级篇-XML-自增ID

15、Spring Boot MyBatis升级篇-XML-增删改查

16、Spring Boot MyBatis升级篇-XML-分页查询

17、Spring Boot MyBatis升级篇-XML-分页PageHelper不生效

18、Spring Boot MyBatis升级篇-XML-动态SQL(if test)

19、Spring Boot MyBatis升级篇-XML-注解-初尝试

20、Spring Boot MyBatis升级篇- pagehelper替换为pagehelper-spring-boot-starter

 

第八章 Spring Boot 知识点1

1、Spring Boot 拦截器HandlerInterceptor

2、Spring Boot启动加载数据CommandLineRunner

3、Spring Boot环境变量读取和属性对象的绑定

4、Spring Boot使用自定义的properties

5、Spring Boot使用自定义的properties

6、Spring Boot使用@SpringBootApplication

7、Spring Boot 监控和管理生产环境

 

第十章 Spring Boot 打包部署

1、Spring Boot打包部署((提供Linux的sh文件))

 

第十一章 Spring Boot 日志

1、spring boot日志—理论

2、Spring Boot日志-logback

3、Spring Boot日志-log4j2

 

 

分享到:
评论
5 楼 1988南墙之南 2017-10-25  
这里如果 请求的路径和视图的名字取一样的,springboot会报错,他会循环调用
4 楼 ktlqqww 2016-12-20  
请问大神,把应用spring-boot-hello1.zip下载下来打包成jar包出来之后发现访问不了页面,报错

There was an unexpected error (type=Internal Server Error, status=500).
Error resolving template "/helloHtml", template might not exist or might not be accessible by any of the configured Template Resolvers

请问是什么问题?
我在eclipse里面运行正常可访问
3 楼 wheretofindyou2 2016-11-03  
我试了一下thymeleaf和freemarker在一起不好使。我用的版本是1.3.8.RELEASE
2 楼 林祥纤 2016-08-20  
yanglinone1 写道
这里要用  @Controller

害我用@RestController

调半天,发现都返回  /helloHtml 字符串



这是肯定的,基本常识。
1 楼 yanglinone1 2016-08-19  
这里要用  @Controller

害我用@RestController

调半天,发现都返回  /helloHtml 字符串

相关推荐

Global site tag (gtag.js) - Google Analytics