Index: ssts-packing/src/main/java/com/forgon/disinfectsystem/packing/action/PackingAction.java =================================================================== diff -u -r41634 -r41638 --- ssts-packing/src/main/java/com/forgon/disinfectsystem/packing/action/PackingAction.java (.../PackingAction.java) (revision 41634) +++ ssts-packing/src/main/java/com/forgon/disinfectsystem/packing/action/PackingAction.java (.../PackingAction.java) (revision 41638) @@ -734,7 +734,7 @@ try { // 把参数设置进入 WaitPackingTaskTaskParam WaitPackingTaskTaskParam param = new WaitPackingTaskTaskParam(); - RequestUtils.setPropertiesFromRequest(StrutsParamUtils.getRequest(), param); + RequestUtils.setPropertiesFromRequest2(StrutsParamUtils.getRequest(), param); SqlUtils.checkInputParam(param.getSettleAccountsDepartCoding()); SqlUtils.checkInputParam(param.getTousseOrDepart()); StrutsResponseUtils.output(packingManager.getWaitPackingTaskJson(param)); @@ -748,7 +748,7 @@ JSONObject obj = new JSONObject(); try { WaitPackingTaskTaskParam param = new WaitPackingTaskTaskParam(); - RequestUtils.setPropertiesFromRequest(StrutsParamUtils.getRequest(), param); + RequestUtils.setPropertiesFromRequest2(StrutsParamUtils.getRequest(), param); SqlUtils.checkInputParam(param.getSettleAccountsDepartCoding()); SqlUtils.checkInputParam(param.getTousseOrDepart()); @@ -791,7 +791,7 @@ if(StringUtils.isNotBlank(basketBarcode)){ JSONObject returnObj = new JSONObject(); WaitPackingTaskTaskParam param = new WaitPackingTaskTaskParam(); - RequestUtils.setPropertiesFromRequest(StrutsParamUtils.getRequest(), param); + RequestUtils.setPropertiesFromRequest2(StrutsParamUtils.getRequest(), param); SqlUtils.checkInputParam(param.getSettleAccountsDepartCoding()); JSONArray taskVos = packingManager.getWaitPackingTaskJson(param,returnObj); Set taskGroups = packingManager.getTaskGroupsOfContainer(basketBarcode, param.getTaskGroup()); Index: forgon-tools/src/main/java/com/forgon/tools/util/RequestUtils.java =================================================================== diff -u -r36872 -r41638 --- forgon-tools/src/main/java/com/forgon/tools/util/RequestUtils.java (.../RequestUtils.java) (revision 36872) +++ forgon-tools/src/main/java/com/forgon/tools/util/RequestUtils.java (.../RequestUtils.java) (revision 41638) @@ -6,10 +6,15 @@ import java.io.UnsupportedEncodingException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.math.BigDecimal; import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import javax.servlet.http.HttpServletRequest; @@ -167,5 +172,141 @@ } } } + /** + * 根据请求的参数设置bean的属性值(通过反射机制对set方法调用赋值) + * bean中的属性不限于String类型 只包含部分类型 但不包含Date类型 + * @param request 请求对象 + * @param bean + */ + public static void setPropertiesFromRequest2(HttpServletRequest request, Object bean) { + if (bean == null || request == null) { + return; + } + + // 使用缓存提高性能 + Class clazz = bean.getClass(); + Map setterMethods = getSetterMethodsCache(clazz); + + // 遍历请求参数 + Map parameterMap = request.getParameterMap(); + + for (Map.Entry entry : parameterMap.entrySet()) { + String paramName = entry.getKey(); + String[] values = entry.getValue(); + + if (values == null || values.length == 0 || StringUtils.isBlank(values[0])) { + continue; + } + + String paramValue = values[0]; + Method setterMethod = setterMethods.get(paramName); + + if (setterMethod != null) { + try { + // 获取setter方法的参数类型 + Class[] parameterTypes = setterMethod.getParameterTypes(); + if (parameterTypes.length != 1) { + continue; + } + + // 根据参数类型转换值 + Object convertedValue = convertValue(paramValue, parameterTypes[0]); + if (convertedValue != null) { + setterMethod.invoke(bean, convertedValue); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + } + } + /** + * 缓存setter方法 + */ + private static final Map, Map> SETTER_METHODS_CACHE = new ConcurrentHashMap, Map>();; + /** + * 获取set方法 + * @param clazz + * @return + */ + private static Map getSetterMethodsCache(Class clazz) { + return SETTER_METHODS_CACHE.computeIfAbsent(clazz, k -> { + Map methods = new HashMap<>(); + for (Method method : clazz.getDeclaredMethods()) { + if (isSetterMethod(method)) { + String propertyName = getPropertyNameFromSetter(method); + methods.put(propertyName, method); + + // 也支持驼峰命名的属性名 + String camelCaseName = StringUtils.uncapitalize(propertyName); + if (!camelCaseName.equals(propertyName)) { + methods.put(camelCaseName, method); + } + } + } + return methods; + }); + } + + /** + * 判断是否是setter方法 + * @param method + * @return + */ + private static boolean isSetterMethod(Method method) { + return method.getName().startsWith("set") && + method.getParameterCount() == 1 && + method.getReturnType() == Void.TYPE; + } + + /** + * 从setter方法名获取属性名 + * @param method + * @return + */ + private static String getPropertyNameFromSetter(Method method) { + String methodName = method.getName(); + if (methodName.startsWith("set") && methodName.length() > 3) { + String prop = methodName.substring(3); + return prop.length() == 1 ? prop.toLowerCase() : + prop.substring(0, 1).toLowerCase() + prop.substring(1); + } + return methodName; + } + + /** + * 类型转换方法 + * 只包含部分类型 不包含日期类型 + * @param value + * @param targetType + * @return + */ + private static Object convertValue(String value, Class targetType) { + if (StringUtils.isBlank(value)) { + return null; + } + + try { + if (targetType == String.class) { + return URLDecoder.decode(value, StandardCharsets.UTF_8.name()); + } else if (targetType == Integer.class || targetType == int.class) { + return Integer.parseInt(value); + } else if (targetType == Long.class || targetType == long.class) { + return Long.parseLong(value); + } else if (targetType == Double.class || targetType == double.class) { + return Double.parseDouble(value); + } else if (targetType == Float.class || targetType == float.class) { + return Float.parseFloat(value); + } else if (targetType == Boolean.class || targetType == boolean.class) { + return Boolean.parseBoolean(value) || "1".equals(value); + }else if (targetType == BigDecimal.class) { + return new BigDecimal(value); + } + } catch (Exception e) { + e.printStackTrace(); + } + + return null; + } }