Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Tue Jun 30 17:24:02 CST 2015 There was an unexpected error (type=Not Found, status=404). No message available
那麼就是由於你所建立的包不在springboot的主配置類所在包內,點擊查看詳情css
含有註解@SpringBootApplication的類,好比默認建立好的主配置類是這樣子的:html
package com.test.HelloWord; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class HelloWordApplication { public static void main(String[] args) { SpringApplication.run(HelloWordApplication.class, args); } }
要想使用註解,建立的包必須所有在package com.test.HelloWord內部java
1.@SpringBootConfiguration ------- 2. @EnableAutoConfigurationjquery
1.@SpringBootConfigurationweb
這個註解又包含:spring
@Configuration,它表示配置類:bootstrap
- 該類是一個配置類
- 加了這個註解會自動放入spring 容器
- @EnableAutoConfiguration:使用springBoot能夠自動配置,擺脫了ssm中使用spring.xml,mybatis.xml,以及springmvc.xml文件配置的繁瑣,工做原理就是就是找到主配置類所在的包,並將該包以及所在的子包歸入控制器
spring 在啓動時會根據D:\MAVENRes\org\springframework\boot\spring-boot-autoconfigure\2.1.0.RELEASE\spring-boot-autoconfigure-2.1.0.RELEASE.jar下面的/META-INF/spring.factories自動加載第三方jar包數組
@Conditional註解:瀏覽器
在application.properties加入debug=true便可tomcat
1.配置文件的做用:就是對默認的配置進行修改
2.默認的全局配置文件:
<server> <port>8888</port> </server>
而yml如何實現更改端口,參考下面代碼:
server: port: 8888 注意‘:’與8888之間存在空格
要注意port要與server垂直對齊
3.具體如何修改默認配置舉一個小例子:
默認端口是8080,若是我想修改爲其餘的端口號只須要這步操做便可: server.port=8081,只須要這一句話便可以把端口號改成8081
4.在yml文件中對象屬性進行操做:
直接看代碼:
package com.test.HelloWord.po; @Component//做用是將此bean放入spring容器 @ConfigurationProperties(prefix="StudentPo")//做用是將StudentPo可以被yml文件識別,並能對其進行屬性的注入 public class StudentPo { private String name; private int age; private boolean sex; private Data birthday; private Map<String, Object> location; private String hobbbies[]; private List<String> skills; private PetPo pet; 此處省略構造函數以及get set方法 }
而yml文件中的內容以下:
StudentPo: name: zx age: 21 sex: true birthday: 2018/11/21 location: {province: 江蘇省,city: 南京市,zone: 玄武區}//注:這種是對map函數的賦值方法,此處雖然沒有加引號,可是若是字段裏面有轉意符,好比\n,\t等,要想使轉意生效,就必須加雙引號 hobbbies: - 唱歌 - 運動 //這種是對數組進行賦值的方法 skills: - 計算機 - 軟件開發 //這種是對數組進行賦值的方法 pet: nickName: luckliy strain: 哈士奇 //這種是對屬性爲對象的賦值方法
測試語句以下:
@RunWith(SpringRunner.class) @SpringBootTest public class HelloWordApplicationTests { @Autowired StudentPo stu;//因爲已經將student加上註解:@Component//做用是將此bean放入spring容器,因此能夠進行自動注入 @Test public void contextLoads() { System.out.println(stu.toString()); } }
public class StudentPo { @Value("zx")//加上這條註解以後name的值就變爲zx private String name; @Value("23")//age就變爲23 private int age; }
# | @ConfigurationProperties | @Value |
---|---|---|
注值 | 批量注入 | 單個 |
spEL | 不支持 | 支持 |
JSR303 | 支持 | 不支持 |
注入複雜類型 | 支持 | 不支持 |
複雜類型:除了(8個基本類型,String,Date類型之外的都是複雜類型)
下面重點講解一下JSR303校驗的用法:
@Component//做用是將此bean放入spring容器 @ConfigurationProperties(prefix="student")//做用是將StudentPo可以被yml文件識別,並能對其進行屬性的注入 @Validated//開啓jsr303數據校驗 public class StudentPo { @Email private String emial }
經過註解@PropertySource來加載不是默認配置文件的文件,注:默認配置文件只有兩種:
先看看conf.properties
student.age=66
再接着看如何使用:
@Component//做用是將此bean放入spring容器 @ConfigurationProperties(prefix="student")//做用是將StudentPo可以被yml文件識別,並能對其進行屬性的注入 @Validated//開啓jsr303數據校驗 @PropertySource(value= {"classpath:conf.properties"})//做用是引入配置文件 public class StudentPo { private int age }
可是這個註解有個缺陷就是僅支持.properties文件
@SpringBootApplication @ImportResource({"classpath:spring.xml"})//此處是新加入的 public class HelloWordApplication { public static void main(String[] args) { SpringApplication.run(HelloWordApplication.class, args); } }
具體如何調用這個xml文件,咱們先看看這個xml文件的內容:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <bean id="PetPo" class="com.test.HelloWord.po.PetPo"> <property name="nickName" value="zhuxu"></property> <property name="strain" value="哈士奇"></property> </bean> </beans>
對應的PetPo對象:
package com.test.HelloWord.po; public class PetPo { private String nickName; private String strain; 對應的get set方法已經省略,可是必定要加進去,不然屬性就沒法注入 }
對應的測試程序:
@RunWith(SpringRunner.class) @SpringBootTest public class HelloWordApplicationTests { @Autowired ApplicationContext ac;//此處是爲了獲取spring容器 @Test public void test1() { PetPo petPo= (PetPo)ac.getBean("PetPo"); System.out.println(petPo.toString()); }}
可是並不推薦這種方式進行屬性的注入,太麻煩,推薦使用配置類
配置類就是配置文件(.xml文件)配上註解的形式,如何建立配置類:
@Configuration//加上他以後這個類就是配置類 public class AppConfig { @Bean//加上它以後就至關於建立了一個<bean></bean>標籤 public PetPo ppp() {/*ppp至關於<bean id="" class=""></bean>中的id*/ PetPo p=new PetPo(); return p; /*返回的結果類型就至關於<bean id="" class=""></bean>中的class*/ } }
下面寫一個測試方法本身感覺一下
@RunWith(SpringRunner.class) @SpringBootTest public class HelloWordApplicationTests { @Autowired ApplicationContext ac;//此處是爲了獲取spring容器 @Test public void test1() { PetPo petPo= (PetPo)ac.getBean("p"); System.out.println(petPo.toString()); } }
student.age=${}
application-dev.properties application-test.properties
至於具體怎麼切換:就是在主配置文件(application.properties)中加入:
spring.profiles.active=dev
表示切換到dev下的端口環境
server: port: 1234 --- server: port: 8880 spring: profiles: dev//用於聲明環境名爲dev --- server: port: 8881 spring: profiles: test //用於聲明環境名test
這樣的話就建立好了三個環境,可是至於使用哪個環境,就得使用這樣的配置:
在主端口中這樣聲明: server: port: 1234 spring: profiles: active: dev //切換到端口到dev環境
右單擊 ->Run As->RunConfigurations->Arguments->在裏面輸入--spring.profiles.active=環境名 好比:--spring.profiles.active=dev
右單擊項目->Run As->maven build,進入以後在package便可
下面再接着敘述如何在cmd中運行打成的jar包
java -jar HelloWord-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev
右單擊 ->Run As->RunConfigurations->Java Application->HellowdApplication(此處爲項目名,項目不一樣名字也不同)->右邊的Argument, 輸入 -Dspring.profiles.active=dev
springBoot可以默認讀取的文件有兩種,一個是application.properties以及application.yml文件
可是這兩個文件能夠存放在哪個位置,就是在
在主配置文件application.properties中加入:
server.servlet.context-path=/springBoot
注意:此處的springBoot就是新加入的項目名,在未加入以前訪問是這樣的:
http://localhost:8888/HelloWord?name=zhuxu
加了以後就變成了這樣的:
http://localhost:8888/springBoot/HelloWord?name=zhuxu
好比說在個人這個路徑下C:\Users\17732\Desktop\test\application.properties有一個application.properties文件,那麼怎麼使用裏面的配置:
右單擊 ->Run As->RunConfigurations->Arguments->在裏面輸入--spring.config.location=路徑名 好比:--spring.config.location=C:\Users\17732\Desktop\test\application.properties
若是內部外部配置有衝突,優先選擇外部的
接着學習一下如何經過cmd命令調用外部配置文件
java -jar HelloWord-0.0.1-SNAPSHOT.jar --spring.config.location=C:\Users\17732\Desktop\test\application.properties
設置某一個參數,仍是按照這個步驟(右單擊 ->Run As->RunConfigurations->Arguments)到Arguments下
好比說只更改端口:–server.port=8882
具體如何使用日誌參照下面代碼:
@RunWith(SpringRunner.class) @SpringBootTest public class HelloWordApplicationTests { Logger logger=LoggerFactory.getLogger(HelloWordApplicationTests.class); @Test public void testLog() { logger.trace("測試Logger_Trace"); logger.debug("測試Logger_debug"); logger.info("測試Logger_Info"); logger.warn("測試Logger_warn"); logger.error("測試logger_error"); }}
可是在測試時發現只能輸出info warn 以及error中的內容,其餘的內容均爲輸出,這是因爲日誌級別的問題
日誌級別有:
trace debug info warn error fail off
其中默認的級別是 info,只打印info之後的,而Info之前的不給予打印
在主配置文件application.properties中設置:
logging.level.com.test.HelloWord=debug 其中com.test.HelloWord是主配置類的包名
這樣的話就把日誌的默認級別也就是最低級別調到debug
在主配置文件application.properties中設置: logging.file=D:/program/springBoot/HelloWord/log/springBoot.Log 這樣的話就能夠把日誌信息加入到上面目錄下的springBoot.Log文件中 固然還有logging.path=D:/ 這樣的話就直接把日誌輸出指定的文件夾中,默認的文件名是spring.log
指定日誌顯示格式:
在主配置文件application.properties中設置: logging.pattern.console=%d{yyyy-MM-dd} [%thread] %-5level %logger{50} -%msg%n
下面解釋一下上面這串代碼的意思:
在主配置文件application.properties中設置: logging.pattern.file=%d{yyyy-MM-dd} ** [%thread] ** %-5level ** %logger{50} ** %msg%n
<dependency> <groupId>org.webjars</groupId> <artifactId>jquery</artifactId> <version>3.3.1-1</version> </dependency>
<!-- https://mvnrepository.com/artifact/org.webjars/bootstrap --> <dependency> <groupId>org.webjars</groupId> <artifactId>bootstrap</artifactId> <version>4.1.3</version> </dependency>
下面簡單的進行對jquery.js進行訪問:
http://localhost:1234/zhiyi/webjars/jquery/3.3.1-1/jquery.js
注意:開始的是從webjars開始的
http://localhost:1234/zhiyi/Hello.html
在任意靜態資源目錄下,只要文件名叫作index.html,它就是主頁,直接訪問便可:
http://localhost:1234/zhiyi
那麼如何進行更改favicon.ico呢
咱們只須要將favicon.ico放在任何靜態資源目錄中便可
如何自定靜態資源位置:
在主配置文件application.properties中設置: spring.resources.static-locations=classpath:/res/
而後在src/main/resources下建立對應的res目錄,這樣的話靜態資源目錄除了默認的resources以及static目錄,res如今也是靜態資源目錄了
一樣訪問的時候也不須要加前綴res,直接輸入文件名便可:
http://localhost:1234/zhiyi/res.html
若是建立多個資源路徑就這樣:
spring.resources.static-locations=classpath:/res/ ,classpath:/img/
注意:一旦自定義了,默認的靜態資源文件夾就會所有失效
springBoot是不支持動態頁面jsp,可是如何來替換jsp頁面呢,那麼就使用咱們的動態模板引擎thymeleaf
那麼到底什麼是模板引擎:
模板引擎就是將一個網友分爲兩部分:
https://docs.spring.io/spring-boot/docs/2.1.0.RELEASE/reference/htmlsingle/#using-boot-starter
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring5</artifactId> </dependency> <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-java8time</artifactId> </dependency>
導入以後咱們來研究第一個問題:引入thymeleaf 代碼應該往哪裏寫
thymeleaf的代碼所有放在構建路徑(src/main/resources/)下的templete目錄下
下面先看一個模板:
templete目錄下的html:
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Thymleaf</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <p th:text="${welcome}">Welcome to thymeleaf!</p> </body> </html>
先看一下上面這個代碼片斷,其餘的都是模板,之後直接拿着用,可是p中有一個th:text="${welcome},會優先顯示welcome中的內容
那麼總結一下:th:就是替換的意思,好比:
<p id="aaa" class="aaa" th:id="${welcome}" th:class="${welcome}" th:text="${welcome}">Welcome to thymeleaf!</p>
那麼若是${welcome}中有值,那麼就就會有優先使用welcome中的值
在寫一個controller:
@Controller public class BootController { @RequestMapping("/wecome") public String wecome(Map<String, Object> map) { map.put("welcome", "welcome_Thymeleaf"); return "result"; }}
th後面到底能夠存放哪些內容呢:
Order | Feature | Attributes |
---|---|---|
1 | Fragment inclusion | th:insert th:replace |
2 | Fragment | iteration |
3 | Conditional evaluation | th:if th:unless th:switch th:case |
4 | Local variable definition | th:object th:with |
5 | General attribute modification | th:attr th:attrprepend th:attrappend |
6 | Specific attribute modification | th:value th:href th:src … |
7 | Text (tag body modification) | th:text th:utext |
8 | Fragment specification | th:fragment |
9 | Fragment removal | th:remove |
下面重點講一下 th:text,th:untext
th:text="<h1>hello</h1>" th:untext="<h1>hello</h1>"
第一個顯示的是大大的標題hello,而第二個顯示的是‘《h1》hello《/h1》’
下面寫一個關於th:each的用法:
html頁面:
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Thymleaf</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <link rel="stylesheet" href="webjars/bootstrap/4.1.3/css/bootstrap.min.css"> <link rel="stylesheet" href="webjars/bootstrap/4.1.3/css/css/bootstrap-theme.min.css"> <script src="webjars/jquery/3.3.1-1/jquery.js"></script> <script src="webjars/bootstrap/4.1.3/css/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <table class="table table-striped table-bordered"> <thead> <tr> <th>姓名</th> <th>年齡</th> </tr> </thead> <tbody> <tr th:each="student : ${Students}"> <td th:text="${student.name}">姓名</td> <td th:text="${student.age}">年齡</td> </tr> </tbody> </table> </div> </body> </html>
對應的controller:
@Controller public class BootController { @RequestMapping("/wecome") public String wecome(Map<String, Object> map) { map.put("welcome", "welcome_Thymeleaf"); List<Student> lStudents = new ArrayList<Student>(); lStudents.add(new Student("zx", 21)); lStudents.add(new Student("zx1", 22)); lStudents.add(new Student("zx3", 23)); lStudents.add(new Student("zx4", 24)); map.put("Students", lStudents); return "result"; }}
對應的Po:
public class Student { private String name; private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Student() { super(); } public Student(String n,Integer a) { super(); this.name=n; this.age=a; }}
springBoot是不支持jsp開發的,由於jsp頁面須要打成war包部署到tomcat,可是springBoot是不用打成war包就能運行,這個時候就須要使用外置的tomcat
下面接着講一個maven的小知識:
接着進入咱們的主題:如何使用外置的tomcat,參考 點擊此處
而後進行下面幾個步驟:
spring.mvc.view.prefix=/ spring.mvc.view.suffix=.jsp
index.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> Hello ${requestScope.name} Welcome to Index.jsp </body> </html>
Controller:
@Controller public class WebController { @RequestMapping("/welcome") public String welcome(Map<String, Object>map) { map.put("name", "朱旭"); return "index"; }}