spirngmvc登录注册
时间: 2023-08-29 08:05:43 浏览: 85
Spring MVC是一种基于MVC(Model-View-Controller)模式的Web框架,可以用于开发Web应用程序。下面是一个简单的Spring MVC登录注册示例。
1. 创建一个Maven项目,添加Spring MVC的依赖。
```xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
```
2. 创建一个控制器(Controller)类,处理请求和响应。
```java
@Controller
public class UserController {
@RequestMapping("/login")
public String login() {
return "login";
}
@RequestMapping("/register")
public String register() {
return "register";
}
@RequestMapping("/doLogin")
public String doLogin(@RequestParam String username, @RequestParam String password, Model model) {
// 处理登录逻辑
if (isValid(username, password)) {
model.addAttribute("username", username);
return "welcome";
} else {
model.addAttribute("error", "Invalid username or password");
return "login";
}
}
@RequestMapping("/doRegister")
public String doRegister(@RequestParam String username, @RequestParam String password, Model model) {
// 处理注册逻辑
if (isValid(username, password)) {
model.addAttribute("username", username);
return "welcome";
} else {
model.addAttribute("error", "Invalid username or password");
return "register";
}
}
private boolean isValid(String username, String password) {
// 验证用户名和密码是否正确
return true;
}
}
```
3. 创建视图(View)文件,显示HTML页面。
- login.html
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<h1>Login</h1>
<form action="/doLogin" method="POST">
<div>
<label for="username">Username:</label>
<input type="text" id="username" name="username">
</div>
<div>
<label for="password">Password:</label>
<input type="password" id="password" name="password">
</div>
<button type="submit">Login</button>
</form>
<p style="color:red">${error}</p>
</body>
</html>
```
- register.html
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Register</title>
</head>
<body>
<h1>Register</h1>
<form action="/doRegister" method="POST">
<div>
<label for="username">Username:</label>
<input type="text" id="username" name="username">
</div>
<div>
<label for="password">Password:</label>
<input type="password" id="password" name="password">
</div>
<button type="submit">Register</button>
</form>
<p style="color:red">${error}</p>
</body>
</html>
```
- welcome.html
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Welcome</title>
</head>
<body>
<h1>Welcome, ${username}!</h1>
</body>
</html>
```
4. 配置Spring MVC,指定视图解析器(ViewResolver)和静态资源路径。
```xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-5.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-5.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-5.2.xsd">
<context:component-scan base-package="com.example.controller" />
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".html" />
</bean>
<mvc:resources mapping="/static/**" location="/static/" />
</beans>
```
5. 运行Tomcat服务器,访问http://localhost:8080/login即可进入登录页面。
阅读全文