1. 程式人生 > >Spring MVC使用註解實現賬戶註冊與登陸

Spring MVC使用註解實現賬戶註冊與登陸

.post etl 對象 ble har 構造 get方法 port lis

涉及到的註解

@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~

Spring MVC使用註解實現賬戶註冊與登陸