**(一)**了解spring boot
1.安装maven压缩包
2.将下载下来的包解压到解压到D:\Program Files\Maven\apache-maven-3.6.1
3.配置Maven环境配量
创建系统变量
修改Path环境变量
4.通过idea配置本地Maven库,修改Maven主目录
5.设置阿里云镜像库地址
在maven安装目录扎到settings.xml文件打开加入阿里云镜像
<mirror>
<id>alimaven</id>
<mirrorOf>central</mirrorOf>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror>
**(二)**使用Maven方式构建
点击finsh完成
1.在pom.xml文件中添加依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.hw.lesson01</groupId>
<artifactId>HelloWorld01</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
一定要刷新,否则添加依赖失败
2.创建类HelloWorld01Application
2.1在src/main/java目录里创建net.hw.lesson01包
2.2net.hw.lesson01包里创建启动类HelloWorld01Application
src/main/java中创建包的原因是启动类必须放在src/main/java包中appApplication
一定要在包的最外层,否则Spring无法对所有的类进行托管,会造成@Autowired 无法注入。
package net.jyq.lesson01;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HelloWorld01Application {
public static void main(String[] args) {
SpringApplication.run(HelloWorld01Application.class, args);
}
}
3.创建控制器HelloWorld01Controller
package net.jyq.lesson01.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloWorld01Controller {
@GetMapping("/hello")
public String hello() {
return "<h3>Hello Spring Boot World!</h3>";
}
}
4.运行HelloWorld01Application
在浏览器中输入地址http://localhost:8080/hello
5.修改控制器,启动应用
添加了hw01则地址改变了源地址为http://localhost:8080/hello
现在地址为http://localhost:8080/hw01/hello
5.定制Banner
1.在resources目录下创建banner.txt文件
2.通过http://patorjk.com/software/taag网站生成字符
3.启动程序,查看字符
4.关闭程序
5.启动程序,图案消失
**(三)**使用Spring Initializr方式构建Spring Boot项目
1.创建新项目HelloWorld02
2.点击完成生成项目,在pom.xml中添加依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>net.hw.lesson01</groupId>
<artifactId>helloworld02</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>HelloWorld02</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.创建net.jyq.lesson01再去创建一个包controller,在子包中创建HelloWorld02Controller
package net.jyq.lesson1;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloWorld02Controller {
@ResponseBody
@RequestMapping("/hello")
public String hello() {
return "<h3>你好,Spring Boot世界!</h3>";
}
@GetMapping("/index")
public String index(){
return "index";
}
}
4.修改控制器
5.启动控制器,启动应用
在浏览器里访问http://localhost:8080/hello
在浏览器里访问http://localhost:8080/hw02/hello
**(四)**利用控制器返回页面
1.添加thymeleaf依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>net.hw.lesson01</groupId>
<artifactId>helloworld02</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>HelloWorld02</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.准备图片作为静态资源
3.创建首页index.xml
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<img src="/images/xz.jpg" alt="">
<a href="/login">跳转登录</a>
</body>
</html>
4.修改控制器,添加映射,负责返回
启动应用,访问http://localhost:8080/hw02/index
(五)课后作业
任务1.创建项目输出学生信息
创建控制器StudentInfoController
package net.jyq.lesson1;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class StudentInfoController {
@ResponseBody
@RequestMapping("/student")
public String student() {
return "学号:20190059 性别:男"
;
}
@GetMapping("/login")
public String index(){
return "login";
}
}
启动查看,输入http://localhost:8080/student
任务2.让HelloWorld02实现页面跳转功能
1.创建login.xml登录界面
<html>
<head>
<title>用户登录</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" >
</head>
<body>
<h3 style="text-align: center">用户登录</h3>
<form id="frmLogin" action="login" method="post">
<table class="tb" border="1" cellpadding="10" style="margin: 0px auto">
<tr>
<td align="center">账号</td>
<td><input id="username" type="text" name="username"/></td>
</tr>
<tr>
<td align="center">密码</td>
<td><input id="password" type="password" name="password"/></td>
</tr>
<tr align="center">
<td colspan="2">
<input type="submit" value="登录"/>
<input type="reset" value="重置"/>
</td>
</tr>
</table>
</form>
</body>
</html>
2.在index添加登录方法,写入
3.在控制器中添加跳转页面,运行程序在浏览器输入http://localhost:8080/login
点解登录,跳转到用户登录