コード例 #1
0
  /** before方法在调用控制器方法前执行,相反的after则是控制器执行后才执行 */
  @Override
  protected Object before(Invocation inv) throws Exception {
    HttpServletRequest request = inv.getRequest();
    HttpSession session = request.getSession();

    if (session == null || session.getAttribute("loginUser") == null) {
      String pwd = inv.getParameter("pwd");
      String userName = inv.getParameter("userName");
      if (StringUtils.isNotBlank(pwd) && StringUtils.isNotBlank(userName)) {
        Admin admin = adminService.queryAdmin(userName, pwd);

        if (null != admin) {
          UserSession loginUser = this.createAdminSession(admin);
          session.setAttribute("loginUser", loginUser);
        }
      }
    }

    return true;
  }
コード例 #2
0
  public Object[] resolve(final Invocation inv, final ParameterBindingResult parameterBindingResult)
      throws Exception {
    Object[] parameters = new Object[paramMetaDatas.length];
    for (int i = 0; i < resolvers.length; i++) {
      if (resolvers[i] == null) {
        continue;
      }
      try {
        if (logger.isDebugEnabled()) {
          logger.debug(
              "Resolves parameter "
                  + paramMetaDatas[i].getParamType().getSimpleName()
                  + " using "
                  + resolvers[i].getClass().getName());
        }
        parameters[i] = resolvers[i].resolve(inv, paramMetaDatas[i]);
        // afterPropertiesSet
        if (parameters[i] instanceof InitializingBean) {
          ((InitializingBean) parameters[i]).afterPropertiesSet();
        }
        if (parameters[i] == null) {
          DefValue defValudeAnnotation = paramMetaDatas[i].getAnnotation(DefValue.class);
          if (defValudeAnnotation != null && paramMetaDatas[i].getParamType() == String.class) {
            parameters[i] = defValudeAnnotation.value();
          }
        }
      } catch (TypeMismatchException e) {
        // 出现这个错误肯定是解析一般参数失败导致的,而非bean里面的某个属性值的解析失败

        logger.debug("", e);

        // 对简单类型的参数,设置一个默认值给它以支持对该方法的继续调用
        if (paramMetaDatas[i].getParamType().isPrimitive()) {
          DefValue defValudeAnnotation = paramMetaDatas[i].getAnnotation(DefValue.class);
          if (defValudeAnnotation == null
              || DefValue.NATIVE_DEFAULT.equals(defValudeAnnotation.value())) {
            // 对这最常用的类型做一下if-else判断,其他类型就简单使用converter来做吧
            if (paramMetaDatas[i].getParamType() == int.class) {
              parameters[i] = Integer.valueOf(0);
            } else if (paramMetaDatas[i].getParamType() == long.class) {
              parameters[i] = Long.valueOf(0);
            } else if (paramMetaDatas[i].getParamType() == boolean.class) {
              parameters[i] = Boolean.FALSE;
            } else if (paramMetaDatas[i].getParamType() == double.class) {
              parameters[i] = Double.valueOf(0);
            } else if (paramMetaDatas[i].getParamType() == float.class) {
              parameters[i] = Float.valueOf(0);
            } else {
              TypeConverter typeConverter = SafedTypeConverterFactory.getCurrentConverter();
              parameters[i] =
                  typeConverter.convertIfNecessary("0", paramMetaDatas[i].getParamType());
            }
          } else {
            TypeConverter typeConverter = SafedTypeConverterFactory.getCurrentConverter();
            parameters[i] =
                typeConverter.convertIfNecessary(
                    defValudeAnnotation.value(), paramMetaDatas[i].getParamType());
          }
        }
        //
        String paramName = parameterNames[i];
        if (paramName == null) {
          for (String name : paramMetaDatas[i].getParamNames()) {
            if ((paramName = name) != null) {
              break;
            }
          }
        }
        Assert.isTrue(paramName != null);
        FieldError fieldError =
            new FieldError( //
                "method", // 该出错字段所在的对象的名字;对于这类异常我们统一规定名字为method
                paramName, // 出错的字段的名字;取其参数名
                inv.getParameter(paramName), // 被拒绝的值
                true, // whether this error represents a binding failure (like a type mismatch);
                // else, it is a validation failure
                new String[] {e.getErrorCode()}, // "typeMismatch"
                new String[] {
                  inv.getParameter(paramName)
                }, // the array of arguments to be used to resolve this message
                null // the default message to be used to resolve this message
                );
        parameterBindingResult.addError(fieldError);
      } catch (Exception e) {
        // 什么错误呢?比如很有可能是构造对象不能成功导致的错误,没有默认构造函数、构造函数执行失败等等
        logger.error("", e);
        throw e;
      }
    }
    return parameters;
  }