Esempio n. 1
0
  /**
   * Initialize the <code>Validator</code> to perform validation.
   *
   * @param key The key that the validation rules are under (the form elements
   * name attribute).
   * @param bean The bean validation is being performed on.
   * @param application servlet context
   * @param request The current request object.
   * @param errors The object any errors will be stored in.
   * @param page This in conjunction with  the page property of a
   * <code>Field<code> can control the processing of fields.  If the field's
   * page is less than or equal to this page value, it will be processed.
   */
  public static Validator initValidator(
      String key,
      Object bean,
      ServletContext application,
      HttpServletRequest request,
      ActionMessages errors,
      int page) {

    ValidatorResources resources = Resources.getValidatorResources(application, request);

    Locale locale = RequestUtils.getUserLocale(request, null);

    Validator validator = new Validator(resources, key);
    validator.setUseContextClassLoader(true);

    validator.setPage(page);

    validator.setParameter(SERVLET_CONTEXT_PARAM, application);
    validator.setParameter(HTTP_SERVLET_REQUEST_PARAM, request);
    validator.setParameter(Validator.LOCALE_PARAM, locale);
    validator.setParameter(ACTION_MESSAGES_PARAM, errors);
    validator.setParameter(Validator.BEAN_PARAM, bean);

    return validator;
  }
Esempio n. 2
0
  /**
   * Gets the <code>ActionMessage</code> based on the <code>ValidatorAction</code> message and the
   * <code>Field</code>'s arg objects.
   *
   * @param request the servlet request
   * @param va Validator action
   * @param field the validator Field
   */
  public static ActionMessage getActionMessage(
      HttpServletRequest request, ValidatorAction va, Field field) {

    String args[] =
        getArgs(
            va.getName(),
            getMessageResources(request),
            RequestUtils.getUserLocale(request, null),
            field);

    String msg = field.getMsg(va.getName()) != null ? field.getMsg(va.getName()) : va.getMsg();

    return new ActionMessage(msg, args);
  }
Esempio n. 3
0
  /**
   * Gets the <code>ActionMessage</code> based on the <code>ValidatorAction</code> message and the
   * <code>Field</code>'s arg objects.
   *
   * @param validator the Validator
   * @param request the servlet request
   * @param va Validator action
   * @param field the validator Field
   */
  public static ActionMessage getActionMessage(
      Validator validator, HttpServletRequest request, ValidatorAction va, Field field) {

    Msg msg = field.getMessage(va.getName());
    if (msg != null && !msg.isResource()) {
      return new ActionMessage(msg.getKey(), false);
    }

    String msgKey = null;
    String msgBundle = null;
    if (msg == null) {
      msgKey = va.getMsg();
    } else {
      msgKey = msg.getKey();
      msgBundle = msg.getBundle();
    }

    if (msgKey == null || msgKey.length() == 0) {
      return new ActionMessage("??? " + va.getName() + "." + field.getProperty() + " ???", false);
    }

    ServletContext application =
        (ServletContext) validator.getParameterValue(SERVLET_CONTEXT_PARAM);
    MessageResources messages = getMessageResources(application, request, msgBundle);
    Locale locale = RequestUtils.getUserLocale(request, null);

    Arg[] args = field.getArgs(va.getName());
    String[] argValues = getArgValues(application, request, messages, locale, args);

    ActionMessage actionMessage = null;
    if (msgBundle == null) {
      actionMessage = new ActionMessage(msgKey, argValues);
    } else {
      String message = messages.getMessage(locale, msgKey, argValues);
      actionMessage = new ActionMessage(message, false);
    }
    return actionMessage;
  }
Esempio n. 4
0
  /**
   * Gets the <code>Locale</code> sensitive value based on the key passed in.
   *
   * @param request servlet request
   * @param key the request key
   */
  public static String getMessage(HttpServletRequest request, String key) {
    MessageResources messages = getMessageResources(request);

    return getMessage(messages, RequestUtils.getUserLocale(request, null), key);
  }
Esempio n. 5
0
 /**
  * Get the <code>Locale</code> of the current user.
  *
  * @param request servlet request
  * @deprecated Use RequestUtils.getUserLocale() instead. This will be removed after Struts 1.2.
  */
 public static Locale getLocale(HttpServletRequest request) {
   return RequestUtils.getUserLocale(request, null);
 }