前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringBoot笔记

SpringBoot笔记

作者头像
CBeann
发布2023-12-25 16:25:25
960
发布2023-12-25 16:25:25
举报
文章被收录于专栏:CBeann的博客CBeann的博客

IDEA打开Run Dashboard窗口

修改D:\work\IntelliJ IDEA 2018.2.4Workspace\SpringCloudDemoHoxton\.idea下的workspace.xml

代码语言:javascript
复制
 <option name="configurationTypes">
      <set>
        <option value="SpringBootApplicationConfigurationType" />
      </set>
    </option>

SpringBoot加入webjars依赖

将原来的BootStrap.js用依赖的方式加入到项目中(以BootStrap为例)

1)访问 WebJars - Web Libraries in Jars

2)选择好 版本 和 引入的方式

3)在依赖中查看静态资源的路径

4)开启服务器并且访问

http://localhost:8080/webjars/bootstrap/4.0.0/js/bootstrap.js

5)webjars在html中的使用

注意:引入的是css文件而不是js文件,引入的是css文件而不是js文件,引入的是css文件而不是js文件

代码语言:javascript
复制
<link rel="stylesheet"  href="/webjars/bootstrap/4.0.0/css/bootstrap.css" />

thymeleaf中使用webjars

代码语言:javascript
复制
<link rel="stylesheet"  th:href="@{/webjars/bootstrap/4.0.0/css/bootstrap.css}" />

SpringBoot整合Thymeleaf

整合

1)加入依赖

代码语言:javascript
复制
<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>

2)在hml中引入命名空间

代码语言:javascript
复制
<html xmlns:th="http://www.thymeleaf.org"  lang="en">  

3)关闭缓存

代码语言:javascript
复制
#开发时关闭缓存
spring.thymeleaf.cache=false

4)开始使用

代码语言:javascript
复制
<div th:text="${name}"></div>

语法

取一个普通的值

代码语言:javascript
复制
<div th:text="${name}"></div>

foreach循环

代码语言:javascript
复制
<div> 
       <p th:text="${message}" th:each="message : ${userlist}" />
  </div>

if语句

代码语言:javascript
复制
  <p th:text="男" th:if="${sex==1}"></p> 
   <p th:text="女" th:if="${sex==0}"></p> 

URL

代码语言:javascript
复制
<a th:href="@{'/www.baidu.com?sex='+${sex}}">click me</a>

<form  method="post"  th:action="@{'/hello?sex='+${sex}}">
代码语言:javascript
复制
@RequestMapping("/success")
	public String hello(Map<String,Object> map){
		map.put("name", "张三");
		List<String> list=new ArrayList<>();
		list.add("zhangsan");
		list.add("lisi");
		list.add("wangwu");
		map.put("userlist", list);
		map.put("sex", 0);
		return "success";
	}

SpringBoot整合swagger

代码语言:javascript
复制
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
			<version>2.9.2</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger-ui</artifactId>
			<version>2.9.2</version>
		</dependency>
代码语言:javascript
复制
@EnableSwagger2

http://localhost:8080/swagger-ui.html

SpringBoot定制错误页面

1)没有模板引擎
2)有模板引擎

error文件夹放在templates文件下

SpringBoot更改项目图标

步骤

在项目的静态资源文件夹下放一个favicon.ico图标,注意:名字为favicon.ico favicon.ico favicon.ico

启动项目即可

依据

SpringBoot获得application.properties中数据

代码语言:javascript
复制
package com.example.demo.entity;  
  
import org.springframework.beans.factory.annotation.Value;  
import org.springframework.context.annotation.PropertySource;  
import org.springframework.stereotype.Component;  
  
@Component  
@PropertySource("classpath:jdbc.properties")//如果是application.properties,就不用写@PropertyScource("application.properties"),其他名字用些  
public class Jdbc {  
      
    @Value("${jdbc.user}")
    private String user;  
      
    @Value("${jdbc.password}") 
    private String password;  
      
    public void speack(){  
        System.out.println("username:"+user+"------"+"password:"+password);  
    }  
  
}

SpringBoot junit 测试

代码语言:javascript
复制
package com.example.demo.service;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import com.example.demo.SpringBootTestApplication;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes=SpringBootTestApplication.class)//制定SpinrgBoot的启动类
@WebAppConfiguration//由于是web项目,所以要模拟Servletontext
public class TestHelloService {
	
	@Autowired
	private HelloService service;

	@Test
	public void test() {
		service.speak();
	}

}

SpringBoot全局异常捕捉

@ControllerAdvice @ExceptionHandler(value=Exception.class) @ResponseBody 一起使用

作用:当遇到一个错误时,可以捕捉到并且返回相应信息

代码语言:javascript
复制
@ControllerAdvice
public class AllException {	
	@ExceptionHandler(value=Exception.class)
	@ResponseBody
	public String exception(Exception e){
		return "处理错误!";
	}
}

SpringBoot使用自定义的properties

自定义的文件名是demo.properties,其中demo.name=李四

代码语言:javascript
复制
demo.id=1
demo.name=\u674E\u56DB
代码语言:javascript
复制
package com.example.demo.bean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component//加入到ioc容器
@PropertySource("classpath:demo.properties")//引入自己的properties
public class Demo {
	
	@Value("${demo.id}")
	private Integer id;
	
	@Value("${demo.name}")
	private String name;
 
	public Integer getId() {
		return id;
	}
 
	public void setId(Integer id) {
		this.id = id;
	}
 
	public String getName() {
		return name;
	}
 
	public void setName(String name) {
		this.name = name;
	}
 
	@Override
	public String toString() {
		return "Demo [id=" + id + ", name=" + name + "]";
	}
	
	
 
}

SpringBoot导入XML

代码语言:javascript
复制
package com.example.demo.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

/** 
  * 
  * classpath路径:locations={"classpath:classpath:context.xml","classpath:classpath:context2.xml"} 
  * file路径: locations = {"file:d:/test/classpath:context.xml"};
 */ 
@Configuration 
@ImportResource(locations={"classpath:context.xml"}) 
public class Config {

}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-03-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • IDEA打开Run Dashboard窗口
  • SpringBoot加入webjars依赖
  • SpringBoot整合Thymeleaf
    • 整合
      • 语法
        • 1)没有模板引擎
        • 2)有模板引擎
        • 步骤
        • 依据
    • SpringBoot整合swagger
    • SpringBoot定制错误页面
    • SpringBoot更改项目图标
    • SpringBoot获得application.properties中数据
    • SpringBoot junit 测试
    • SpringBoot全局异常捕捉
    • SpringBoot使用自定义的properties
    • SpringBoot导入XML
    相关产品与服务
    容器服务
    腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档