当前位置:实例文章 » JAVA Web实例» [文章]SpringBoot登陆+6套主页-【JSB项目实战】

SpringBoot登陆+6套主页-【JSB项目实战】

发布人:shili8 发布时间:2025-03-04 13:19 阅读次数:0

**SpringBoot 登录 +6 套主页 - JSB项目实战**

在本文中,我们将一步步地讲解如何使用 SpringBoot 构建一个完整的登录系统,并且为其添加六套不同的主页样式。我们将使用 SpringBoot2.3.4.RELEASE 版本,Spring Security5.3.4.RELEASE 版本。

### 一、项目结构首先,我们需要创建一个新的 SpringBoot项目。在这里,我们使用 Spring Initializr 来快速创建一个新项目。

xml<dependencies>
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-web</artifactId>
 </dependency>
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-security</artifactId>
 </dependency>
 <dependency>
 <groupId>mysql</groupId>
 <artifactId>mysql-connector-java</artifactId>
 <scope>runtime</scope>
 </dependency>
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-test</artifactId>
 <scope>test</scope>
 </dependency>
</dependencies>


### 二、数据库配置我们需要在 `application.properties` 文件中配置我们的数据库连接信息。

propertiesspring.datasource.url=jdbc:mysql://localhost:3306/jsbspring.datasource.username=rootspring.datasource.password=123456spring.jpa.hibernate.ddl-auto=update


### 三、用户表和登录功能我们需要创建一个 `User` 表来存储我们的用户信息。同时,我们也需要实现登录功能。

java// User.java@Entitypublic class User {
 @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
 private Long id;
 private String username;
 private String password;

 // getter and setter}


java// LoginController.java@RestController@RequestMapping("/api")
public class LoginController {
 @Autowired private UserService userService;

 @PostMapping("/login")
 public Result login(@RequestBody User user) {
 User dbUser = userService.getUserByUsername(user.getUsername());
 if (dbUser != null && dbUser.getPassword().equals(user.getPassword())) {
 return Result.success("登录成功");
 } else {
 return Result.fail("登录失败");
 }
 }
}


### 四、主页样式我们需要为我们的登录系统添加六套不同的主页样式。这里,我们使用 Thymeleaf 来实现。

java// IndexController.java@Controller@RequestMapping("/")
public class IndexController {
 @GetMapping public String index(Model model) {
 return "index";
 }
}


html<!-- index.html -->
<!DOCTYPE html>
<html xmlns:th=" /><head>
 <title>JSB</title>
 <link rel="stylesheet" href=" /> integrity="sha384-TX8t27EcRE3e/K5tfP+hhuMJgMrmAsGeCgvfGA+fQg7eKcCsGSVR5Jfp9yCbdDU+"
 crossorigin="anonymous">
</head>
<body>
 <div class="container">
 <h1>JSB</h1>
 <p>Welcome to JSB!</p>
 </div>
</body>
</html>


java// IndexController.java (continued)
@GetMapping("/index2")
public String index2(Model model) {
 return "index2";
}


html<!-- index2.html -->
<!DOCTYPE html>
<html xmlns:th=" /><head>
 <title>JSB</title>
 <link rel="stylesheet" href=" /> integrity="sha384-TX8t27EcRE3e/K5tfP+hhuMJgMrmAsGeCgvfGA+fQg7eKcCsGSVR5Jfp9yCbdDU+"
 crossorigin="anonymous">
</head>
<body>
 <div class="container">
 <h1>JSB</h1>
 <p>This is index2!</p>
 </div>
</body>
</html>


java// IndexController.java (continued)
@GetMapping("/index3")
public String index3(Model model) {
 return "index3";
}


html<!-- index3.html -->
<!DOCTYPE html>
<html xmlns:th=" /><head>
 <title>JSB</title>
 <link rel="stylesheet" href=" /> integrity="sha384-TX8t27EcRE3e/K5tfP+hhuMJgMrmAsGeCgvfGA+fQg7eKcCsGSVR5Jfp9yCbdDU+"
 crossorigin="anonymous">
</head>
<body>
 <div class="container">
 <h1>JSB</h1>
 <p>This is index3!</p>
 </div>
</body>
</html>


java// IndexController.java (continued)
@GetMapping("/index4")
public String index4(Model model) {
 return "index4";
}


html<!-- index4.html -->
<!DOCTYPE html>
<html xmlns:th=" /><head>
 <title>JSB</title>
 <link rel="stylesheet" href=" /> integrity="sha384-TX8t27EcRE3e/K5tfP+hhuMJgMrmAsGeCgvfGA+fQg7eKcCsGSVR5Jfp9yCbdDU+"
 crossorigin="anonymous">
</head>
<body>
 <div class="container">
 <h1>JSB</h1>
 <p>This is index4!</p>
 </div>
</body>
</html>


java// IndexController.java (continued)
@GetMapping("/index5")
public String index5(Model model) {
 return "index5";
}


html<!-- index5.html -->
<!DOCTYPE html>
<html xmlns:th=" /><head>
 <title>JSB</title>
 <link rel="stylesheet" href=" /> integrity="sha384-TX8t27EcRE3e/K5tfP+hhuMJgMrmAsGeCgvfGA+fQg7eKcCsGSVR5Jfp9yCbdDU+"
 crossorigin="anonymous">
</head>
<body>
 <div class="container">
 <h1>JSB</h1>
 <p>This is index5!</p>
 </div>
</body>
</html>


java// IndexController.java (continued)
@GetMapping("/index6")
public String index6(Model model) {
 return "index6";
}


html<!-- index6.html -->
<!DOCTYPE html>
<html xmlns:th=" /><head>
 <title>JSB</title>
 <link rel="stylesheet" href=" /> integrity="sha384-TX8t27EcRE3e/K5tfP+hhuMJgMrmAsGeCgvfGA+fQg7eKcCsGSVR5Jfp9yCbdDU+"
 crossorigin="anonymous">
</head>
<body>
 <div class="container">
 <h1>JSB</h1>
 <p>This is index6!</p>
 </div>
</body>
</html>


### 五、总结在本文中,我们一步步地讲解了如何使用 SpringBoot 构建一个完整的登录系统,并且为其添加六套不同的主页样式。我们使用 SpringBoot2.3.4.RELEASE 版本,Spring Security5.3.4.RELEASE 版本。

### 六、参考* [Spring Boot]( />* [Spring Security](

其他信息

其他资源

Top