Spring MVC使用注解实现账户注册与登陆
涉及到的注解
@Controller
控制器类
@RequestMapping
映射路径
@RequestParam
用于将指定的请求参数赋值给方法中的形参
定义域对象——User1
用来接受并封装前台传递的数据
package com.wen.domain;
import java.io.Serializable;
//实现序列化接口
public class User1 implements Serializable {
//私有字段
private String loginName;
private String password;
private String username;
//公共构造器
public User1(){
super();
}
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
用户处理各种请求
package com.wen.controller;
import com.wen.domain.User1;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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;
import java.util.ArrayList;
import java.util.List;
//Controller 注解用于指示该类是一个控制器,可以同时处理多个请求动作
@Controller
//RequestMapping 可以用了注释一个控制器类,此时,所有方法都映射为相对于类级别的请求
//表示该控制器处理的所有请求都被映射到value属性所指示的路径下
@RequestMapping(value = "/user")
public class UserController {
//静态List<User1>集合,此处代替数据库用来保存注册的用户信息
private static List<User1> user1List;
//UserController类的构造器,初始化List<User1>集合
public UserController(){
super();
user1List=new ArrayList<>();
}
//静态的日志类LogFactory
private static final Log logger= LogFactory.getLog(UserController.class);
//该方法映射的请求为http://localhost:8080/.../user/register,该方法支持GET请求
@RequestMapping(value = "/register",method = RequestMethod.GET)
public String registerForm(){
logger.info("register GET方法被调用");
//跳转到注册界面registerForm.jsp
return "registerForm";
}
//该方法映射的请求为http://localhost:8080/.../user/register,该方法支持POST请求
@RequestMapping(value = "/register",method = RequestMethod.POST)
public String register(
//将请求中的参数进行赋值
@RequestParam("loginName") String loginName,
@RequestParam("password") String password,
@RequestParam("userName") String userName){
logger.info("register POST方法被调用");
//创建User对象
User1 user1 =new User1();
user1.setLoginName(loginName);
user1.setPassword(password);
user1.setUsername(userName);
//模仿数据库存储的User信息
user1List.add(user1);
//跳转到登陆界面loginForm.jsp
return "loginForm";
}
//该方法映射的请求为http://localhost:8080/.../user/login
@RequestMapping("/login")
public String login(
//将请求中的参数进行赋值
@RequestParam("loginName") String loginName,
@RequestParam("password") String password,
Model model){
logger.info("登录名:"+loginName+"密码:"+password);
//到集合中查找用户是否存在,此处用来模拟数据库验证
for (User1 user1:user1List){
if (user1.getLoginName().equals(loginName)
&& user1.getPassword().equals(password)){//若一致
model.addAttribute("user1",user1);//将数据添加到request中
return "welcome";//跳转到welcome.jsp
}
}
return "loginForm";//不一致,则跳转到loginFrom.jsp
}
}
registerForm.jsp
这里的表单会被提交到register请求,使用的是POST请求。
响应的是UserController类中的register()方法。
<%--
Created by IntelliJ IDEA.
User: wen
Date: 2019/1/29
Time: 22:03
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>注册页面</title>
</head>
<body>
<form action="register" method="post">
<label for="loginName">登录名:</label><input type="text" id="loginName" name="loginName">
<label for="password">密码:</label><input type="password" id="password" name="password">
<label for="userName">真实姓名:</label><input type="text" id="userName" name="userName">
<input type="submit" id="submit" value="注册">
</form>
</body>
</html>
loginForm.jsp
这里的表单会被提交到login请求,使用的是POST请求。
响应的是UserController类的login方法。
<%--
Created by IntelliJ IDEA.
User: wen
Date: 2019/1/29
Time: 22:07
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登陆页面</title>
</head>
<body>
<form action="login" method="post">
<label for="loginName">登陆名:</label><input type="text" id="loginName" name="loginName">
<label for="password">密码:</label><input type="password" id="password" name="password">
<input type="submit" id="submit" value="登陆">
</form>
</body>
</html>
welcome.jsp
用于表示登陆成功的页面。
此页面接受request中的user1属性中的username属性的值。
<%--
Created by IntelliJ IDEA.
User: wen
Date: 2019/1/29
Time: 22:11
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>欢迎</title>
</head>
<body>
<h3>欢迎【${requestScope.user1.username}】登录</h3>
</body>
</html>
End~