前言:
前段时间大约花了20天的时间学习SpringBoot,并完成了一个功能完整的信息管理网站,在此做一下记录。
使用的框架/工具的版本:
整体框架 SprongBoot :1.5.12Release
前端框架模板:thymeleaf
数据库:MySQL+Jpa
项目管理工具 maven :3.3.9
配置文件中的信息如下
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/数据库名
username: 用户名
password: 密码
jpa:
hibernate:
ddl-auto: update
show-sql: true
thymeleaf:
prefix: classpath:/templates/
suffix: .html
mode: LEGACYHTML5
encoding: UTF-8
content-type: text/html
cache: false
mvc:
static-path-pattern: /static/**
logging:
level:
root: WARN
org:
springframework:
web: DEBUG
hibernate: ERROR
从前到后遇到的坑很多,但是因为没有及时记录,忘记了许多,只能是将遇到的一些让人印象深刻的记下来,供大家参考和自己以后回忆。
一、使用thymeleaf模板的注意事项
首先,thymeleaf模板的使用非常简单。只需要在pom.xml文件中加入配置项,或者使用IDEA建立项目的时候选中thymeleaf即可。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
问题一:
默认的thymeleaf模板是严格要求HTML页面使用HTML5标准,这导致了很多时候无法直接使用现成的HTML模板。
查找之后,发现解决方案为在pom.xml文件中添加如下代码
<!--启用不严格检查html-->
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.22</version>
</dependency>
问题二:
使用静态资源的问题,在SpringBoot+thymeleaf项目中不能直接使用本地路径调用本地资源,
当资源在项目中的resource目录下的时候
可以直接使用如下代码调用aaa.jpg图片
<img th:src="@{static/aaa.jpg}">
<img th:src="@{static/assets/****.jpg}">
当想要使用的资源在项目外的时候,则需要自己建立一个MyWebAppConfigurer类,
@Configuration
public class MyWebAppConfigurer extends WebMvcConfigurerAdapter implements ApplicationContextAware {
private ApplicationContext applicationContext;
public MyWebAppConfigurer(){
super();
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("file:../image/").addResourceLocations("classpath:/static/");
super.addResourceHandlers(registry);
}
}
在addResourceLocationos中加入想要加入的静态文件的路径
问题三
form表单提交。在数据传输的过程中会使用到很多thymeleaf模板中的语法。这里写出一些比较复杂的,并且常用到的
<form th:action="@{|/activity/${activity.getActivit_id()}|}" method="delete" th:method="delete">
<input type=&