コード例 #1
0
 @ActionMapping("add")
 public void addPerson(
     @Valid @ModelAttribute Person person,
     BindingResult result,
     ActionRequest actionRequest,
     ActionResponse actionResponse,
     SessionStatus sessionStatus,
     Model model) {
   if (!result.hasErrors()) {
     logger.info("ACTION: action saving person = " + person);
     try {
       DateTime dt1 = DateTime.now();
       DateTime dt2 = dt1.plusYears(10);
       IdentityCard identityCard =
           new IdentityCard(person, "BL", "112233", dt1.toDate(), dt2.toDate());
       identityCard.setAddress("-");
       person.setIdentityCard(identityCard);
       personManager.save(person);
       model.addAttribute(
           "message",
           messageSource.getMessage("label.Person.saved", null, actionRequest.getLocale()));
       sessionStatus.setComplete();
     } catch (Exception e) {
       logger.error("Unexpected error when saving person.", e);
       model.addAttribute("error", "Internal Error. Contact Administrator.");
     }
   } else {
     logger.info("Validation failed");
     model.addAttribute("errors", result);
   }
 }
コード例 #2
0
  protected void validateFields(ActionRequest actionRequest) throws Exception {

    Locale defaultLocale = LocaleUtil.getDefault();
    String defaultLanguageId = LocaleUtil.toLanguageId(defaultLocale);

    boolean sendAsEmail = GetterUtil.getBoolean(getParameter(actionRequest, "sendAsEmail"));
    String subject = getParameter(actionRequest, "subject");

    boolean saveToDatabase = GetterUtil.getBoolean(getParameter(actionRequest, "saveToDatabase"));

    boolean saveToFile = GetterUtil.getBoolean(getParameter(actionRequest, "saveToFile"));

    if (!sendAsEmail && !saveToDatabase && !saveToFile) {
      SessionErrors.add(actionRequest, "handlingRequired");
    }

    if (sendAsEmail) {
      if (Validator.isNull(subject)) {
        SessionErrors.add(actionRequest, "subjectRequired");
      }

      String[] emailAdresses = WebFormUtil.split(getParameter(actionRequest, "emailAddress"));

      if (emailAdresses.length == 0) {
        SessionErrors.add(actionRequest, "emailAddressRequired");
      }

      for (String emailAdress : emailAdresses) {
        emailAdress = emailAdress.trim();

        if (!Validator.isEmailAddress(emailAdress)) {
          SessionErrors.add(actionRequest, "emailAddressInvalid");
        }
      }
    }

    if (saveToFile) {
      String fileName = getParameter(actionRequest, "fileName");

      // Check if server can create a file as specified

      try {
        FileOutputStream fileOutputStream = new FileOutputStream(fileName, true);

        fileOutputStream.close();
      } catch (SecurityException se) {
        SessionErrors.add(actionRequest, "fileNameInvalid");
      } catch (FileNotFoundException fnfe) {
        SessionErrors.add(actionRequest, "fileNameInvalid");
      }
    }

    if (saveToDatabase) {
      int i = 1;

      String languageId = LocaleUtil.toLanguageId(actionRequest.getLocale());

      String fieldLabel = ParamUtil.getString(actionRequest, "fieldLabel" + i + "_" + languageId);

      while ((i == 1) || Validator.isNotNull(fieldLabel)) {
        if (fieldLabel.length() > 75) {
          SessionErrors.add(actionRequest, "fieldSizeInvalid" + i);
        }

        i++;

        fieldLabel = ParamUtil.getString(actionRequest, "fieldLabel" + i + "_" + languageId);
      }
    }

    if (!validateUniqueFieldNames(actionRequest)) {
      SessionErrors.add(actionRequest, DuplicateColumnNameException.class.getName());
    }
  }