public void dictionaryConvert(Object obj, HttpServletRequest request) { try { if (obj == null) return; //当前view和entity中的所有types的字段 List<String> fieldNameList = new ArrayList<>(); Class tempClass = obj.getClass(); while (tempClass !=null) { Field[] declaredFields = tempClass.getDeclaredFields(); for (Field f : declaredFields) { f.setAccessible(true); if (f.getType().getName().equals("java.lang.Integer") && f.getName().contains("Types")) { fieldNameList.add(f.getName()); } } tempClass = tempClass.getSuperclass(); //得到父类,然后赋给自己 }
时间: 2023-07-15 11:12:21 浏览: 157
这段代码的作用是将对象中所有类型为java.lang.Integer并且字段名中包含"Types"的属性名保存到一个List中。首先判断传入的对象是否为空,然后通过反射获取对象的Class对象,并通过while循环遍历该对象及其父类的所有 declaredFields 字段。对于每个字段,如果其类型为java.lang.Integer,并且字段名包含“Types”,则将该字段名添加到 fieldNameList 列表中。最后返回 fieldNameList 列表。
相关问题
package com.birdboot.controller; import com.birdboot.entity.User; import com.birdboot.http.HttpServletRequest; import com.birdboot.http.HttpServletResponse; import java.io.*; /** * 用于处理与用户数据相关的业务 */ public class UserController { private static File userDir; static { userDir = new File("./users"); if(!userDir.exists()){ userDir.mkdirs(); } } public void reg(HttpServletRequest request, HttpServletResponse response){ System.out.println("开始处理用户注册!!!"); //1获取表单数据 String username = request.getParameter("username"); String password = request.getParameter("password"); String nickname = request.getParameter("nickname"); String ageStr = request.getParameter("age"); System.out.println(username+","+password+","+nickname+","+ageStr); //必要的验证 if(username==null||username.isEmpty()|| password==null||password.isEmpty()|| nickname==null||nickname.isEmpty()|| ageStr==null||ageStr.isEmpty()|| !ageStr.matches("[0-9]+") ){ response.sendRedirect("/reg_info_error.html"); return; } int age = Integer.parseInt(ageStr); User user = new User(username,password,nickname,age); File userFile = new File(userDir,username+".obj"); if(userFile.exists()){ response.setStatusReason("/have_user.html"); return; } try { FileOutputStream fos = new FileOutputStream(userFile); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(user); response.setStatusReason("/reg_success.html"); } catch (IOException e) { e.printStackTrace(); } } }
这段代码是一个Java Web应用程序中的控制器类,用于处理用户注册请求。它包含一个reg方法,该方法接收一个HttpServletRequest对象和一个HttpServletResponse对象作为参数,用于获取用户提交的注册表单数据,并将用户数据保存到本地文件系统中。具体解释如下:
1. 首先,类定义了一个静态的File类型的userDir变量,用于存储用户数据的目录,并在静态代码块中初始化该变量。如果该目录不存在,则创建该目录。
2. reg方法使用request对象的getParameter方法获取表单数据,包括username、password、nickname和age等字段,并进行必要的验证,如字段不能为空且年龄必须为数字等。
3. 如果表单数据验证通过,则将表单数据封装成一个User对象,并使用该对象的用户名作为文件名,在userDir目录下创建一个对象输出流,并将User对象写入该流中,最后响应客户端注册成功的页面。
4. 如果用户已经存在,则响应客户端用户已存在的页面。
需要注意的是,该代码并不完整,可能还需要一些其他的类和方法来实现完整的用户注册功能。
ssm管理系统文件中util文件里PageHelper类解释以下代码package com.util; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletRequest; public class PageHelper { public static void getPage(List<?> list, String name, List<String> nameList, List<String> valueList, int pageSize, String number, HttpServletRequest request, String method) { StringBuffer buffer = new StringBuffer(); String name2 = name.substring(0, 1).toUpperCase() + name.substring(1); String path = ""; String action = "getAll" + name2 + ".action"; if (method != null) { action = "query" + name2 + "ByCond.action"; } List<Object> objList = new ArrayList<Object>(); if (nameList != null && valueList != null) { for (int i = 0; i < nameList.size(); i++) { path += "&" + nameList.get(i) + "=" + valueList.get(i); } } int pageNumber = list.size(); int maxPage = pageNumber; if (maxPage % pageSize == 0) { maxPage = maxPage / pageSize; } else { maxPage = maxPage / pageSize + 1; } if (number == null) { number = "0"; } int start = Integer.parseInt(number) * pageSize; int over = (Integer.parseInt(number) + 1) * pageSize; int count = pageNumber - over; if (count <= 0) { over = pageNumber; } for (int i = start; i < over; i++) { Object obj = list.get(i); objList.add(obj); } buffer.append(" 共为"); buffer.append(maxPage); buffer.append("页 共有"); buffer.append(pageNumber); buffer.append("条 当前为第"); buffer.append((Integer.parseInt(number) + 1)); buffer.append("页 "); if ((Integer.parseInt(number) + 1) == 1) { buffer.append("首页"); } else { buffer.append("<a href=\"" + name + "/" + action + "?number=0" + path + "\">首页</a>"); } buffer.append(" "); if ((Integer.parseInt(number) + 1) == 1) { buffer.append("上一页"); } else { buffer.append("<a href=\"" + name + "/" + action + "?number=" + (Integer.parseInt(number) - 1) + "" + path + "\">上一页</a>"); } buffer.append(" "); if (maxPage <= (Integer.parseInt(number) + 1)) { buffer.append("下一页"); } else { buffer.append("<a href=\"" + name + "/" + action + "?number=" + (Integer.parseInt(number) + 1) + "" + path + "\">下一页</a>"); } buffer.append(" "); if (maxPage <= (Integer.parseInt(number) + 1)) { buffer.append("尾页"); } else { buffer.append("<a href=\"" + name + "/" + action + "?number=" + (maxPage - 1) + "" + path + "\">尾页</a>"); } String html = buffer.toString(); request.setAttribute("html", html); request.setAttribute(name + "List", objList); }
The `PageHelper` class in the `util` package of the SSM management system is used to implement pagination functionality for displaying data in pages.
The `getPage` method takes the following parameters:
- `list`: the list of objects to be paginated
- `name`: the name of the entity being paginated
- `nameList`: a list of parameter names used for filtering the data
- `valueList`: a list of parameter values used for filtering the data
- `pageSize`: the number of objects to be displayed per page
- `number`: the current page number
- `request`: the HTTP servlet request object
- `method`: the name of the method used for filtering the data
The method first constructs the URL for the pagination links by appending the `action` parameter to the entity name. If a filter method is provided, it replaces the default `getAll` method with the filter method.
Next, it creates a new list to hold the paginated data and calculates the total number of pages based on the size of the original data list and the page size. It then adds the relevant parameters to the URL for each page and constructs the HTML code for the pagination links.
Finally, it sets the paginated data list and pagination HTML code as attributes of the request object to be passed back to the view for display.
阅读全文