this.getSession().setAttribute("userid", users.getUsersid()); this.getSession().setAttribute("username", users.getUsername());
时间: 2024-05-21 19:17:59 浏览: 5
这段代码是将用户的ID和用户名存储到当前会话(session)中,以便在以后的请求中可以方便地获取这些信息。通过调用getSession()方法,可以获取当前请求的会话对象,然后通过setAttribute()方法将用户的ID和用户名存储到会话中。在以后的请求中,可以通过调用getSession()方法获取当前会话,然后通过getAttribute()方法获取存储在会话中的用户ID和用户名。这样做的好处是可以避免在每个请求中都需要重新获取用户的ID和用户名,从而提高系统的性能。
相关问题
//用户登录 @RequestMapping("login.action") public String login() { this.front(); String username = this.getRequest().getParameter("username"); String password = this.getRequest().getParameter("password"); Users u = new Users(); u.setUsername(username); List<Users> usersList = this.usersService.getUsersByCond(u); if (usersList.size() == 0) { this.getSession().setAttribute("message", "用户名不存在"); return "redirect:/index/preLogin.action"; } else { Users users = usersList.get(0); if (password.equals(users.getPassword())) { this.getSession().setAttribute("userid", users.getUsersid()); this.getSession().setAttribute("username", users.getUsername()); this.getSession().setAttribute("users", users); return "redirect:/index/index.action"; } else { this.getSession().setAttribute("message", "密码错误"); return "redirect:/index/preLogin.action"; } } }解析该段每一句代码
这段代码定义了一个用于用户登录的方法,使用 @RequestMapping 注解来映射请求路径为 "login.action"。方法的返回值是一个字符串,会根据不同的情况跳转到不同的页面。下面是每句代码的解析:
1. `this.front();`:调用父类 BaseController 的 front() 方法,用于初始化一些基础信息。
2. `String username = this.getRequest().getParameter("username");`:从请求中获取参数名为 "username" 的值,赋给本地变量 username。
3. `String password = this.getRequest().getParameter("password");`:从请求中获取参数名为 "password" 的值,赋给本地变量 password。
4. `Users u = new Users();`:创建一个新的 Users 对象。
5. `u.setUsername(username);`:设置 Users 对象的用户名属性为本地变量 username 的值。
6. `List<Users> usersList = this.usersService.getUsersByCond(u);`:调用 usersService 的 getUsersByCond() 方法,传入 Users 对象 u,返回满足条件的 Users 对象列表,赋给本地变量 usersList。
7. `if (usersList.size() == 0) {`:如果 usersList 的大小为 0,即没有找到匹配的用户:
8. `this.getSession().setAttribute("message", "用户名不存在");`:将提示信息 "用户名不存在" 存入会话中,属性名为 "message"。
9. `return "redirect:/index/preLogin.action";`:重定向到 "/index/preLogin.action" 路径,即跳转到登录页面。
10. `} else {`:如果找到了匹配的用户:
11. `Users users = usersList.get(0);`:获取 usersList 的第一个元素,即匹配的用户。
12. `if (password.equals(users.getPassword())) {`:如果本地变量 password 的值等于用户的密码:
13. `this.getSession().setAttribute("userid", users.getUsersid());`:将用户的 ID 存入会话中,属性名为 "userid"。
14. `this.getSession().setAttribute("username", users.getUsername());`:将用户的用户名存入会话中,属性名为 "username"。
15. `this.getSession().setAttribute("users", users);`:将用户的 Users 对象存入会话中,属性名为 "users"。
16. `return "redirect:/index/index.action";`:重定向到 "/index/index.action" 路径,即跳转到首页。
17. `} else {`:如果本地变量 password 的值不等于用户的密码:
18. `this.getSession().setAttribute("message", "密码错误");`:将提示信息 "密码错误" 存入会话中,属性名为 "message"。
19. `return "redirect:/index/preLogin.action";`:重定向到 "/index/preLogin.action" 路径,即跳转到登录页面。
解析该段每一句代码//忘记密码 @RequestMapping("ForgetPwd.action") public String ForgetPwd() { this.front(); String username = this.getRequest().getParameter("username"); String email = this.getRequest().getParameter("email"); String vcode = this.getRequest().getParameter("vcode"); if(vcode.equals("") && vcode.length() != 6){ this.getSession().setAttribute("message", "请输入完整信息"); return "redirect:/index/preForget.action"; } Users u = new Users(); u.setUsername(username); List<Users> usersList = this.usersService.getUsersByCond(u); if (usersList.size() == 0) { this.getSession().setAttribute("message", "用户名不存在"); return "redirect:/index/preForget.action"; } else { Users users = usersList.get(0); if (email.equals(users.getEmail()) && vcode.length() == 6) { this.getSession().setAttribute("userid", users.getUsersid()); this.getSession().setAttribute("username", users.getUsername()); this.getSession().setAttribute("users", users); this.getSession().setAttribute("message", "您的密码为:"+users.getPassword()); return "redirect:/index/index.action"; }else{ this.getSession().setAttribute("message", "请输入正确验证码"); return "redirect:/index/preForget.action"; } } }
这段代码是一个处理忘记密码的请求的方法,注解`@RequestMapping("ForgetPwd.action")`表示该方法对应的请求路径为"ForgetPwd.action"。下面是代码的详细解析:
```java
//处理忘记密码的请求
@RequestMapping("ForgetPwd.action")
public String ForgetPwd() {
//调用front()方法设置前台界面
this.front();
//获取请求参数中的用户名、邮箱和验证码
String username = this.getRequest().getParameter("username");
String email = this.getRequest().getParameter("email");
String vcode = this.getRequest().getParameter("vcode");
//判断验证码是否为空且长度是否为6
if(vcode.equals("") && vcode.length() != 6){
//若验证码为空或长度不为6,则设置提示信息并重定向到忘记密码页面
this.getSession().setAttribute("message", "请输入完整信息");
return "redirect:/index/preForget.action";
}
//根据用户名查询用户信息
Users u = new Users();
u.setUsername(username);
List<Users> usersList = this.usersService.getUsersByCond(u);
//判断是否查到用户信息
if (usersList.size() == 0) {
//若未查到用户信息,则设置提示信息并重定向到忘记密码页面
this.getSession().setAttribute("message", "用户名不存在");
return "redirect:/index/preForget.action";
} else {
//若查到用户信息,则取第一个用户对象
Users users = usersList.get(0);
//判断邮箱和验证码是否正确
if (email.equals(users.getEmail()) && vcode.length() == 6) {
//若邮箱和验证码正确,则设置用户id、用户名、用户信息和提示信息,并重定向到首页
this.getSession().setAttribute("userid", users.getUsersid());
this.getSession().setAttribute("username", users.getUsername());
this.getSession().setAttribute("users", users);
this.getSession().setAttribute("message", "您的密码为:"+users.getPassword());
return "redirect:/index/index.action";
}else{
//若邮箱或验证码不正确,则设置提示信息并重定向到忘记密码页面
this.getSession().setAttribute("message", "请输入正确验证码");
return "redirect:/index/preForget.action";
}
}
}
```
该方法首先调用`front()`方法设置前台界面,然后获取请求参数中的用户名、邮箱和验证码,判断验证码是否为空且长度是否为6,若验证码为空或长度不为6,则设置提示信息并重定向到忘记密码页面;若验证码正确,则根据用户名查询用户信息,判断是否查到用户信息,若未查到用户信息,则设置提示信息并重定向到忘记密码页面;若查到用户信息,则判断邮箱和验证码是否正确,若正确,则设置用户id、用户名、用户信息和提示信息,并重定向到首页;若邮箱或验证码不正确,则设置提示信息并重定向到忘记密码页面。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)