前兩天寫了SSM 重構註冊登錄界面,而後添加了一點功能:保持登陸狀態,修改當前登陸用戶信息,查看登陸狀態前端
可經過點擊 tag 來查看 v0.1:git
登陸成功後主頁面:github
修改用戶信息(用戶名沒法修改):數據庫
電話號碼和郵箱不能爲空:bash
修改爲功:session
檢測登陸狀態:app
若未登陸就檢測登陸狀態:dom
用戶的信息添加了電話號碼和描述,因此要在實體類和數據庫中都作一點改動:jsp
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 中對錶作一點改動:ide
alter table user add gender varchar(10), add description varchar(200);
複製代碼
爲了添加新功能,須要在 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>
複製代碼
而後在接口中添加對應方法:
User showInfo(String username);
void setUserInfo(User user);
複製代碼
先寫異常的接口 ExceptionService 吧:
void setInfoException(User user) throws UserException;
void statusException(String username) throws UserException;
複製代碼
而後寫實現類:
//重置信息檢測,關鍵信息不能爲空
@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("郵箱不能爲空");
}
}
//用戶狀態檢測,若是在 Session 中未找到有用戶登錄,就拋出異常
@Override
public void statusException(String username) throws UserException {
if (username == null){
throw new UserException("請先登陸");
}
}
複製代碼
在 UserService 中新增三個功能,其中有兩個有異常檢測:
User showInfo(String username);
String getStatus(String username) throws UserException;
void setUserInfo(User user) throws UserException;
複製代碼
而後是實現類:
//顯示用戶信息
@Override
public User showInfo(String username) {
return userMapper.showInfo(username);
}
//顯示當前登陸狀態
@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);
}
複製代碼
在登陸時,須要添加一點代碼,將用戶名寫入 Session,以保持登陸狀態:
public ModelAndView login(User user, HttpServletRequest request) {
ModelAndView modelAndView = new ModelAndView("main");
try {
userService.login(user);
userService.verifyCode(request.getParameter("verifyCode"), verifyCode.getText());
//建立 Session,保持登陸狀態
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", "已登出");
}
複製代碼
新增查看用戶狀態的功能:
//查看用戶狀態,顯示是哪一個用戶在登陸,若是沒有登陸的用戶,就會提示你先登陸
@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;
}
複製代碼
而後是用戶信息相關的操做,若是在修改信息時拋出異常,就帶着錯誤信息回到信息修改頁面:
//顯示用戶信息
@RequestMapping("showInfo")
public ModelAndView showInfo(HttpServletRequest request){
return new ModelAndView("userInfo")
.addObject("user", userService.showInfo(
((String)request.getSession().getAttribute("username"))));
}
//對用戶信息進行修改
@RequestMapping("setUserInfo")
public ModelAndView setUserInfo(User user){
ModelAndView modelAndView = new ModelAndView("userInfo");
try {
userService.setUserInfo(user);
//設置提示信息
modelAndView.addObject("message", "修改爲功");
//跳轉
modelAndView.setViewName("main");
} catch (UserException e){
modelAndView.addObject("message", e.getMessage());
}
return modelAndView;
}
複製代碼
新增兩個前端頁面:userInfo.jsp 和 userStatus.jsp,都是差很少的頁面,這裏就不演示了
接下來的 v0.2.2 是採用過濾器來實現相同功能