1.使用SpringMVC框架完成模拟注册和登录。(不需要写入数据库) 2.注册视图:包括用户名(文本框),密码(密码框),个人头像(图片文件),省份(下来菜单),爱好(复选框) 3.注册成功视图:注册成功后,在注册成功视图显示所有注册信息 4.其他功能包括:实现用户名、密码非空校验;实现用户名和密码不能小于6位的长度校验。 5.使用SpringMVC的拦截器对用户权限进行拦截。 6.提交项目源码
时间: 2024-03-14 18:44:22 浏览: 113
springMVC实现用户注册及登陆
很高兴为您解答这个问题。以下是一个简单的SpringMVC项目,用于模拟注册和登录:
首先,我们需要创建一个Maven项目,并添加以下依赖项:
```xml
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.9</version>
</dependency>
<!-- JSTL -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- Servlet API -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<!-- JSP API -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
</dependency>
<!-- Javax Validation API -->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
<!-- Hibernate Validator Engine -->
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.1.5.Final</version>
</dependency>
</dependencies>
```
然后,我们需要创建一个UserController类,用于处理所有与用户相关的请求。以下是UserController的代码:
```java
@Controller
@RequestMapping("/")
public class UserController {
@Autowired
private Validator validator;
@GetMapping("/")
public String index() {
return "index";
}
@GetMapping("/register")
public String registerForm(Model model) {
model.addAttribute("user", new User());
model.addAttribute("provinces", Arrays.asList("北京", "上海", "广州", "深圳", "杭州"));
model.addAttribute("hobbies", Arrays.asList("阅读", "游戏", "旅游", "健身", "音乐"));
return "register";
}
@PostMapping("/register")
public String registerSubmit(@ModelAttribute("user") @Valid User user, BindingResult result, Model model) {
if (result.hasErrors()) {
model.addAttribute("provinces", Arrays.asList("北京", "上海", "广州", "深圳", "杭州"));
model.addAttribute("hobbies", Arrays.asList("阅读", "游戏", "旅游", "健身", "音乐"));
return "register";
} else {
model.addAttribute("user", user);
return "registersuccess";
}
}
@GetMapping("/login")
public String loginForm() {
return "login";
}
@PostMapping("/login")
public String loginSubmit(@RequestParam("username") String username, @RequestParam("password") String password, Model model) {
if (username.equals("admin") && password.equals("password")) {
model.addAttribute("username", username);
return "loginsuccess";
} else {
model.addAttribute("error", "用户名或密码错误!");
return "login";
}
}
@ModelAttribute("user")
public User getUser() {
return new User();
}
@InitBinder
private void initBinder(WebDataBinder binder) {
binder.setValidator(validator);
}
}
```
UserController类中定义了四个请求处理方法:index()、registerForm()、registerSubmit()和loginSubmit()。index()方法返回首页,registerForm()方法返回注册表单视图,registerSubmit()方法处理提交的注册表单数据,loginSubmit()方法处理登录表单数据。
UserController类中还定义了一个getUser()方法,用于在registerForm()方法中创建一个空的User对象,并将其绑定到表单中,以便用户填写数据。
最后,我们需要创建一个User类,用于存储用户信息。以下是User类的代码:
```java
public class User {
@NotEmpty(message = "用户名不能为空!")
@Size(min = 6, message = "用户名长度不能小于6位!")
private String username;
@NotEmpty(message = "密码不能为空!")
@Size(min = 6, message = "密码长度不能小于6位!")
private String password;
private String province;
private List<String> hobbies;
// getters and setters
}
```
User类中定义了用户名、密码、省份和爱好等属性,并使用了JSR-303注解对它们进行了校验。
最后,我们需要创建视图模板,用于呈现注册表单、注册成功信息、登录表单和登录成功信息。以下是四个视图模板的代码:
index.jsp:
```html
<!DOCTYPE html>
<html>
<head>
<title>Spring MVC Login & Registration Example</title>
</head>
<body>
<h1>Welcome</h1>
<ul>
<li><a href="register">Register</a></li>
<li><a href="login">Login</a></li>
</ul>
</body>
</html>
```
register.jsp:
```html
<!DOCTYPE html>
<html>
<head>
<title>Registration Form</title>
</head>
<body>
<h1>Registration Form</h1>
<form:form method="post" modelAttribute="user" enctype="multipart/form-data">
<table>
<tr>
<td>Username:</td>
<td><form:input path="username" /></td>
<td><form:errors path="username" cssClass="error" /></td>
</tr>
<tr>
<td>Password:</td>
<td><form:password path="password" /></td>
<td><form:errors path="password" cssClass="error" /></td>
</tr>
<tr>
<td>Avatar:</td>
<td><form:input path="avatar" type="file" /></td>
<td></td>
</tr>
<tr>
<td>Province:</td>
<td><form:select path="province">
<form:option value="">--请选择--</form:option>
<form:options items="${provinces}" />
</form:select></td>
<td></td>
</tr>
<tr>
<td>Hobbies:</td>
<td><form:checkboxes path="hobbies" items="${hobbies}" /></td>
<td></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Submit" /></td>
<td></td>
</tr>
</table>
</form:form>
</body>
</html>
```
registersuccess.jsp:
```html
<!DOCTYPE html>
<html>
<head>
<title>Registration Success</title>
</head>
<body>
<h1>Registration Success</h1>
<p>Username: ${user.username}</p>
<p>Password: ${user.password}</p>
<p>Province: ${user.province}</p>
<p>Hobbies: ${user.hobbies}</p>
</body>
</html>
```
login.jsp:
```html
<!DOCTYPE html>
<html>
<head>
<title>Login Form</title>
</head>
<body>
<h1>Login Form</h1>
<c:if test="${not empty error}">
<p class="error">${error}</p>
</c:if>
<form method="post" action="${pageContext.request.contextPath}/login">
<table>
<tr>
<td>Username:</td>
<td><input type="text" name="username" /></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password" /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Login" /></td>
</tr>
</table>
</form>
</body>
</html>
```
loginsuccess.jsp:
```html
<!DOCTYPE html>
<html>
<head>
<title>Login Success</title>
</head>
<body>
<h1>Login Success</h1>
<p>Welcome, ${username}!</p>
</body>
</html>
```
最后,我们需要创建一个Spring配置文件,用于配置SpringMVC框架。以下是配置文件的代码:
```xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-5.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-5.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-5.3.xsd">
<context:component-scan base-package="com.example.controller" />
<mvc:annotation-driven />
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
</beans>
```
现在,我们可以运行这个SpringMVC项目,并在浏览器中访问http://localhost:8080/来查看登录和注册页面。
阅读全文