PHP速学视频免费教程(入门到精通)
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
thymeleaf是新一代java模板引擎,类似于velocity、freemarker等传统java模板引擎。与传统java模板引擎不同的是,thymeleaf支持html原型,既可以让前端工程师在浏览器中直接打开查看样式,也可以让后端工程师结合真实数据查看显示效果。同事,spring boot提供了thymeleaf自动化配置解决方案,因此在spring boot中使用thymeleaf 非常方便。以下是可用的步骤,用于将thymeleaf整合到spring boot中
新建一个Spring Boot工程,然后添加spring-boot-starter-web 和spring-boot-starter-thymeleaf 依赖
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid></dependency><!-- 整合Thymeleaf --><dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-thymeleaf</artifactid></dependency>
Spring Boot为Thymeleaf提供了自动化配置类ThymeleafAutoConfiguration,相关的配置属性在ThymeleafProperties 类中,ThymeleafProperties类部分源码如下:
@ConfigurationProperties(prefix = "spring.thymeleaf") public class ThymeleafProperties { private static final Charset DEFAULT_ENCODING = StandardCharsets.UTF_8; public static final String DEFAULT_PREFIX = "classpath:/templates/"; public static final String DEFAULT_SUFFIX = ".html"; }
由此配置可以看到,默认的模板位置在classpath:/templates/,默认的模板后缀名为.html。在使用IDEA创建Spring Boot项目时,默认会在templates文件夹中创建一些文件。如需对默认的Thymeleaf 配置参数进行自定义配置,可以在application.properties 中进行配置,部分常见配置如下:
#是否开启缓存,开发时可设置为false,默认为truespring.thymeleaf.cache=false#检查模版是否存在,默认为truespring.thymeleaf.check-template=true#检查模版位置是否存在,默认为truespring.thymeleaf.check-template-location=true#模版文件编码spring.thymeleaf.encoding=UTF-8#模版文件位置spring.thymeleaf.prefix=classpath:/templates/#Content-Type配置spring.thymeleaf.servlet.content-type=text/html#模版文件后缀spring.thymeleaf.suffix=.html
创建Book实体类,然后在Controller中返回ModelAndView,如下:
public class Book { private int id; private String name; private String author; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } }
@RestController public class BookController { @GetMapping(value = "/books") public ModelAndView books(){ List<book> books = new ArrayList(); Book b1 = new Book(); b1.setId(1); b1.setAuthor("唐家三少"); b1.setName("斗罗大陆Ⅰ"); Book b2 = new Book(); b2.setId(2); b2.setAuthor("唐家三少"); b2.setName("斗罗大陆Ⅱ"); books.add(b1); books.add(b2); ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("books",books); modelAndView.setViewName("books"); return modelAndView; } }</book>
在resources目录下的templates目录中创建books.html,如下:
nbsp;html> <meta> <title>图书列表</title>
图书编号 | 图书名称 | 图书作者 |
代码解释:
首先在第二行导入Thymeleaf 的名称空间
通过遍历将books中的数据展示出来,Thymeleaf中通过th:each进行集合遍历,通过th:text展示数据
浏览器输入"http://localhost:8081/books",查看运行结果,如图:
FreeMarker是一个历史悠久的模板引擎,适用于Web环境和非Web环境。FreeMarker 需要经过解析才能在浏览器中展示出来。FreeMarker 不仅可以用来配置HTML页面模板,也可以作为电子邮件模板、配置文件模板以及源码模板。整合步骤如下:
创建Spring Boot项目,然后添加spring-boot-starter-web和spring-boot-starter-freemarker依赖,如下:
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid></dependency><!-- 整合FreeMarker --><dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-freemarker</artifactid></dependency>
Spring Boot对FreeMarker 也提供了自动化配置类FreeMarkerAutoConfiguration,相关的配置属性在FreeMarkerProperties中,FreeMarkerProperties的部分源码如下:
@ConfigurationProperties(prefix = "spring.freemarker") public class FreeMarkerProperties extends AbstractTemplateViewResolverProperties { public static final String DEFAULT_TEMPLATE_LOADER_PATH = "classpath:/templates/"; public static final String DEFAULT_PREFIX = ""; public static final String DEFAULT_SUFFIX = ".ftl"; ... }
FreeMarker 默认模板位置和Thymeleaf 一样,都在classpath:/templates/中,默认文件后缀是.ftl,开发者可以在application.properties 中对这些默认配置进行修改,如下:
控制器和Thymeleaf 中的控制器一样,这里不再重复
在resources目录下的templates目录中创建books.ftl 文件,如下:
nbsp;html> <meta> <title>图书列表FreeMarker</title>
图书编号 | 图书名称 | 图书作者 |
${book.id} | ${book.name} | ${book.author} |
代码解释:
先判断model中的books部位可控并且books中有数据,然后再进行遍历
浏览器输入"http://localhost:8081/books",查看运行结果,如图:
已抢7616个
抢已抢97771个
抢已抢15290个
抢已抢54102个
抢已抢198736个
抢已抢88478个
抢