Esempio n. 1
0
  @RequestMapping(value = "/contacts/{action}", method = RequestMethod.GET)
  public String contacts(
      @PathVariable String action,
      @RequestParam(value = "id", required = false) Integer contactId,
      Model model) {

    String viewName = null;

    switch (action) {
      case "list":
        model.addAttribute(
            ModelAttrs.CONTACT_LIST.toString(), this.contactsManager.findAllContacts());
        viewName = ViewData.list_contacts.getViewName();
        break;
      case "new":
        // Edit contact
        if (contactId != null) {
          model.addAttribute(ModelAttrs.CONTACT_EDIT.toString(), true);
          model.addAttribute(
              ModelAttrs.CONTACT_BEAN.toString(), this.contactsManager.findById(contactId));
        }
        // Create contact
        else {
          model.addAttribute(ModelAttrs.CONTACT_CREATE.toString(), true);
          model.addAttribute(ModelAttrs.CONTACT_BEAN.toString(), new Contact());
        }

        viewName = ViewData.new_contacts.getViewName();
        break;
      default:
        break;
    }

    return viewName;
  }
Esempio n. 2
0
  @RequestMapping(value = "/contacts/update", method = RequestMethod.POST)
  public String createOrUpdateContact(
      Model model, @ModelAttribute Contact contact, BindingResult result) {

    if (result.hasErrors()) {

      model.addAttribute(
          contact.getId() == null
              ? ModelAttrs.CONTACT_CREATE.toString()
              : ModelAttrs.CONTACT_EDIT.toString(),
          true); // TO CHANGE
      model.addAttribute(ModelAttrs.HAS_ERRORS.toString(), true);
      model.addAttribute(ModelAttrs.FIELD_ERRORS.toString(), result.getFieldErrors());
      model.addAttribute(ModelAttrs.CONTACT_BEAN.toString(), contact);

      return ViewData.new_contacts.getViewName();
    }

    System.out.println("contact retrieved: " + contact.getNom());

    if (contact.getId() == null) {
      this.contactsManager.persist(contact);
    } else {
      this.contactsManager.merge(contact);
    }

    return "redirect:/contacts/list";
  }