问题总结

Spring-Boot+gradle+thymleaf配置过程笔记

刚开始实习,公司目前要我先配置起来一个SpringBoot+gradle+thymleaf的架子,然后在网上也查了些资料,毕竟用gradle的感觉不多,所以准备自己做些整理,也希望能帮助到有需要的人。

  1. 可以通过修改build.gradle文件中的属性来提高下载速度

    buildscript{
        repositories{
            maven { url "http://maven.aliyun.com/nexus/content/groups/public" }
        }
    }
    repositories{
            maven { url "http://maven.aliyun.com/nexus/content/groups/public" }
    }
    
  2. spring-boot项目中,在application.properties文件中配置数据库连接

    spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase?useSSL=false
    spring.datasource.username=root
    spring.datasource.password=root
    #数据库驱动
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    spring.jpa.hibernate.ddl-auto=create
    #是否在控制台显示数据库语句
    spring.jpa.show-sql=true
    
  3. 既然需要使用SpringBoot,那这三个注解肯定会经常用到。

    @RequestMapping、@GetMapping、@PostMapping的区别

    • @GetMapping是一个组合注解,是@RequestMapping(method=RequestMethod.GET)的缩写

    • @PostMapping是一个组合注解,是@RequestMapping(method=RequestMethod.POST)的缩写

    • @RequestMapping注解表示的是一个映射关系

      @RestController  
      @RequestMapping("/")  
      public class IndexController {  
          @RequestMapping("/home")  
          String get() {  
              //mapped to hostname:port/home/  
              return "Hello from get";  
          }  
          @RequestMapping("/index")  
          String index() {  
              //mapped to hostname:port/home/index/  
              return "Hello from index";  
          }  
      }/home 的请求会由 get() 方法来处理,而到 /home/index 的请求会由 index() 来处理
      
  4. POST和GET的区别

    • GET - 从指定的资源请求数据。

    • POST - 向指定的资源提交要被处理的数据

      比较 GET 与 POST

      下面的表格比较了两种 HTTP 方法:GET 和 POST。

      GETPOST
      后退按钮/刷新无害数据会被重新提交(浏览器应该告知用户数据会被重新提交)。
      书签可收藏为书签不可收藏为书签
      缓存能被缓存不能缓存
      编码类型application/x-www-form-urlencodedapplication/x-www-form-urlencoded 或 multipart/form-data。为二进制数据使用多重编码。
      历史参数保留在浏览器历史中。参数不会保存在浏览器历史中。
      对数据长度的限制是的。当发送数据时,GET 方法向 URL 添加数据;URL 的长度是受限制的(URL 的最大长度是 2048 个字符)。无限制。
      对数据类型的限制只允许 ASCII 字符。没有限制。也允许二进制数据。
      安全性与 POST 相比,GET 的安全性较差,因为所发送的数据是 URL 的一部分。在发送密码或其他敏感信息时绝不要使用 GET !POST 比 GET 更安全,因为参数不会被保存在浏览器历史或 web 服务器日志中。
      可见性数据在 URL 中对所有人都是可见的。数据不会显示在 URL 中。
  5. @Controller和@RestController的区别

    @RestController注解相当于@ResponseBody+@Controller合在一起的作用

    • 如果只是使用@RestController注解controller,则controller中的方法的返回值都是字符串形式返回

    • @Controller则可以配合Thymleaf实现前端页面展示

  6. gradle(4.3.1)中配置thymleaf

    • 在build.gradle中添加以下代码

      dependencies {
      	implementation('org.springframework.boot:spring-boot-starter-thymeleaf')
      }
      
    • 在前端页面添加标签库

      <html xmlns="http://www.w3.org/1999/xhtml"
      
      xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"> 
      
      
    • thymleaf缓存配置

      每次更改页面,如果不配置Thymeleaf 缓存设置为false,那么每次更改html页面都需要重启页面才刷新。解决方法:在application.properties文件中添加以下代码

      #Thymeleaf cache set
      spring.thymeleaf.cache=false
      

暂时就这么多,以后有问题会继续更新。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值