本章我将带领大家学习如何在Spring boot中实现Web开发。
1. Web开发方法介绍
Spring Boot提供了一套完整的Web开发流程,从后端到后端,再到数据库、计划任务、消息队列等。借助Spring框架开发Web应用程序通常有两种形式:
1、前后端分离
在这种方法中,后端开发和前端开发是完全分离的。 您只需要协商套接字即可。 前端负责开发页面并调用前端socket显示数据,后端负责提供Restful风格的socket。
2.使用Spring boot自带的模板
Springboot支持多种主流前端模板:
· 百里香叶
· 自由标记
· 格罗维
·胡子
· 速度
· JSP
需要注意的是,虽然Spring MVC支持JSP,但Spring Boot不建议使用JSP,因为使用嵌入式servlet容器时存在一些使用限制。 另外,Velocity在2010年后停止更新,所以不推荐两者。 。
Springboot可以同时支持以上模板。 什么是同时支持? 简而言之,Springboot项目中可以同时共存多个模板。 我们需要做的就是在pom文件中引入相关模板引擎的jar包。 .Springboot可以根据模板的后缀名来确定由哪个模板引擎来解析这个动态页面。
2.1 常用模板页面后缀
Thymeleaf : .html
freemaker : .ftl
jsp : jsp
2.2 前端与前端模板的区别
注意,以上模板都是前端模板,与后端模板(如Angular)不同。
1.前端模板:
前端模板一般是模板提供的js,根据模板指定的语法规则解析html中的模板标签;
2.后台模板:
与后端类似,页面请求到达后,后端模板引擎根据特定的语法规则解析模板中的内容,将数据填充到模板中,最终返回给浏览器的实际上是一个完整的HTML页面。
2. 使用 Spring Boot 构建 Web 项目 1. 创建 Maven 项目及其模块 注意:
为了方便我们后面描述其他SpringBoot知识点,我们构建一个SpringBoot项目,然后在这个项目中创建多个模块。 每个模块都描述了一个SpringBoot知识点,这样我们就不需要构建多个intelliJ项目。
我们创建一个maven项目(注意,不要勾选create from archytype,虽然它会帮你创建项目骨架,但是它会从内网下载一些东西,速度很慢网站前后端页面模板,导致卡住)创建一个Web项目。
1.1 创建父项目--boot-demos
选择Maven构建项目
设置项目的GAV坐标
1.2 在父项目中创建子模块--demo05
子模块也是一个Maven框架。
如果我们的项目案例中没有使用默认的src目录,可以将其删除。
接下来我们将介绍如何在demo05模块中实现一个简单的Web功能。
2、改造pom.xml文件
我们修改demo05的pom.xml文件中的代码,将项目转换为spring-boot项目网站前后端页面模板,并添加web依赖。
org.springframework.boot
spring-boot-starter-parent
2.2.5.RELEASE
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
3.Thymeleaf简介
Thymeleaf 是一个 XML/XHTML/HTML5 模板引擎,可用于 Web 和非 Web 环境中的应用程序开发。 它提供了一个用于集成SpringMVC的可选模块。 在应用程序开发中,您可以使用Thymeleaf完全替代JSP或其他模板引擎,例如VelocityFreeMarker等。
1.Thymeleaf样本模板
Name
Price
Oranges
0.99
可以看出Thymeleaf主要是以属性的形式添加到html标签中的。 浏览器解析html时,检测到该属性不存在时会忽略该属性。 因此,Thymeleaf的模板可以直接通过浏览器打开进行解释,特别好用。 有利于前后端分离。
2.使用Thymeleaf模板实现Web开发步骤2.1添加thymeleaf依赖包
在我们的Spring Boot项目demo05中使用Thymeleaf,只需要引入以下依赖即可。
org.springframework.boot
spring-boot-starter-thymeleaf
2.2 创建thymeleaf模板文件
在 src/main/resources/ 目录中创建文件夹 templates。 默认情况下名称是固定的。 在此目录中创建一个index.html。
index
Hello
2.3 编写控制器代码
创建一个HelloController类,并在其中编译一个控制器socket,如图所示。
@Controller
public class HelloController {
@RequestMapping("/")
public String index(ModelMap map) {
// 添加一个属性,用来在模板中根据这个key来读取对应的值
map.addAttribute("msg", "跟一一哥学习SpringBoot");
// return 模板文件的名称-->对应src/main/resources/templates/index.html
return "index";
}
}
2.4 创建Application入口类
package com.yyg.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 创建web项目
*/
@SpringBootApplication
public class WebApplication {
public static void main(String[] args){
SpringApplication.run(WebApplication.class,args);
}
}
完整的项目结构:
完整的pom.xml文件:
4.0.0
demo05
org.springframework.boot
spring-boot-starter-parent
2.2.5.RELEASE
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-thymeleaf
net.sourceforge.nekohtml
nekohtml
1.9.22
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
2.5 启动入口类
在浏览器中输入:8080/,看到如下结果。
这是在 Thymeleaf 模板的帮助下呈现的页面。
它在不破坏HTML本身内容的情况下实现了数据的逻辑分离。
更多Thymeleaf页面句型,可以参考我的另一篇关于Thymeleaf的博客!
四、SAXParseException异常处理解决方案 1、问题描述
如果我们的模板文件,即index.html文件,是编译时默认生成的html文件,如图:
那么启动项目后,访问socket可能会出现如下异常:
org.xml.sax.SAXParseException: 元素类型 "meta" 必须由匹配的结束标记 "" 终止。
2.异常原因如图
3、解决方案
1、修改thymeleaf.mode的值为LEGACYHTML5;
2、在pom.xml文件中添加依赖包:
net.sourceforge.nekohtml
nekohtml
1.9.22
完成以上两步后,在以后的HTML文件中,即使标签不成对出现或者标签没有结束标签,也不会再出现解析异常。
5. Spring Boot中的配置文件
在Spring Boot中,如果需要更改默认配置,只需要将下面需要更改的属性复制到application.properties中,更改为需要的值,比如更改模板文件的扩展名、修改默认模板路径等
1.Thymeleaf默认配置属性
# Enable template caching.
spring.thymeleaf.cache=true
# Check that the templates location exists.
spring.thymeleaf.check-template-location=true
# Content-Type value.
spring.thymeleaf.servlet.content-type=text/html
# Enable MVC Thymeleaf view resolution.
spring.thymeleaf.enabled=true
# Template encoding.
spring.thymeleaf.encoding=UTF-8
# Comma-separated list of view names that should be excluded from resolution.
spring.thymeleaf.excluded-view-names=
# Template mode to be applied to templates. See also StandardTemplateModeHandlers.
spring.thymeleaf.mode=HTML5
# Prefix that gets prepended to view names when building a URL.
spring.thymeleaf.prefix=classpath:/templates/
# Suffix that gets appended to view names when building a URL.
spring.thymeleaf.suffix=.html
# Order of the template resolver in the chain.
spring.thymeleaf.template-resolver-order=
# Comma-separated list of view names that can be resolved.
spring.thymeleaf.view-names=