使用Java编写一个简单的具有登录功能的web项目
时间: 2023-05-26 20:06:30 浏览: 78
我们可以使用Spring Boot框架来编写一个简单的具有登录功能的web项目。项目结构如下:
```
- src
- main
- java
- com.example.demo
- controller
- LoginController.java
- UserController.java
- model
- User.java
- repository
- UserRepository.java
- service
- UserService.java
- App.java
- resources
- application.properties
- templates
- login.html
- user.html
- test
- java
- com.example.demo
- ApplicationTests.java
- pom.xml
```
1. 建立一个Spring Boot应用程序
在pom.xml文件中添加以下依赖项:
```xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
</dependencies>
```
2. 创建实体类
在model包下创建User.java实体类:
```java
package com.example.demo.model;
import javax.persistence.*;
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String username;
private String password;
public User() {}
public User(String username, String password) {
this.username = username;
this.password = password;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
```
3. 创建Repository
在repository包中创建一个UserRepository.java接口,并继承JpaRepository:
```java
package com.example.demo.repository;
import com.example.demo.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<User, Long> {
User findByUsername(String username);
}
```
4. 创建Service
在service包中创建UserService.java类:
```java
package com.example.demo.service;
import com.example.demo.model.User;
import com.example.demo.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public User getUserByUsername(String username) {
return userRepository.findByUsername(username);
}
}
```
5. 创建Controller
在controller包中创建LoginController.java控制器,实现登录功能:
```java
package com.example.demo.controller;
import com.example.demo.model.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
@Controller
public class LoginController {
@Autowired
private UserService userService;
@GetMapping("/")
public String login(Model model) {
model.addAttribute("user", new User());
return "login";
}
@PostMapping("/login")
public String login(@ModelAttribute User user, Model model) {
User u = userService.getUserByUsername(user.getUsername());
if (u == null || !u.getPassword().equals(user.getPassword())) {
model.addAttribute("errorMsg", "Invalid username or password");
return "login";
}
return "redirect:/user/" + u.getId();
}
}
```
controller包下创建UserController.java控制器,实现用户页面:
```java
package com.example.demo.controller;
import com.example.demo.model.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
@Controller
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/user/{id}")
public String user(@PathVariable Long id, Model model) {
User user = userService.getUserById(id);
model.addAttribute("user", user);
return "user";
}
}
```
6. 创建HTML页面
在templates包中创建login.html页面:
```html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Login Page</title>
<meta charset="UTF-8">
<style type="text/css">
#login-form {
border: 2px solid #eee;
padding: 10px;
margin: 50px auto;
max-width: 350px;
}
label {
display: block;
margin-bottom: 5px;
}
input[type="text"],input[type="password"] {
display: block;
width: 100%;
margin-bottom: 20px;
padding: 5px;
}
input[type="submit"] {
background-color: #4CAF50;
color: white;
padding: 10px 20px;
border: none;
cursor: pointer;
width: 100%;
}
input[type="submit"]:hover {
background-color: #45a049;
}
.form-error {
color: red;
margin-top: 10px;
}
</style>
</head>
<body>
<div id="login-form">
<h2>Login Form</h2>
<form method="post" th:object="${user}" th:action="@{/login}">
<label for="username">Username:</label>
<input type="text" id="username" name="username" th:field="*{username}"/>
<label for="password">Password:</label>
<input type="password" id="password" name="password" th:field="*{password}"/>
<input type="submit" value="Login"/>
<div th:if="${errorMsg}">
<p class="form-error" th:text="${errorMsg}"></p>
</div>
</form>
</div>
</body>
</html>
```
在templates包中创建user.html页面:
```html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>User Page</title>
<meta charset="UTF-8">
</head>
<body>
<h1>Hello, <span th:text="${user.username}"></span>!</h1>
</body>
</html>
```
7. 运行程序
在App.java文件中添加@SpringBootApplication注解,并通过运行该文件来启动应用程序。
打开http://localhost:8080/可以看到登录页面。
输入正确的用户名和密码,会跳转到用户页面,可以看到用户的欢迎消息。
到此为止,我们已经用Java编写了一个简单的具有登录功能的web项目。