SpringMVC 实现注册登录
记录一下SpringMVC实现注册登录功能这个小程序。
一、新建动态Dynamic Web Project:
- 新建Dynamic Web Project,项目名为LoginSpringMVC
- 导入相应的JAR包:
- 项目整体结构:
二、配置web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
id="WebApp_ID" version="4.0">
<display-name>LoginSpringMVC</display-name>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--字符编码过滤器 -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<!-- 是否强制设在request编码 -->
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
三、创建jsp文件:
loginFrom.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录页面</title>
</head>
<body>
<h3>登录页面</h3>
<br>
<form action="login" method="post">
<table>
<tr>
<td><label>登录名:</label></td>
<td><input type="text" id="loginname" name="loginname"></td>
</tr>
<tr>
<td><label>密码:</label></td>
<td><input type="password" id="password" name="password"></td>
</tr>
<tr>
<td><input id="submit" type="submit" value="登录"></td>
</tr>
</table>
</form>
</body>
</html>
registerFrom.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册页面</title>
</head>
<body>
<h3>注册页面</h3>
<br>
<form action="register" method="post">
<table>
<tr>
<td><label>登录名:</label></td>
<td><input type="text" id="loginname" name="loginname"></td>
</tr>
<tr>
<td><label>密码:</label></td>
<td><input type="password" id="password" name="password"></td>
</tr>
<tr>
<td><label>真实姓名:</label></td>
<td><input type="text" id="username" name="username"></td>
</tr>
<tr>
<td><input id="submit" type="submit" value="注册"></td>
</tr>
</table>
</form>
</body>
</html>
welcome.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登陆成功页面</title>
</head>
<body>
<h3>欢迎${requestScope.user.username }登录</h3>
<br>
</body>
</html>
四、编写java文件:
User.java :
package com.smk.domain;
public class User {
private String loginname;
private String password;
private String username;
public String getLoginname() {
return loginname;
}
public void setLoginname(String loginname) {
this.loginname = loginname;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
UserController.java:
import java.util.ArrayList;
import java.util.List;
import com.smk.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
@RequestMapping(value = "/user")
public class UserController {
// 静态List<User>集合,此处代替数据库用来保存注册的用户信息
private static List<User> userList;
// UserController类的构造器,初始化List<User>集合
public UserController() {
super();
userList = new ArrayList<User>();
}
@RequestMapping(value = "/register", method = RequestMethod.GET)
public String registerForm() {
return "registerForm";
}
@RequestMapping(value = "/register", method = RequestMethod.POST)
public String register(@RequestParam("loginname") String loginname,
@RequestParam("password") String password,
@RequestParam("username") String username) {
// 创建user对象
User user = new User();
user.setLoginname(loginname);
user.setPassword(password);
user.setUsername(username);
// 模拟数据库存储User信息
userList.add(user);
return "loginForm";
}
// 登录
@RequestMapping("/login")
public String login(@RequestParam("loginname") String loginname,
@RequestParam("password") String password,
Model model) {
// 到集合中查找用户是否存在,此处用来模拟数据库验证
for (User user : userList) {
if (user.getLoginname().equals(loginname) && user.getPassword().equals(password)) {
model.addAttribute("user", user);
return "welcome";
}
}
return "loginForm";
}
}
五、配置springmvc-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!--默认的注解映射的支持 -->
<mvc:annotation-driven />
<!--启用自动扫描 -->
<context:component-scan base-package="com.smk.controller" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/content/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
六、测试:
访问 http://localhost:8080/LoginSpringMVC/user/register
输入注册数据会自动跳转到 Login 页面
完成登录: