该文章主要为完成实训任务,详细实现过程及结果见【http://t.csdn.cn/Qh554】
<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.0modelVersion>
<groupId>cn.kox.bootgroupId>
<artifactId>HelloWorld01artifactId>
<version>1.0-SNAPSHOTversion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.7.11version>
parent>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
dependencies>
<properties>
<maven.compiler.source>17maven.compiler.source>
<maven.compiler.target>17maven.compiler.target>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
properties>
project>
cn.kox.boot
包,在包里创建启动类HelloWorld01Application
package cn.kox.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @ClassName: HelloWorld01Application
* @Author: Kox
* @Data: 2023/5/24
* @Sketch: 项目入口类
*/
@SpringBootApplication
public class HelloWorld01Application {
public static void main(String[] args) {
// 参数1:入口类示例;参数2:命令行参数
SpringApplication.run(HelloWorld01Application.class, args);
}
}
package cn.kox.boot.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @ClassName: HelloController
* @Author: Kox
* @Data: 2023/5/24
* @Sketch:
*/
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "Hello Spring Boot World~
";
}
}
package cn.kox.boot;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @ClassName: HelloWorld01Application
* @Author: Kox
* @Data: 2023/5/24
* @Sketch: 项目入口类
*/
@SpringBootApplication
public class HelloWorld01Application {
public static void main(String[] args) {
// 创建Spring应用
SpringApplication app = new SpringApplication(HelloWorld01Application.class);
// 设置标语模式 - 关闭
app.setBannerMode(Banner.Mode.OFF);
// 运行Spring应用
app.run(args);
}
}
pom.xm
文件
<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.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>3.1.0version>
<relativePath/>
parent>
<groupId>cn.kox.bootgroupId>
<artifactId>helloworld02artifactId>
<version>0.0.1-SNAPSHOTversion>
<name>HelloWorld02name>
<description>HelloWorld02description>
<properties>
<java.version>17java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
package cn.kox.boot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @ClassName: HelloController
* @Author: Kox
* @Data: 2023/5/24
* @Sketch: Hello控制器
*/
@Controller
public class HelloController {
@ResponseBody
@GetMapping("/hello")
public String hello() {
return "你好,Spring Boot世界~
";
}
}
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-thymeleafartifactId>
dependency>
body{
background-color: red;
text-align: center;
}
DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页title>
<link href="/css/index.css" rel="stylesheet", type="text/css">
head>
<body>
<h1>Welcome to Spring Boot World~h1>
<h3 th:text="${#dates.format(today,'yyyy年MM月dd日 HH:mm:ss')}">2023-05-10h3>
<img src = "/images/img.png" width="300" height="250">
body>
html>
package cn.kox.boot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Calendar;
/**
* @ClassName: HelloController
* @Author: Kox
* @Data: 2023/5/24
* @Sketch: Hello控制器
*/
@Controller
@RequestMapping("/kox")
public class HelloController {
@ResponseBody
@GetMapping("/hello")
public String hello() {
return "你好,Spring Boot世界~
";
}
@GetMapping("/index")
public String index(Model model) {
model.addAttribute("today", Calendar.getInstance());
return "index"; // 返回l逻辑视图名
}
t
}
DOCTYPE html>
<html>
<head>
<title>User Logintitle>
<style>
body {
font-family: Arial, sans-serif;
}
.container {
width: 300px;
margin: 0 auto;
padding: 20px;
border: 1px solid #ccc;
border-radius: 5px;
}
.container h2 {
text-align: center;
}
.container label {
display: block;
margin-bottom: 5px;
}
.container input[type="text"],
.container input[type="password"] {
width: 100%;
padding: 8px;
border: 1px solid #ccc;
border-radius: 3px;
margin-bottom: 10px;
}
.container input[type="submit"] {
width: 100%;
background-color: #4CAF50;
color: white;
padding: 10px;
border: none;
border-radius: 3px;
cursor: pointer;
}
.container input[type="submit"]:hover {
background-color: #45a049;
}
style>
head>
<body>
<div class="container">
<h2>User Loginh2>
<form>
<label for="username">Username:label>
<input type="text" id="username" name="username" required>
<label for="password">Password:label>
<input type="password" id="password" name="password" required>
<input type="submit" value="Login">
form>
div>
body>
html>