Hello World
一个功能:浏览器发送hello请求,服务器接受请求并处理,响应hello world字符串
第一步:创建一个maven工程
第二步:导入依赖spring boot相关依赖(这里是1.5.9版本)
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
第三步:编写一个主程序 启动Spring Boot应用
/**
* @SpringBootApplication 标注一个主程序类 ,说明这是一个springboot应用
*/
@SpringBootApplication
public class HelloWorldMainApplication {
public static void main(String[] args) {
//spring应用启动起来
SpringApplication.run(HelloWorldMainApplication.class,args);
}
}
第四步:编写业务逻辑(controller,service)
第五步:运行主程序测试
第六步:简化部署
将这个应用打成jar包,直接使用java-jar的命令执行
<!-- 这个插件 , 可以将应用打包成一个可执行的jar包-->
<build>
<plugins>
<plugin>
<artifactId>spring-boot-maven-plugin</artifactId>
<groupId>org.springframework.boot</groupId>
</plugin>
</plugins>
</build>
Hello World探究
1、POM文件
(1)父项目
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
//这是上面父项目的父项目
//真正管理spring boot应用里的所有依赖版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>
(2)启动器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
**spring-boot-starter :场景启动器
web:导入web模块正常运行所依赖的组件
*********spring boot将所有的功能场景都抽取出来,做成一个个的starters(启动器),只需在项目里引入这些starter,相关场景所有的依赖都会导入进来!
2、主程序类,主入口类
/**
* @SpringBootApplication 标注一个主程序类 ,说明这是一个springboot应用
*/
@SpringBootApplication
public class HelloWorldMainApplication {
public static void main(String[] args) {
//spring应用启动起来
SpringApplication.run(HelloWorldMainApplication.class,args);
}
}
@SpringBootApplication :说明是spring boot的主配置类,spring boot运行这个类的main方法来启动spring boot应用
thymeleaf
1、导入thymeleaf的名称空间
<html lang="en" xmlns:th="http://www.thymeleaf.org">
2、使用thtmeleaf语法
<!DOCTYPE html >
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>成功!</h1>
<!-- th:text: 将div里的文本内容设置为-->
<div th:text="${hello}">这里是欢迎信息</div>
</body>
</html>
3、语法规则
1)th:text;改变当前元素里的文本内容
th: 任意html属性 来代替原生属性值----------(例如)这里的th:text中的值会取代“这里是欢迎信息”
2) 表达式
thymeleaf教程