使用SpringMVC设计一个注册和登录功能,自定义一个登陆验证的控制器,实现正确登陆跳转至欢迎页面,错误登陆跳转至注册页面的基本功能。
时间: 2024-01-21 11:18:56 浏览: 68
Spring+SpringMVC实现登录功能
首先,我们需要在Spring MVC中配置控制器和视图解析器。在web.xml中添加Spring MVC的DispatcherServlet和上下文配置文件的路径:
```xml
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-context.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
```
在spring-context.xml中配置控制器和视图解析器:
```xml
<context:component-scan base-package="com.example.controller" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
```
接下来,创建一个UserController类,处理用户注册和登录的请求:
```java
@Controller
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value = "/register", method = RequestMethod.GET)
public String showRegistrationForm(Model model) {
model.addAttribute("user", new User());
return "registration";
}
@RequestMapping(value = "/register", method = RequestMethod.POST)
public String processRegistrationForm(@ModelAttribute("user") User user, BindingResult result) {
if (result.hasErrors()) {
return "registration";
}
userService.save(user);
return "redirect:/welcome";
}
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String showLoginForm(Model model) {
model.addAttribute("user", new User());
return "login";
}
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String processLoginForm(@ModelAttribute("user") User user, BindingResult result) {
User registeredUser = userService.findByUsername(user.getUsername());
if (registeredUser == null || !registeredUser.getPassword().equals(user.getPassword())) {
result.rejectValue("username", "error.user", "Invalid username or password");
return "login";
}
return "redirect:/welcome";
}
@RequestMapping(value = "/welcome", method = RequestMethod.GET)
public String showWelcomePage() {
return "welcome";
}
}
```
UserController类使用了@Autowired注解来自动注入UserService,处理注册和登录的请求,并将结果返回到对应的视图。在这里,我们自定义了登录验证的逻辑,如果用户输入的用户名和密码与数据库中的不匹配,就返回登录页面并显示错误消息。
最后,创建注册和登录页面的JSP视图,放在WEB-INF/views目录下:
registration.jsp
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Registration Form</title>
</head>
<body>
<h2>Registration Form</h2>
<form:form method="POST" modelAttribute="user">
<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>Confirm Password:</td>
<td><form:password path="confirmPassword" /></td>
<td><form:errors path="confirmPassword" cssClass="error" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Register" /></td>
</tr>
</table>
</form:form>
</body>
</html>
```
login.jsp
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login Form</title>
</head>
<body>
<h2>Login Form</h2>
<form:form method="POST" modelAttribute="user">
<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 colspan="2"><input type="submit" value="Login" /></td>
</tr>
</table>
</form:form>
</body>
</html>
```
welcome.jsp
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Welcome Page</title>
</head>
<body>
<h2>Welcome!</h2>
</body>
</html>
```
以上就是一个简单的注册和登录功能的实现。
阅读全文