南昌高端网站开发费用表,域名价格排行,山东省济宁市嘉祥县建设局网站,北京建设住房和城乡建设官网Thymeleaf同jsp、volocity、freemarker等共同的职能是MVC模式中的视图展示层#xff0c;即View。 当然了#xff0c;SpringBoot中也可以用jsp,不过不推荐这种用法#xff0c;比较推崇的就是使用Thymeleaf。 关于Thymeleaf学习#xff0c;建议参考官方文档:https://www.thym…Thymeleaf同jsp、volocity、freemarker等共同的职能是MVC模式中的视图展示层即View。 当然了SpringBoot中也可以用jsp,不过不推荐这种用法比较推崇的就是使用Thymeleaf。 关于Thymeleaf学习建议参考官方文档:https://www.thymeleaf.org/documentation.html 官方文档例子应有尽有。 之前接触过Thymeleaf是因为公司项目初建期间用过它搭建过测试环境后来根据领导的指示需要快速开发而且当时对于Thymeleaf不是十分了解当时对于它的了解认识只是展示前端数据的同jsp职能一样当然这也是它们的共性。比较详细的深入了解和使用是帮助一位学妹解决毕业论文的项目问题。那时帮助她写了好十几个类代码和对应的十几个html代码。当时感触比较深的是Thymeleaf是如此的好用比jsp还好用jsp要遍历之类要么加%%或者引用jstl标签库进行数据遍历等。而Thymeleat就不需要。简洁直观我比较推崇Thymeleaf特别是在前后端分离的项目有的时候不用jsp,光纯htmljs的确有点吃力即便使用了vue.js或者angular.js、react.js等虽然增加了便利性但是了如果一个项目前端语言不是十分统一的话对于将来维护成本方面会大大提高不易维护。 Thymeleaf对于前后端分离我认为还是不错的。 下面进入demo例子讲解 一、导入maven依赖 ?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdorg.springframework/groupIdartifactIdgs-uploading-files/artifactIdversion0.1.0/versionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion1.5.8.RELEASE/version/parentpropertiesjava.version1.8/java.version/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-thymeleaf/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId/plugin/plugins/build/project 二、编写实体 package hello;public class User {private Integer id;private String name;public Integer getId() {return id;}public void setId(Integer id) {this.id id;}public String getName() {return name;}public void setName(String name) {this.name name;}} 三、编写Controller package hello;import java.util.ArrayList;
import java.util.List;import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;Controller
public class TestController {GetMapping(value/test)public String test(Model model) {ListUser userList new ArrayListUser();for (int i 0; i 10; i) {User user new User();user.setId(1);user.setName(张三);userList.add(user);}model.addAttribute(users, userList);return test;}
} 四、编写配置文件,将其放置在/src/main/resources下 application.properties server.port8080
spring.thymeleaf.cachefalse
spring.thymeleaf.prefixclasspath:/templates/
spring.thymeleaf.check-template-locationtrue
spring.thymeleaf.suffix.html
spring.thymeleaf.encodingUTF-8
spring.thymeleaf.content-typetext/html
spring.thymeleaf.modeHTML5 五、在src/main/resources目录下新建templates目录并新建html文件 test.htm html xmlns:thhttp://www.thymeleaf.org
bodyh2用户列表/h2divulli th:eachuser:${users}span th:text${user.id}/span-span th:text${user.name}/span-/li/ul/div
/body
/html 六、编写启动类 package hello;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;SpringBootApplication
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}} 最后结果如下图所示: 这里没有加入mysql或者其他数据相关的如果要引用数据库引用对应数据库的依赖和配置对应的数据库连接池即可。 这里可以参考我的关于SpringbootMyBatisThymeleaf 示例地址为:https://github.com/youcong1996/springboot-mybatis-demo转载于:https://www.cnblogs.com/youcong/p/9385918.html