Exemplo n.º 1
0
 public boolean supports(ParamMetaData metaData) {
   if (ClassUtils.isPrimitiveOrWrapper(metaData.getParamType())) {
     return true;
   }
   SimpleTypeConverter simpleTypeConverter = SafedTypeConverterFactory.getCurrentConverter();
   return simpleTypeConverter.findCustomEditor(metaData.getParamType(), null) != null
       || simpleTypeConverter.getDefaultEditor(metaData.getParamType()) != null;
 }
Exemplo n.º 2
0
    public Object resolve(Invocation inv, ParamMetaData metaData) {
      String toConvert = null;
      //
      FlashParam flashParam = metaData.getAnnotation(FlashParam.class);
      if (flashParam != null) {
        toConvert = inv.getFlash().get(flashParam.value());
      }

      for (String paramName : metaData.getParamNames()) {
        if (paramName != null) {
          toConvert = inv.getRequest().getParameter(paramName);
          if (toConvert != null) {
            break;
          }
        }
      }
      if (toConvert == null) {
        DefValue defValudeAnnotation = metaData.getAnnotation(DefValue.class);
        if (defValudeAnnotation != null
            && !DefValue.NATIVE_DEFAULT.equals(defValudeAnnotation.value())) {
          toConvert = defValudeAnnotation.value();
        }
      }
      if (toConvert != null) {
        SimpleTypeConverter typeConverter = SafedTypeConverterFactory.getCurrentConverter();
        return typeConverter.convertIfNecessary(toConvert, metaData.getParamType());
      }
      if (metaData.getParamType().isPrimitive()) {
        // 对这最常用的类型做一下if-else判断,其他类型就简单使用converter来做吧
        if (metaData.getParamType() == int.class) {
          return Integer.valueOf(0);
        } else if (metaData.getParamType() == long.class) {
          return Long.valueOf(0);
        } else if (metaData.getParamType() == boolean.class) {
          return Boolean.FALSE;
        } else if (metaData.getParamType() == double.class) {
          return Double.valueOf(0);
        } else if (metaData.getParamType() == float.class) {
          return Float.valueOf(0);
        } else {
          SimpleTypeConverter typeConverter = SafedTypeConverterFactory.getCurrentConverter();
          return typeConverter.convertIfNecessary("0", metaData.getParamType());
        }
      }
      return null;
    }
  /**
   * 得到参数列表
   *
   * @param method
   * @param model
   * @param request
   * @param response
   * @param c
   * @return
   */
  @SuppressWarnings("unchecked")
  public static Object[] getArgs(
      Method method,
      Map<String, Object> model,
      HttpServletRequest request,
      HttpServletResponse response,
      Class<?> c) {
    Class<?>[] paramTypes = method.getParameterTypes();
    Object[] args = new Object[paramTypes.length];
    Map<String, Object> argMap = new HashMap<String, Object>(args.length);
    Map<String, String> pathValues = null;
    PathPattern pathPattern = method.getAnnotation(PathPattern.class);
    if (pathPattern != null) {
      String path = request.getRequestURI();
      int index = path.lastIndexOf('.');
      if (index != -1) {
        path = path.substring(0, index);
        String[] patterns = pathPattern.patterns();
        pathValues = getPathValues(patterns, path);
      }
    }
    MapBindingResult errors = new MapBindingResult(argMap, "");
    ParameterNameDiscoverer parameterNameDiscoverer =
        new LocalVariableTableParameterNameDiscoverer();
    for (int i = 0; i < paramTypes.length; i++) {
      Class<?> paramType = paramTypes[i];

      MethodParameter methodParam = new MethodParameter(method, i);
      methodParam.initParameterNameDiscovery(parameterNameDiscoverer);
      GenericTypeResolver.resolveParameterType(methodParam, c.getClass());

      String paramName = methodParam.getParameterName();
      // map
      if (Map.class.isAssignableFrom(paramType)) {
        args[i] = model;
      }
      // HttpServletRequest
      else if (HttpServletRequest.class.isAssignableFrom(paramType)) {
        args[i] = request;
      }
      // HttpServletResponse
      else if (HttpServletResponse.class.isAssignableFrom(paramType)) {
        args[i] = response;
      }
      // HttpSession
      else if (HttpSession.class.isAssignableFrom(paramType)) {
        args[i] = request.getSession();
      }
      // Errors
      else if (Errors.class.isAssignableFrom(paramType)) {
        args[i] = errors;
      }
      // MultipartFile
      else if (MultipartFile.class.isAssignableFrom(paramType)) {
        MultipartFile[] files = resolveMultipartFiles(request, errors, paramName);
        if (files != null && files.length > 0) {
          args[i] = files[0];
        }
      }
      // MultipartFile[]
      else if (MultipartFile[].class.isAssignableFrom(paramType)) {
        args[i] = resolveMultipartFiles(request, errors, paramName);
      } else {
        // 简单数据类型
        if (BeanUtils.isSimpleProperty(paramType)) {
          SimpleTypeConverter converter = new SimpleTypeConverter();
          Object value;
          // 是否是数组
          if (paramType.isArray()) {
            value = request.getParameterValues(paramName);
          } else {
            Object[] parameterAnnotations = methodParam.getParameterAnnotations();
            value = null;
            if (parameterAnnotations != null && parameterAnnotations.length > 0) {
              if (pathValues != null && pathValues.size() > 0) {
                for (Object object : parameterAnnotations) {
                  if (PathVariable.class.isInstance(object)) {
                    PathVariable pv = (PathVariable) object;
                    if (StringUtils.isEmpty(pv.value())) {
                      value = pathValues.get(paramName);
                    } else {
                      value = pathValues.get(pv.value());
                    }
                    break;
                  }
                }
              }
            } else {
              value = request.getParameter(paramName);
            }
          }
          try {
            args[i] = converter.convertIfNecessary(value, paramType, methodParam);
            model.put(paramName, args[i]);
          } catch (TypeMismatchException e) {
            errors.addError(new FieldError(paramName, paramName, e.getMessage()));
          }
        } else {
          // 复杂数据类型POJO类
          if (paramType.isArray()) {
            ObjectArrayDataBinder binder =
                new ObjectArrayDataBinder(paramType.getComponentType(), paramName);
            args[i] = binder.bind(request);
            model.put(paramName, args[i]);
          } else {
            Object bindObject = BeanUtils.instantiateClass(paramType);
            SummerServletRequestDataBinder binder =
                new SummerServletRequestDataBinder(bindObject, paramName);
            binder.bind(request);
            BindException be = new BindException(binder.getBindingResult());
            List<FieldError> fieldErrors = be.getFieldErrors();
            for (FieldError fieldError : fieldErrors) {
              errors.addError(fieldError);
            }
            args[i] = binder.getTarget();
            model.put(paramName, args[i]);
          }
        }
      }
    }
    return args;
  }