前一篇SpringBoot初步主要涉及后端代码,这一篇主要是记录一下springBoot中的前端知识。
1、thymeleaf
看网上好多使用thymeleaf模板构建springBoot前端,索性也尝试了一下。
先在static目录下放了张图片,浏览器中可以访问。
然后再templates目录下创建了个hello.html静态资源文件
application.properties中增加配置
#thymeleaf模板配置
spring.thymeleaf.mode=HTML5
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.cache=false
spring.thymeleaf.encoding=utf-8
spring.resources.chain.strategy.content.enabled=true
spring.resources.chain.strategy.content.paths=/**
此时浏览器中访问不到,经查找原因,在配置文件中又新增一下配置
#dev tools
spring.devtools.livereload.enabled=true
# 静态文件请求匹配方式
spring.mvc.static-path-pattern=/**
# 修改默认的静态寻址资源目录
spring.resources.static-locations = classpath:/templates/,classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/
再次访问成功。此时理解为:网上直接增加thymeleaf配置就可以的例子,是动态资源,我加的是静态资源,所以访问时报404的错。后来增加的是静态资源配置。(待更新)
2、ajax前端请求
实现功能:增加页面,使用jquery.ajax()向后端传递请求,并将后台获取的数据渲染至页面列表(调用users用户列表接口)。
过程中主要问题为jquery.ajax()方法以及后台对于入参和出参处理。
前端
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Springboot + Jsp</title>