springboot web开发(springboot web工程)

Springboot的Web开发支持

Springboot提供了spring-boot-starter-web为Web开发予以支持,spring-boot-starter-web为我们提供了嵌入的Tomcat以及Spring MVC的依赖,而Web相关的自动配置存储在
spring-boot-autoconfigure.jar

org.springframework.boot.autoconfigure.web包
下。



从这些文件名可以看出:


ServerPropertiesAutoConfiguration和ServerProperties自动配置内嵌的
Server容器


HttpEncodingAutoConfiguration和HttpEncodingProperties用来自动配置
http的编码

Thymeleaf模板引擎

Springboot提供了大量的模板引擎,Springboot推荐使用Thymeleaf作为模板引擎,因为Thymeleaf提供了完美的Spring MVC的支持。最好不要使用JSP,因为JSP在内嵌的Servlet容器中运行有一些问题,内嵌的Tomcat、Jetty不支持已jar形式运行JSP

基础知识

引入Thymeleaf

Thymeleaf是一个Java类库,它是一个xhtml、xml和html5的模板引擎,可以作为MVC的Web应用的View层。Thymeleaf提供了额外的模块与Spring MVC集成,所以我们可以使用Thymeleaf完全替代JSP

引入Thymeleaf:

<html xmlns:th=“http://www.thymeleaf.org”>

通过xmlns:th= http://www.thymeleaf.org命名空间,将镜头页面转换为动态的视图,需要进行动态处理的元素将使用”th:”为前缀

<link th:href="@{bootstrap/css/bootstrap.min.css}" rel="stylesheet"/>

<link th:href="@{bootstrap/css/bootstrap-theme.min.css}" rel="stylesheet"/>

<script type="text/javascript" th:src="@{jquery-1.10.2.min.js}"></script>

<script type="text/javascript" th:src="@{bootstrap/js/bootstrap.min.js}"></script>

通过@{}引入web静态资源文件,比如css、js文件。

访问model中的数据

通过${}访问model中的属性,这和JSP是很像的。



说明:使用<span th:text=”${singlePerson.name}”></span>访问model中的singlePerson的name属性。注意:需要处理的动态内容需要加上”th:”前缀。

model中的数据迭代

示例代码:



说明:使用th:each来做循环th:each=”person:${people}”),person作为迭代元素来使用,然后访问迭代元素中的属性就可以。

数据判断

示例代码:



说明:通过th:if=”${not #lists.isEmpty(people)}”表达式判断people是否为空,Thymeleaf支持>、<、>=、<=、 ==、!=作为比较条件,同时也支持将SpringEL表达式语言用于条件中。

在JS中访问model



说明:通过th:inline=“javascript”添加到script标签,这样js代码即可访问model中的属性。通过”[[${}]]”格式获得实际的值(Bean对象)

在html中访问model的属性

我们需要在列表后单击每一行后面的按钮获得model中的值,代码如下:



说明:注意格式th:onclick=”’getName(\”+${person.name}+’\’);’”

与Spring MVC集成(原理)

在Spring MVC中,如果我们需要集成一个模板引擎的话,需要定义ViewResolver,而ViewResolver需要定义一个View,代码示例:



我们使用了JstiView定义了一个
InternalResourceViewResolver,因而使用Thymeleaf作为我们的模板引擎也应该做类似的定义,庆幸的是Thymelaef为我们定义好了
org.thymeleaf.spring4.view.
ThymeleafView
org.thymeleaf.spring4.view.
ThymeleafViewResolver(默认使用ThymeleafView作为View)。Thymeleaf为我们提供了一个SpringTemplateEngine类,用来驱动在Spring MVC下使用Thymeleaf模板引擎,另外还提供了一个TemplateResolver用来设置通用的模板引擎(包含前缀、后缀),这使我们在Spring MVC中集成Thymeleaf引擎变得十分简单。代码如下:



Springboot Thymeleaf的支持

在Springboot中这一切都是不需要的,Springboot通过org.springframework.boot.autoconfigure.thymeleaf包对Thymeleaf进行了自动配置,如下:



通过
ThymeleafAutoConfiguration类
对集成所需要的Bean进行自动配置,包括templateResolver、templateEngine、thymeleafViewResolver的配置。

通过ThymeleafProperties类来配置Thymeleaf,在applicaiotn.properties中,以spring.thymeleaf开头来配置,通过查看ThymeleafProperties的主要源码,我们可以看出如何设置属性以及默认配置,源码如下:



实战

(1)在pom.xml文件中编辑

添加如下依赖:



说明:
spring-boot-starter-thymeleaf会自动包含spring-boot-starter-web

(2)编写JavaBean

用于在模板页面中展示数据使用



(3)添加脚本样式静态文件

根据默认原则,脚本样式、图片等静态文件应放在src/main/resources/static下,这里引入了Bootstrap和jQuery。



(4)编写页面

根据默认规则,页面应该放置在
src/main/resources/templates
下,在
src/main/resources/templates下新建
index.html文件,如图:



代码如下:



(5)准备数据

在Springboot的入口类TestApplication中进行编辑,代码如下:



说明:也可以在一个Controller类中编写这个方法,不用在Springboot的入口类中编写。

(6)运行

访问
http://localhost:8080/springboot/index,如图: