Example usage for org.springframework.validation BindingResult getGlobalErrors

List of usage examples for org.springframework.validation BindingResult getGlobalErrors

Introduction

In this page you can find the example usage for org.springframework.validation BindingResult getGlobalErrors.

Prototype

List<ObjectError> getGlobalErrors();

Source Link

Document

Get all global errors.

Usage

From source file:cz.muni.fi.pa165.mvc.controllers.TeamController.java

@RequestMapping(value = "/create", method = RequestMethod.POST)
public String create(@Valid @ModelAttribute("teamCreate") TeamDTO formBean, BindingResult bindingResult,
        Model model, RedirectAttributes redirectAttributes, UriComponentsBuilder uriBuilder) {
    Long id = null;//from   w  w w .jav  a2 s . c  om
    log.debug("create(teamCreate={})", formBean);
    if (bindingResult.hasErrors()) {
        for (ObjectError ge : bindingResult.getGlobalErrors()) {
            log.trace("ObjectError: {}", ge);
        }
        for (FieldError fe : bindingResult.getFieldErrors()) {
            model.addAttribute(fe.getField() + "_error", true);
            log.trace("FieldError: {}", fe);
        }
        return "team/new";
    }
    try {
        id = teamFacade.createTeam(formBean);
        redirectAttributes.addFlashAttribute("alert_success",
                "Team: " + teamFacade.getTeamById(id) + " was created");
    } catch (Exception ex) {
        log.warn("cannot Create a team {}");
        redirectAttributes.addFlashAttribute("alert_danger",
                "This team cannot be created because it has already exited in La Liga now.");
    }
    return "redirect:" + uriBuilder.path("/team/list").buildAndExpand(id).encode().toUriString();
}

From source file:cz.muni.fi.pa165.mvc.controllers.TeamController.java

@RequestMapping(value = "/edit", method = RequestMethod.POST)
public String edit(@Valid @ModelAttribute("teamEdit") TeamDTO formBean, BindingResult bindingResult,
        Model model, RedirectAttributes redirectAttributes, UriComponentsBuilder uriBuilder) {
    log.debug("edit(teamEdit={})", formBean);
    if (bindingResult.hasErrors()) {
        log.debug("some errror");
        for (ObjectError ge : bindingResult.getGlobalErrors()) {
            log.trace("ObjectError: {}", ge);

        }/*from ww  w.  ja v  a 2s  .  c o m*/
        for (FieldError fe : bindingResult.getFieldErrors()) {
            model.addAttribute(fe.getField() + "_error", true);
            log.trace("FieldError: {}", fe);
        }
        return "team/edit";

    }
    try {
        teamFacade.updateTeam(formBean);
        redirectAttributes.addFlashAttribute("alert_success", "Team was edited");
    } catch (Exception ex) {
        log.warn("cannot edit this team {}");
        redirectAttributes.addFlashAttribute("alert_danger",
                "Team's name has already exited or any errors happen, please check again!");
    }
    return "redirect:" + uriBuilder.path("/team/list").toUriString();

}

From source file:cz.muni.fi.dndtroopsweb.controllers.TroopController.java

/**
 * Creates troop based on form provided/*from   w  w w .ja  va  2  s .  c om*/
 *
 * @param formBean troop to be created
 * @return details of newly created troop
 */
@RequestMapping(value = "/create", method = RequestMethod.POST)
public String create(@Valid @ModelAttribute("troopCreate") TroopCreateDTO formBean, BindingResult bindingResult,
        Model model, RedirectAttributes redirectAttributes, UriComponentsBuilder uriBuilder) {
    log.debug("create(formBean={})", formBean);
    //in case of validation error forward back to the the form
    if (bindingResult.hasErrors()) {
        for (ObjectError ge : bindingResult.getGlobalErrors()) {
            log.trace("ObjectError: {}", ge);
        }
        for (FieldError fe : bindingResult.getFieldErrors()) {
            model.addAttribute(fe.getField() + "_error", true);
            log.trace("FieldError: {}", fe);
        }
        return "troop/new";
    }

    Long id = troopFacade.createTroop(formBean);

    redirectAttributes.addFlashAttribute("alert_success", "Troop " + id + " was created");
    return "redirect:" + uriBuilder.path("/troop/list").toUriString();
}

From source file:cz.muni.fi.dndtroopsweb.controllers.TroopController.java

/**
 * Changes money based on form for chosen troop
 *
 * @param formBean troop to be modified/*from  w  ww .  ja  v a2  s .  co m*/
 * @return details of modified troop
 */
@RequestMapping(value = "/cash/{id}", method = RequestMethod.POST)
public String cash(@PathVariable long id, @Valid @ModelAttribute("troop") TroopDTO formBean,
        BindingResult bindingResult, Model model, RedirectAttributes redirectAttributes,
        UriComponentsBuilder uriBuilder) {
    log.debug("update money(troop={})", formBean);
    //in case of validation error forward back to the the form
    if (bindingResult.hasErrors()) {
        for (ObjectError ge : bindingResult.getGlobalErrors()) {
            log.trace("ObjectError: {}", ge);
        }
        for (FieldError fe : bindingResult.getFieldErrors()) {
            model.addAttribute(fe.getField() + "_error", true);
            log.trace("FieldError: {}", fe);
        }
        return "troop/money";
    }
    troopFacade.changeMoney(id, formBean.getMoney());
    //Long id = formBean.getId();
    redirectAttributes.addFlashAttribute("alert_success", "Money updated");
    return "redirect:" + uriBuilder.path("/troop/details/{id}").buildAndExpand(id).encode().toUriString();
}

From source file:cz.muni.fi.dndtroopsweb.controllers.TroopController.java

/**
 * Changes mission based on form for chosen troop
 *
 * @param formBean troop to be modified//from  w w w.j a v  a2 s .  c  o m
 * @return details of modified troop
 */
@RequestMapping(value = "/job/{id}", method = RequestMethod.POST)
public String job(@Valid @ModelAttribute("hero") TroopDTO formBean, BindingResult bindingResult, Model model,
        RedirectAttributes redirectAttributes, UriComponentsBuilder uriBuilder) {
    log.debug("update mission(troop={})", formBean);
    //in case of validation error forward back to the the form
    if (bindingResult.hasErrors()) {
        for (ObjectError ge : bindingResult.getGlobalErrors()) {
            log.trace("ObjectError: {}", ge);
        }
        for (FieldError fe : bindingResult.getFieldErrors()) {
            model.addAttribute(fe.getField() + "_error", true);
            log.trace("FieldError: {}", fe);
        }
        return "troop/mission";
    }
    troopFacade.changeMission(formBean.getId(), formBean.getMission());
    Long id = formBean.getId();
    redirectAttributes.addFlashAttribute("alert_success", "Mission updated");
    return "redirect:" + uriBuilder.path("/troop/details/{id}").buildAndExpand(id).encode().toUriString();
}

From source file:cz.muni.fi.pa165.mvc.controllers.GameController.java

@RequestMapping(value = "/edit", method = RequestMethod.POST)
public String edit(@Valid @ModelAttribute("gameEdit") GameCreateDTO formBean, BindingResult bindingResult,
        Model model, RedirectAttributes redirectAttributes, UriComponentsBuilder uriBuilder) {
    log.debug("edit(gameEdit={})", formBean);
    //in case of validation error forward back to the the form
    if (bindingResult.hasErrors()) {
        log.debug("some errror");
        for (ObjectError ge : bindingResult.getGlobalErrors()) {
            log.trace("ObjectError: {}", ge);

        }//from  w  w  w .  j a  va  2  s  .  co  m
        for (FieldError fe : bindingResult.getFieldErrors()) {
            model.addAttribute(fe.getField() + "_error", true);
            log.trace("FieldError: {}", fe);
        }
        return "game/edit";

    }

    if (formBean.getGuestTeam().equals(formBean.getHomeTeam())) {
        redirectAttributes.addFlashAttribute("alert_warning",
                "Game editing failed - two same teams were selected!");
        return "redirect:" + uriBuilder.path("/game/list").build().encode().toUriString();
    }

    GameDTO g = gameFacade.findById(formBean.getId());
    g.setDateOfGame(formBean.getDateOfGame());
    g.setGuestTeam(teamFacade.getTeamById(formBean.getGuestTeam()));
    g.setHomeTeam(teamFacade.getTeamById(formBean.getHomeTeam()));

    gameFacade.update(g);

    //report success
    redirectAttributes.addFlashAttribute("alert_success", "Player was edited");
    return "redirect:" + uriBuilder.path("/game/list").toUriString();

}

From source file:cz.muni.fi.pa165.mvc.controllers.GameController.java

@RequestMapping(value = "/create", method = RequestMethod.POST)
public String create(@Valid @ModelAttribute("playerCreate") GameCreateDTO formBean, BindingResult bindingResult,
        Model model, RedirectAttributes redirectAttributes, UriComponentsBuilder uriBuilder) {
    log.debug("create(playerCreate={})", formBean);
    //in case of validation error forward back to the the form
    if (bindingResult.hasErrors()) {
        log.debug("some errror");
        for (ObjectError ge : bindingResult.getGlobalErrors()) {
            log.trace("ObjectError: {}", ge);

        }/*from ww  w.ja  v  a2  s .  c  om*/
        for (FieldError fe : bindingResult.getFieldErrors()) {
            model.addAttribute(fe.getField() + "_error", true);
            log.trace("FieldError: {}", fe);
        }
        return "game/new";

    }

    //create game

    if (formBean.getGuestTeam().equals(formBean.getHomeTeam())) {
        redirectAttributes.addFlashAttribute("alert_warning",
                "Game creation failed - two same teams were selected!");
        return "redirect:" + uriBuilder.path("/game/list").build().encode().toUriString();
    }

    Long id = gameFacade.create(formBean);

    //report success
    redirectAttributes.addFlashAttribute("alert_success", "Match " + id + " was created");
    return "redirect:" + uriBuilder.path("/game/list").buildAndExpand(id).encode().toUriString();
}

From source file:cz.muni.fi.dndtroopsweb.controllers.HeroController.java

/**
 * Changes hero's role/*www  .ja  v a  2s .  c om*/
 *
 * @param formBean hero whose role will be changed
 * @return Details of hero whose role has been changed
 */
@RequestMapping(value = "/change/{id}", method = RequestMethod.POST)
public String change(@PathVariable long id, @Valid @ModelAttribute("hero") HeroDTO formBean,
        BindingResult bindingResult, Model model, RedirectAttributes redirectAttributes,
        UriComponentsBuilder uriBuilder) {
    log.debug("change role(hero={})", formBean);
    //in case of validation error forward back to the the form
    if (bindingResult.hasErrors()) {
        for (ObjectError ge : bindingResult.getGlobalErrors()) {
            log.trace("ObjectError: {}", ge);
        }
        for (FieldError fe : bindingResult.getFieldErrors()) {
            model.addAttribute(fe.getField() + "_error", true);
            log.trace("FieldError: {}", fe);
        }
        return "hero/role";
    }
    heroFacade.changeRole(id, formBean.getRole());
    redirectAttributes.addFlashAttribute("alert_success", "Role was changed");
    return "redirect:" + uriBuilder.path("/hero/details/{id}").buildAndExpand(id).encode().toUriString();
}

From source file:cz.muni.fi.dndtroopsweb.controllers.HeroController.java

/**
 * Creates hero based on filled form//from ww w .  j  a  va 2 s.  c  o m
 *
 * @param formBean hero to be created
 * @return Details of created hero
 */
@RequestMapping(value = "/create", method = RequestMethod.POST)
public String create(@Valid @ModelAttribute("heroCreate") HeroCreateDTO formBean, BindingResult bindingResult,
        Model model, RedirectAttributes redirectAttributes, UriComponentsBuilder uriBuilder) {
    log.debug("create(heroCreate={})", formBean);
    //in case of validation error forward back to the the form
    if (bindingResult.hasErrors()) {
        for (ObjectError ge : bindingResult.getGlobalErrors()) {
            log.trace("ObjectError: {}", ge);
        }
        for (FieldError fe : bindingResult.getFieldErrors()) {
            model.addAttribute(fe.getField() + "_error", true);
            log.trace("FieldError: {}", fe);
        }
        return "hero/new";
    }
    Long id = heroFacade.createHero(formBean);
    redirectAttributes.addFlashAttribute("alert_success", "Hero " + id + " was created");
    return "redirect:" + uriBuilder.path("/hero/details/{id}").buildAndExpand(id).encode().toUriString();
}

From source file:cz.muni.fi.dndtroopsweb.controllers.HeroController.java

/**
 * Insert hero into troop//  w  w w. j  a v  a2s  . co m
 *
 * @param formBean hero to be added into troop
 * @return Details of hero who has been inserted
 */
@RequestMapping(value = "/add/{id}", method = RequestMethod.POST)
public String add(@PathVariable long id, @Valid @ModelAttribute("hero") HeroDTO formBean,
        BindingResult bindingResult, Model model, RedirectAttributes redirectAttributes,
        UriComponentsBuilder uriBuilder) {
    log.debug("add troop(hero={})", formBean);
    //in case of validation error forward back to the the form
    if (bindingResult.hasErrors()) {
        for (ObjectError ge : bindingResult.getGlobalErrors()) {
            log.trace("ObjectError: {}", ge);
        }
        for (FieldError fe : bindingResult.getFieldErrors()) {
            model.addAttribute(fe.getField() + "_error", true);
            log.trace("FieldError: {}", fe);
        }
        return "hero/troop";
    }
    if (heroFacade.getHeroWithId(id).getTroop() != null) {
        redirectAttributes.addFlashAttribute("alert_warning",
                "Hero \"" + heroFacade.getHeroWithId(id).getName() + "\" is already in troop");
        return "redirect:" + uriBuilder.path("/hero/details/" + id).toUriString();
    }
    heroFacade.addTroop(id, formBean.getId());
    redirectAttributes.addFlashAttribute("alert_success", "Hero added into Troop");
    return "redirect:" + uriBuilder.path("/hero/details/{id}").buildAndExpand(id).encode().toUriString();
}