if ("student".equals(role)) { LambdaQueryWrapper<Student> studentLambdaQueryWrapper = new LambdaQueryWrapper<>(); studentLambdaQueryWrapper.eq(Student::getStudentNumber,username); studentLambdaQueryWrapper.eq(Student::getPassword,password); Student student = studentService.getOne(studentLambdaQueryWrapper); if (ObjectUtils.isNotEmpty(student)) { if (ObjectUtils.isNotEmpty(student.getDormitoryId())){ Dormitory dormitory = dormitoryService.getById(student.getDormitoryId()); student.setDormitory(dormitory.getDormitoryNo()); } session.setAttribute("loginUserinfo", student); model.addAttribute("userinfo", student); return "userinfoCenter"; } else { model.addAttribute("msg", "账号或者密码错误"); return "admin/login"; } }请详细解释每一行代码
时间: 2024-02-10 15:12:29 浏览: 28
这段代码是一个 Java Web 应用程序中的登录验证逻辑。接下来,我将对每一行代码进行解释:
1. `if ("student".equals(role)) {`: 判断用户角色是否为 "student",如果是则进行学生登录验证。
2. `LambdaQueryWrapper<Student> studentLambdaQueryWrapper = new LambdaQueryWrapper<>();`: 创建一个 LambdaQueryWrapper 对象,其中的泛型参数为 Student 类型,用于构建查询条件。
3. `studentLambdaQueryWrapper.eq(Student::getStudentNumber,username);`: 在查询条件中添加等于操作,查询学号等于输入的 username 的学生数据。
4. `studentLambdaQueryWrapper.eq(Student::getPassword,password);`: 在查询条件中添加等于操作,查询密码等于输入的 password 的学生数据。
5. `Student student = studentService.getOne(studentLambdaQueryWrapper);`: 调用 studentService 的 getOne 方法,查询符合条件的学生数据,该方法会返回一个 Student 对象。
6. `if (ObjectUtils.isNotEmpty(student)) {`: 判断查询到的 Student 对象是否为空。
7. `if (ObjectUtils.isNotEmpty(student.getDormitoryId())){`: 判断学生是否有宿舍,如果有则执行以下代码。
8. `Dormitory dormitory = dormitoryService.getById(student.getDormitoryId());`: 调用 dormitoryService 的 getById 方法,根据学生的宿舍 ID 查询宿舍信息,该方法会返回一个 Dormitory 对象。
9. `student.setDormitory(dormitory.getDormitoryNo());`: 将学生的宿舍编号设置为宿舍对象的 dormitoryNo 属性值。
10. `session.setAttribute("loginUserinfo", student);`: 将登录用户信息存储到 Session 中,名称为 "loginUserinfo"。
11. `model.addAttribute("userinfo", student);`: 将登录用户信息存储到 Model 中,名称为 "userinfo"。
12. `return "userinfoCenter";`: 返回一个视图名称,表示登录成功后跳转到的页面。
13. `} else {`: 如果查询到的 Student 对象为空,则执行以下代码。
14. `model.addAttribute("msg", "账号或者密码错误");`: 在 Model 中添加一个名为 "msg" 的属性,属性值为 "账号或者密码错误",用于在页面上显示错误提示信息。
15. `return "admin/login";`: 返回一个视图名称,表示登录失败后跳转到的页面。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
package ece448.iot_sim; import java.util.List; import java.util.Map; import java.util.TreeMap; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import ece448.iot_sim.http_server.RequestHandler; public class HTTPCommands implements RequestHandler { // Use a map so we can search plugs by name. private final TreeMap<String, PlugSim> plugs = new TreeMap<>(); public HTTPCommands(List plugs) { for (PlugSim plug: plugs) { this.plugs.put(plug.getName(), plug); } } @Override public String handleGet(String path, Map<String, String> params) { // list all: / // do switch: /plugName?action=on|off|toggle // just report: /plugName logger.info("HTTPCmd {}: {}", path, params); if (path.equals("/")) { return listPlugs(); } PlugSim plug = plugs.get(path.substring(1)); if (plug == null) return null; // no such plug String action = params.get("action"); if (action == null) return report(plug); // P2: add your code here, modify the next line if necessary if("toggle".equals(action)){ plug.toggle(); return report(plug); }else if("on".equals(action)){ plug.switchOn(); return report(plug); }else if("off".equals(action)){ plug.switchOff(); return report(plug); } return "<html><body></body></html>"; } protected String listPlugs() { StringBuilder sb = new StringBuilder(); sb.append("<html><body>"); for (String plugName: plugs.keySet()) { sb.append(String.format("%s", plugName, plugName)); } sb.append("</body></html>"); return sb.toString(); } protected String report(PlugSim plug) { String name = plug.getName(); return String.format("<html><body>" +"Plug %s is %s." +"Power reading is %.3f." +"Switch On" +"Switch Off" +"Toggle" +"</body></html>", name, plug.isOn()? "on": "off", plug.getPower(), name, name, name); }如何对上述代码全部功能进行测试呢?请提供测试代码。
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
详细解读一下这段代码:<%@ page language="java" import="java.text.*,java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>使用application内置对象--checkMessage.jsp</title> </head> <body bgcolor="#abcdef"> <%!Vector<String> v = new Vector<String>(); int i = 0;%> <% String datetime = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(Calendar.getInstance().getTime()); //获取系统时间 %> <% request.setCharacterEncoding("utf-8"); String name = request.getParameter("name"); String title = request.getParameter("title"); String message = request.getParameter("message"); %> <% if (name == null || "".equals(name.trim())) { //trim()主要解决里面只有空格的问题 name = " 网友" + (int) (Math.random() * 100000 + 10000); } if (title == null || "".equals(title.trim())) { title = " 无"; } if (message == null || "".equals(message.trim())) { message = " 无"; } %> <% i++; String str = "第" + "" + i + " " + "楼 " + ".留言人: " + name + ".标题: " + title + ".内容:
" + message + ".时间: " + datetime + ".
"; v.add(str); application.setAttribute("message", v); %> 留言成功. 返回留言板 </body> </html>
![](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)
![apk](https://img-home.csdnimg.cn/images/20210720083646.png)