SpringBoot_整合视图层技术
SpringBoot整合视图层技术
在目前的企业级应用开发中,前后端分离是趋势,但是视图层技术还占有一席之地。Spring Boot对视图层技术提供了很好的支持,官方推荐使用的模板引擎是Thymeleaf,不过像FreeMarker也支持,JSP 技术在这里并不推荐使用。
Thymeleaf
配置
引入jar包:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
spring boot配置:
#thymeleaf start
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
#开发时关闭缓存,不然没法看到实时页面
spring.thymeleaf.cache=false
#thymeleaf end
页面引入:
<html xmlns:th="http://www.thymeleaf.org">
标签的使用
引入url
<a th:href="@{http://blog.csdn.net/u012706811}">绝对路径</a>
<a th:href="@{/}">相对路径</a>
<a th:href="@{css/bootstrap.min.css}">Content路径,默认访问static下的css文件夹</a>
当要写动态链接时括号属性如:@{user(id=${user.id})()..} 相等于 user?id=1&..
th:action
<form id="form" th:action="@{/login}">...</form>
th:href
<a th:href="@{/logout}" ></a>
th:src
<script th:src="@{/resources/js/jquery/jquery.json-2.4.min.js}"
th:text
<td th:text="${username}" ></td>
th:value
<option th:value="Adult">Adult</option>
<input type="hidden" th:value="${msg}" />
th:include
th:replace
th:fragment
声明定义该属性的div为模板片段,常用与头文件、页尾文件的引入。常与th:include,th:replace一起使用。
例如:
声明模板片段/WEBINF/templates/footer. html
<div th: fragment=" copy" >
© 2011 The Good Thymes Virtual Grocery
</div>
引入模板片段
<div th: include=" /templates/footer : : copy" ></div>
<div th: replace=" /templates/footer : : copy" ></div>
th:if
<div th:if="${rowStat.index} == 0">... do something ...</div>
th:each
<form id="login-form" th:action="@{/addStudent}"
th:object="${stuReqBean}" method="POST">
<div class="student" th:each="stuIter,rowStat:${stuReqBean.students}">
<input type="text" class="firstName" value=""
th:field="*{students[__${rowStat.index}__].firstName}"></input>
<input type="text" class="school" value=""
th:field="*{students[__${rowStat.index}__].school}"></input>
...
</div>
</form>
上面的例子中通过选择表达式*{}既能将表单绑定到后台的StudentRequestBean中的集合属性students,也能将Servlet上下文中的StudentRequestBean中的List类型的students变量回显,回显时通过th:each进行遍历。
注意1:绑定集合属性元素下标的用法*{students[__${rowStat.index}__].firstName}
注意2:如果List<Student> students为null,页面将无法显示表单,后台必须给students初始化一个值,即:
List<Student > stus = new ArrayList<Student >();
stus .add(new Student ());
StudentRequestBean.setStudents(stus );
注意3:stuIter代表students的迭代器
th:object
<form id="login-form" th:action="@{/login}" th:object="${loginBean}">...</form>
th:field
<form id="login-form" th:action="@{/login}" th:object="${loginBean}">...
<input type="text" value="" th:field="*{username}"></input>
<input type="text" value="" th:field="*{user[0].username}"></input>
</form>
SpringBoot_整合视图层技术的更多相关文章
- Spring Boot 整合视图层技术
这一节我们主要学习如何整合视图层技术: Jsp Freemarker Thymeleaf 在之前的案例中,我们都是通过 @RestController 来处理请求,所以返回的内容为json对象.那么如 ...
- Spring Boot 整合视图层技术,application全局配置文件
目录 Spring Boot 整合视图层技术 Spring Boot 整合jsp Spring Boot 整合freemarker Spring Boot 整合视图层技术 Spring Boot 整合 ...
- SpringBoot视图层技术
一.SpringBoot整合jsp 在maven的dependencies的依赖中除了springBoot启动器还要添加对jstl和jsp的依赖. <dependencies> <d ...
- SpringBoot整合持久层技术--(一)JdbcTemplate
简介: JdbcTemplate是Spring提供的一套JDBC模板框架,利用AOP技术解决直接使用JDBC带来的重复代码问题.它没有MyBatis使用那么灵活,但是却比直接使用JDBC方便得多.Sp ...
- SpringBoot整合持久层技术--(三)Spring Data JPA
简介: JPA(java Persistence API)和SpringData是两个范畴的概念.spring data jpa是spring公司下的spring data项目的一个模块. sprin ...
- SpringBoot整合持久层技术--(二)MyBatis
简介: 原名iBatis,SpringBoot中使用MyBatis: pom.xml <dependency> <groupId>org.springframework.boo ...
- Spring Boot 整合Web 层技术(整合Servlet)
1 整合Servlet 方式一1.1通过注解扫描完成Servlet 组件的注册 1.1.1创建Servlet /*** 整合Servlet 方式一*/@WebServlet(name = & ...
- SpringBoot整合持久层技术-创建项目
新建项目 Pom.xml文件 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&q ...
- springboot整合持久层技术(mysql驱动问题)
java.sql.SQLException: The server time zone value '�й���ʱ��' is unrecognized or represents more tha ...
随机推荐
- linux系列之-—03 常见问题
问题1 描述:Linux如何查看JDK的安装路径 问题2 描述:执行shell脚本时报错,错误信息为:bash: line 19: jar: command not found 原因:因为在系统环境变 ...
- SonarQube---在具体项目中的使用
一.简介 SonarQube(简称Sonar)是管理代码质量的开放平台,它可以快速地对代码质量进行分析,并给出合理的解决方案,提高管理效率,保证代码质量. Sonar官网,文档 Sonar Scann ...
- 【转】TestNG中的并发(多线程)
优势 并行(多线程)技术在软件术语里被定义为软件.操作系统或者程序可以并行地执行另外一段程序中多个部分或者子组件的能力 多线程方式拥有很大的优势: 1). 减少测试运行时间 如果测试集里包含了大量的用 ...
- Ubuntu14.04 64bit下Caffe + CUDA 6.5安装详细步骤
不多说,直接上干货! 笔者花了很长时间才装完,主要是cuda安装和opencv安装比较费劲,cuda找不到32位的安装包只好重装64位的ubuntu系统,opencv 也是尝试了很久才解决,这里建议用 ...
- Elasticsearch - 搜索类型与搜索位置
一.搜索类型: Elasticsearch同意用户选择其所希望的处理查询的方式. 由于存在一些不同的情形,对其使用不同的搜索类型才是合适的.为了控制查询的运行方式,我们能够在请求中使用search_t ...
- jquery源码学习笔记二:jQuery工厂
笔记一里记录,jQuery的总体结构如下: (function( global, factory ) { //调用factory(工厂)生成jQuery实例 factory( global ); }( ...
- myeclipse提示:Syntax error on tokens, delete these tokens怎么解决
有中文字符或者符号,包括空格. 上次遇到一个问题,检查了一遍语法没错误, 后来发现是拷贝代码的时候有一部分中文空格没删除,就出现这个问题了. 一个个删除就OK了.
- ValueError: Some of types cannot be determined by the first 100 rows, please try again with sampling
ValueError: Some of types cannot be determined by the first 100 rows, please try again with sampling ...
- 七.OC基础加强--1.内存管理 2.野指针,内存泄露 3.set方法的内存管理 4.@property参数 5.@class和循环retain的使用 6.NSString的内存管理
1,内存管理简单介绍 1,为什么要有内存管理? malloc selloc dealloc```需要回头复习 一般的内存 4s 是512m内存:6 是1024m内存: 当内存过大时,会耗尽内存.出现程 ...
- 并不对劲的bzoj1305: [CQOI2009]dance跳舞
传送门-> 又是陈年老坑. 听上去不知道从何下[手]?那要是把题目换成“判断这些人能否条x支舞”呢? 这样就变成了一个网络流可以解决的问题,只要把每个人拆成喜欢和不喜欢两点,每个人两点总流量不超 ...