Пример #1
0
 protected void populateParams() {
   super.populateParams();
   UIBean uiBean = ((UIBean) component);
   S3TagValidationBuilder.build(
       this, this.getStack(), (HttpServletRequest) this.pageContext.getRequest(), uiBean);
   if (this.cssClass == null) {
     uiBean.setCssClass("form-control");
   }
   if (this.theme == null) {
     uiBean.setTheme("bootstrap3");
   }
 }
Пример #2
0
  /**
   * First looks for the token in the PageContext using the supplied name (or {@link
   * org.apache.struts2.util.TokenHelper#DEFAULT_TOKEN_NAME} if no name is provided) so that the
   * same token can be re-used for the scope of a request for the same name. If the token is not in
   * the PageContext, a new Token is created and set into the Session and the PageContext with the
   * name.
   */
  protected void evaluateExtraParams() {
    super.evaluateExtraParams();

    String tokenName;
    Map parameters = getParameters();

    if (parameters.containsKey("name")) {
      tokenName = (String) parameters.get("name");
    } else {
      if (name == null) {
        tokenName = TokenHelper.DEFAULT_TOKEN_NAME;
      } else {
        tokenName = findString(name);

        if (tokenName == null) {
          tokenName = name;
        }
      }

      addParameter("name", tokenName);
    }

    String token = buildToken(tokenName);
    addParameter("token", token);
    addParameter("tokenNameField", TokenHelper.TOKEN_NAME_FIELD);
  }
Пример #3
0
  protected void populateParams() {
    // 此段落必须放在super.populateParams()之前
    try {
      if (StringUtils.isNotBlank(autoValidate)) {
        this.setDynamicAttribute(null, "autoValidate", autoValidate);
      }
    } catch (JspException e) {
      e.printStackTrace();
    }

    super.populateParams();
    UIBean uiBean = ((UIBean) component);

    if (id == null) {
      String formid = "form_" + RandomStringUtils.randomAlphabetic(10);
      uiBean.setId(formid);
      // 将id属性设置到pageContext中,便于前端JS代码进行对象分组控制
      pageContext.setAttribute("closestFormId", formid);
    }
    if (this.theme == null) {
      uiBean.setTheme("bootstrap");
    }
  }
Пример #4
0
  protected Map getTooltipConfig(UIBean component) {
    Object tooltipConfigObj = component.getParameters().get("tooltipConfig");
    Map<String, String> tooltipConfig = new LinkedHashMap<String, String>();

    if (tooltipConfigObj instanceof Map) {
      // we get this if its configured using
      // 1] UI component's tooltipConfig attribute  OR
      // 2] <param name="tooltip" value="" /> param tag value attribute

      tooltipConfig = new LinkedHashMap<String, String>((Map) tooltipConfigObj);
    } else if (tooltipConfigObj instanceof String) {

      // we get this if its configured using
      // <param name="tooltipConfig"> ... </param> tag's body
      String tooltipConfigStr = (String) tooltipConfigObj;
      String[] tooltipConfigArray = tooltipConfigStr.split("\\|");

      for (String aTooltipConfigArray : tooltipConfigArray) {
        String[] configEntry = aTooltipConfigArray.trim().split("=");
        String key = configEntry[0].trim();
        String value;
        if (configEntry.length > 1) {
          value = configEntry[1].trim();
          tooltipConfig.put(key, value);
        } else {
          if (LOG.isWarnEnabled()) {
            LOG.warn(
                "component "
                    + component
                    + " tooltip config param "
                    + key
                    + " has no value defined, skipped");
          }
        }
      }
    }
    if (component.javascriptTooltip != null)
      tooltipConfig.put("jsTooltipEnabled", component.javascriptTooltip);
    if (component.tooltipIconPath != null)
      tooltipConfig.put("tooltipIcon", component.tooltipIconPath);
    if (component.tooltipDelay != null) tooltipConfig.put("tooltipDelay", component.tooltipDelay);
    return tooltipConfig;
  }
  public static void buildValidatorAttribute(
      String validatorAttrValue,
      AbstractUITag tag,
      ValueStack stack,
      HttpServletRequest req,
      UIBean uiBean) {
    Map<String, Object> dynamicAttributes = new HashMap<String, Object>();
    if (validatorAttrValue == null) {
      Map<String, Object> validator = new HashMap<String, Object>();
      Map<String, String> messages = new HashMap<String, String>();
      CompoundRoot rootList = stack.getRoot();
      Object entity = null;
      Object controller = null;
      for (Object obj : rootList) {
        if (obj instanceof Persistable) {
          entity = obj;
        } else if (obj instanceof ActionSupport) {
          controller = obj;
        }
      }

      if (entity != null) {
        try {
          String tagName = tag.name;
          Method method =
              OgnlRuntime.getGetMethod(
                  (OgnlContext) stack.getContext(), entity.getClass(), tagName);
          if (method == null) {
            String[] tagNameSplits = StringUtils.split(tagName, ".");
            if (tagNameSplits.length >= 2) {
              Class<?> retClass = entity.getClass();
              for (String tagNameSplit : tagNameSplits) {
                method =
                    OgnlRuntime.getGetMethod(
                        (OgnlContext) stack.getContext(), retClass, tagNameSplit);
                if (method != null) {
                  retClass = method.getReturnType();
                }
              }
              if (method == null) {
                retClass = controller.getClass();
                for (String tagNameSplit : tagNameSplits) {
                  method =
                      OgnlRuntime.getGetMethod(
                          (OgnlContext) stack.getContext(), retClass, tagNameSplit);
                  if (method != null) {
                    retClass = method.getReturnType();
                  }
                }
              }
            }
          }

          if (method != null) {
            Column column = method.getAnnotation(Column.class);
            if (column != null) {
              if (column.nullable() == false) {
                if (tag.requiredLabel == null) {
                  uiBean.setRequiredLabel("true");
                }
              }
              if (column.unique() == true) {
                validator.put("unique", "true");
              }
            }

            Class<?> retType = method.getReturnType();
            if (retType == LocalDate.class) {
              validator.put("date", true);
              validator.put("dateISO", true);
            } else if (retType == LocalDateTime.class) {
              validator.put("timestamp", true);
            } else if (retType == DateTime.class || retType == Date.class) {
              JsonSerialize jsonSerialize = method.getAnnotation(JsonSerialize.class);
              if (jsonSerialize != null) {
                if (JodaDateJsonSerializer.class == jsonSerialize.using()
                    || DateJsonSerializer.class == jsonSerialize.using()) {
                  validator.put("date", true);
                  validator.put("dateISO", true);
                } else if (JodaDateTimeJsonSerializer.class == jsonSerialize.using()
                    || DateTimeJsonSerializer.class == jsonSerialize.using()) {
                  validator.put("timestamp", true);
                }
              }
            } else if (retType == BigDecimal.class) {
              validator.put("number", true);
            } else if (retType == Integer.class) {
              validator.put("number", true);
              validator.put("digits", true);
            } else if (retType == Long.class) {
              validator.put("number", true);
              validator.put("digits", true);
            }

            Size size = method.getAnnotation(Size.class);
            if (size != null) {
              if (size.min() > 0) {
                validator.put("minlength", size.min());
              }
              if (size.max() < Integer.MAX_VALUE) {
                validator.put("maxlength", size.max());
              }
            }

            Email email = method.getAnnotation(Email.class);
            if (email != null) {
              validator.put("email", true);
            }

            Pattern pattern = method.getAnnotation(Pattern.class);
            if (pattern != null) {
              validator.put("regex", pattern.regexp());
              String message = pattern.message();
              if (!"{javax.validation.constraints.Pattern.message}".equals(message)) {
                messages.put("regex", message);
              }
            }
          }
        } catch (IntrospectionException e) {
          e.printStackTrace();
        } catch (OgnlException e) {
          e.printStackTrace();
        }
      }

      if (validator.size() > 0) {
        try {
          if (messages.size() > 0) {
            validator.put("messages", messages);
          }
          String json = mapper.writeValueAsString(validator);
          json = json.replaceAll("\\\"", "'");
          dynamicAttributes.put("validator", json);
          uiBean.setDynamicAttributes(dynamicAttributes);
        } catch (JsonProcessingException e) {
          e.printStackTrace();
        }
      }
    } else {
      dynamicAttributes.put("validator", validatorAttrValue);
      uiBean.setDynamicAttributes(dynamicAttributes);
    }
  }