**
.用户登陆
**我是一个小白一下是对用户登陆系统得总结,希望其他人用得上,
思路:
获取登陆页面表单提交的userName 和password
到userController层将获取的userName拿到数据库查询,该用户是否存在,有该用户的话在前台获取的password与后台查询到的password是否一致
图解如下:
前端
登陆投票系统
<table style="text-align: center">
<div class="input-group">
<div class="col-3 col-form-label">
<label class="formLabel" for="password"><s:message code="user.password"></s:message></label>
</div>
<div class="col-8">
<input id="password" class="form-control" name="password" type="password">
</div>
</div>
</table>
<br>
<br>
<br>
<div style="text-align: center ;padding: 5px">
<input type="submit" value="登录" style="background-color: darkseagreen" />
<input type="button" style="background-color: darkseagreen" value="重置" onclick="clearForm()"/>
<input type="button" style="background-color: darkseagreen" value="注册" onclick="window.location.href='/user/insert'"/>
</div>
</table>
</div>
控制层
@RequestMapping("/login")
public String login(){
return “home/loginUI”;
}
@RequestMapping("/findUserName")
@ResponseBody
public ModelAndView findUserName(User user,
@RequestParam(value=“userName”)String userName,
@RequestParam(value =“password”)String password,
HttpSession httpSession) {
String passwords=password;
String userNames=userName;
user= userService.findUserName(userNames, passwords);
httpSession.setAttribute(“ss”,user);
ModelAndView modelAndView =new ModelAndView();
if (user !=null) {
modelAndView.setViewName(“home/home”);
//modelAndView.addObject(“ss”,userName);
return modelAndView;
} else {
modelAndView.setViewName(“home/loginUI”);
return modelAndView;
}
}
service层
@Override
public User findUserName(String userName, String password) {
User user=userDao.findUserName(userName);
if (user !=null && user.getPassword().equals(password)) {
return user;
}
return null;
}
谢谢!参考