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"; }}