SpringBoot的Thymeleaf支持,结合SpringMVC做的案例(学习SpringBoot实战)
2017-10-07 17:38
656 查看
1、创建SpringBoot项目
创建项目可以参考:http://blog.csdn.net/tototuzuoquan/article/details/78167839中通过Spring Initializr创建项目的过程。在这个过程中的选择thymeleaf模板引擎。
2、创建好之后的目录结构:
3、pom.xml的内容
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.wisely</groupId> <artifactId>ch7_2</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>ch7_2</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.7.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
4、Person的内容如下
package com.wisely; public class Person { private String name; private Integer age; public Person() {} public Person(String name, Integer age) { this.name = name; this.age = age; } public String getName() { return name; } public Integer getAge() { return age; } public void setName(String name) { this.name = name; } public void setAge(Integer age) { this.age = age; } }
5、Ch72Application的内容如下
package com.wisely; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import java.util.ArrayList; import java.util.List; @Controller @SpringBootApplication public class Ch72Application { @RequestMapping("/") public String index(Model model) { Person single = new Person("aa",11); List<Person> people = new ArrayList<Person>(); Person p1 = new Person("xx",11); Person p2 = new Person("yy",22); Person p3 = new Person("zz",33); people.add(p1); people.add(p2); people.add(p3); model.addAttribute("singlePerson", single); model.addAttribute("people", people); return "index"; } public static void main(String[] args) { SpringApplication.run(Ch72Application.class, args); } }
6、index.html的内容如下:
其中jQuery和bootstrap的包不引入<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta content="text/html;charset=UTF-8"/> <meta http-equiv="X-UA-Compatible" content="IE=edge"/> <meta name="viewport" content="width=device-width, initial-scale=1"/> <link th:href="@{bootstrap/css/bootstrap.min.css}" rel="stylesheet"/> <link th:href="@{bootstrap/css/bootstrap-theme.min.css}" rel="stylesheet"/> <title>Title</title> </head> <body> <div class="panel panel-primary"> <div class="panel-heading"> <h3 class="panel-title">访问model</h3> </div> <div class="panel-body"> <span th:text="${singlePerson.name}"></span> </div> </div> <div th:if="${not #lists.isEmpty(people)}"> <div class="panel panel-primary"> <div class="panel-heading"> <h3 class="panel-title">列表</h3> </div> <div class="panel-body"> <ul class="list-group"> <li class="list-group-item" th:each="personItem:${people}"> <span th:text="${personItem.name}"></span> <span th:text="${personItem.age}"></span> <button class="btn" th:onclick="'getName(\'' + ${personItem.name} + '\');'">获得名字</button> </li> </ul> </div> </div> </div> <script th:src="@{jquery-1.10.2.min.js}" type="text/javascript"></script> <script th:src="@{bootstrap/js/bootstrap.min.js}"></script> <script th:inline="javascript"> var single = [[${singlePerson}]]; console.log(single.name+"/"+single.age) function getName(name){ console.log(name); } </script> </body> </html>
7、运行
在浏览器中输入:http://localhost:8080/,效果如下:参考文章:
关于thymeleaf的知识,可以访问:http://www.thymeleaf.org的官网
相关文章推荐
- SpringBoot学习-第一章 新的Spring基础-<Spring Boot 实战>
- SpringBoot学习-第四章 SpringMVC基础-<Spring Boot 实战>
- SpringBoot学习-第二章 常用配置方式-<Spring Boot 实战>
- springboot学习三(springboot结合spring data jpa和freemarker显示)
- springboot学习(4)springboot使用thymeleaf完成数据的页面展示
- SpringBoot-结合thymeleaf和mybatis案例
- SpringBoot学习-支持thymeleaf模板引擎
- SpringBoot内部配置:“application.properties配置”和”使用XML配置”,读取属性文件中的内容,日志配置,Profile配置(学习:SpringBoot实战)
- SpringBoot学习-第三章 Spring高级应用-<Spring Boot 实战>
- Spring Boot 整合 Thymeleaf 完整 Web 案例
- 从.Net到Java学习第六篇——SpringBoot+mongodb&Thymeleaf&模型验证
- Spring Boot学习记录(二)--thymeleaf模板
- 深入学习spring-boot系列(三)--使用thymeleaf模板
- 【系统学习SpringBoot】SpringBoot读取Property配置
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)
- SpringBoot学习:Hibernate,Mybatis分别与Springboot的整合
- 深入学习spring-boot系列(三)--使用thymeleaf模板
- 【系统学习SpringBoot】SpringBoot新建HelloWorld工程(IDEA)
- 学习一下《JavaEE开发的颠覆者 Spring Boot实战 》
- SpringBoot学习:Spring Boot使用FastJson解析JSON数据