保持登錄狀態(tài)的注冊登陸界面

正文之前

前兩天寫了SSM 重構(gòu)注冊登陸界面,然后添加了一點(diǎn)功能:保持登錄狀態(tài),修改當(dāng)前登錄用戶信息,查看登錄狀態(tài)

正文

Version 0.2.1

新增的功能都是在原有的基礎(chǔ)上添加代碼,基本沒有刪去版本 0.1 的代碼

可通過點(diǎn)擊 tag 來查看 v0.1:

1. 新增功能截圖

登錄成功后主頁面:

修改用戶信息(用戶名無法修改):

電話號碼和郵箱不能為空:

修改成功:

檢測登錄狀態(tài):

若未登錄就檢測登錄狀態(tài):

2. 實(shí)體類

用戶的信息添加了電話號碼和描述,所以要在實(shí)體類和數(shù)據(jù)庫中都做一點(diǎn)改動:

    private String gender;
    private String description;
    public void setGender(String gender) {
        this.gender = gender;
    }
    public String getGender() {
        return gender;
    }

    public void setDescription(String description) {
        this.description = description;
    }
    public String getDescription() {
        return description;
    }

MySQL 中對表做一點(diǎn)改動:

alter table user add gender varchar(10), add description varchar(200);

3. Mybatis

為了添加新功能,需要在 MyBatis 映射器中添加兩步:

  • 查找用戶信息:
    <select id="showInfo" parameterType="String" resultType="domain.User">
        SELECT *
        FROM user
        WHERE username = #{username}
    </select>
  • 修改用戶信息:
    <update id="setUserInfo" parameterType="domain.User">
        UPDATE user SET phone = #{phone}, email = #{email},
        gender = #{gender}, description = #{description}
    </update>

然后在接口中添加對應(yīng)方法:

    User showInfo(String username);
    void setUserInfo(User user);

3. Service

先寫異常的接口 ExceptionService 吧:

    void setInfoException(User user) throws UserException;

    void statusException(String username) throws UserException;

然后寫實(shí)現(xiàn)類:

    //重置信息檢測,關(guān)鍵信息不能為空
    @Override
    public void setInfoException(User user) throws UserException {
        if (user.getPhone() == null || user.getPhone().trim().isEmpty()){
            throw new UserException("電話號碼不能為空");
        } else if (user.getEmail() == null || user.getEmail().trim().isEmpty()){
            throw new UserException("郵箱不能為空");
        }
    }

    //用戶狀態(tài)檢測,如果在 Session 中未找到有用戶登陸,就拋出異常
    @Override
    public void statusException(String username) throws UserException {
        if (username == null){
            throw new UserException("請先登錄");
        }
    }

在 UserService 中新增三個(gè)功能,其中有兩個(gè)有異常檢測:

    User showInfo(String username);
    String getStatus(String username) throws UserException;
    void setUserInfo(User user) throws UserException;

然后是實(shí)現(xiàn)類:

    //顯示用戶信息
    @Override
    public User showInfo(String username) {
        return userMapper.showInfo(username);
    }

    //顯示當(dāng)前登錄狀態(tài)
    @Override
    public String getStatus(String username) throws UserException {
        exceptionService.statusException(username);
        return username;
    }

    //修改用戶信息
    @Override
    public void setUserInfo(User user) throws UserException{
        exceptionService.setInfoException(user);
        userMapper.setUserInfo(user);
    }

4. Controller

在登錄時(shí),需要添加一點(diǎn)代碼,將用戶名寫入 Session,以保持登錄狀態(tài):

    public ModelAndView login(User user, HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("main");
        try {
            userService.login(user);
            userService.verifyCode(request.getParameter("verifyCode"), verifyCode.getText());
            //創(chuàng)建 Session,保持登錄狀態(tài)
            request.getSession().setAttribute("username", user.getUsername());
            //在模型中添加對象,用于 JSP 讀取
            modelAndView.addObject("username", request.getSession().getAttribute("username"));
        } catch (UserException e){
            //如果未登錄成功,就重新登錄
            modelAndView.setViewName("login");
            modelAndView.addObject("message", e.getMessage());
        }
        return modelAndView;

然后是登出的操作:

    //登出賬戶,不需要具體用戶名稱,直接廢除 session 就行
    @RequestMapping("/logout")
    public ModelAndView logout(HttpServletRequest request){
        request.getSession().invalidate();
        return new ModelAndView("login").addObject("message", "已登出");
    }

新增查看用戶狀態(tài)的功能:

    //查看用戶狀態(tài),顯示是哪個(gè)用戶在登錄,如果沒有登錄的用戶,就會提示你先登錄
    @RequestMapping("/userStatus")
    public ModelAndView userState(HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView("userStatus");
        try {
            modelAndView.addObject("username",
                    userService.getStatus((String)request.getSession().getAttribute("username")));
        } catch (UserException e){
            modelAndView.addObject("message", e.getMessage());
        }
        return modelAndView;
    }

然后是用戶信息相關(guān)的操作,如果在修改信息時(shí)拋出異常,就帶著錯(cuò)誤信息回到信息修改頁面:

    //顯示用戶信息
    @RequestMapping("showInfo")
    public ModelAndView showInfo(HttpServletRequest request){
        return new ModelAndView("userInfo")
                .addObject("user", userService.showInfo(
                        ((String)request.getSession().getAttribute("username"))));
    }

    //對用戶信息進(jìn)行修改
    @RequestMapping("setUserInfo")
    public ModelAndView setUserInfo(User user){
        ModelAndView modelAndView = new ModelAndView("userInfo");
        try {
            userService.setUserInfo(user);
            //設(shè)置提示信息
            modelAndView.addObject("message", "修改成功");
            //跳轉(zhuǎn)
            modelAndView.setViewName("main");
        } catch (UserException e){
            modelAndView.addObject("message", e.getMessage());
        }
        return modelAndView;
    }

5. JSP

新增兩個(gè)前端頁面:userInfo.jsp 和 userStatus.jsp,都是差不多的頁面,這里就不演示了

接下來的 v0.2.2 是采用過濾器來實(shí)現(xiàn)相同功能

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容