Example usage for org.springframework.validation BindingResult reject

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

Introduction

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

Prototype

void reject(String errorCode, String defaultMessage);

Source Link

Document

Register a global error for the entire target object, using the given error description.

Usage

From source file:oauth2.controllers.ChangePasswordController.java

@RequestMapping(method = RequestMethod.POST)
public String submit(HttpServletRequest request, HttpServletResponse response,
        @Validated @ModelAttribute(FORM_ATTRIBUTE_NAME) ChangePasswordForm form, BindingResult result) {

    if (result.hasErrors()) {
        return VIEW_CHANGE_PASSWORD;
    }// w  w  w .  j ava2 s  .c om
    try {
        changePasswordService.changePassword(form.getUserId(), form.getOldPassword(), form.getNewPassword());
    } catch (ChangePasswordException ex) {
        result.rejectValue("userId", ex.getMessage(), ex.getLocalizedMessage());
    } catch (AuthenticationException ex) {
        result.reject(ex.getMessage(), ex.getLocalizedMessage());
    }
    if (result.hasErrors()) {
        form.clearPasswords();
        return VIEW_CHANGE_PASSWORD;
    }
    return "redirect:" + getTargetUrl(request, response);
}

From source file:JavaMvc.Controllers.SecurityController.java

@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login(Model model, @ModelAttribute LoginCommand command, BindingResult errors) {
    loginValidator.validate(command, errors);

    if (errors.hasErrors()) {
        return showLoginForm(model, command);
    }//from ww  w  .  java2  s  . c om
    final Subject currentUser = SecurityUtils.getSubject();

    UsernamePasswordToken token = new UsernamePasswordToken(command.getUsername(), command.getPassword(),
            command.isRememberMe());
    try {
        currentUser.login(token);
    } catch (AuthenticationException e) {
        errors.reject("error.login.generic", "Invalid username or password.  Please try again.");
    }

    if (errors.hasErrors()) {
        return showLoginForm(model, command);
    } else {
        return "redirect:/";
    }
}

From source file:com.amazon.aws.samplecode.travellog.web.TravelLogController.java

@RequestMapping("/saveEntry.do")
@Secured("ROLE_ADMIN")
public ModelAndView doSaveEntry(Entry entry, BindingResult result, ModelMap map) {
    if (entry.getTitle().length() == 0) {
        result.reject("title", "You must enter a title for this entry");
    }//from  w  w w  . j av  a 2 s .c om

    if (result.hasErrors()) {
        doHome(map);
        map.addAttribute("popupScreen", "entry_div");
        return new ModelAndView("home", map);
    }
    Journal journal = dao.getJournal();
    entry.setJournal(journal);

    //Make an initial save to get entry id populated
    dao.saveEntry(entry);

    TravelLogSNSManager sns = new TravelLogSNSManager();
    sns.createTopic(entry);

    //save with arn set
    dao.saveEntry(entry);

    doHome(map);
    return new ModelAndView("redirect:home.do");
}

From source file:com.sws.platform.mobile.security.controller.SecurityController.java

@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login(Model model, @ModelAttribute LoginCommand command, BindingResult errors) {
    loginValidator.validate(command, errors);

    if (errors.hasErrors()) {
        return showLoginForm(model, command);
    }/*from   w ww .jav  a2 s. c  om*/

    UsernamePasswordToken token = new UsernamePasswordToken(command.getUsername(), command.getPassword(),
            command.isRememberMe());
    try {
        logger.info(token.getUsername());
        SecurityUtils.getSubject().login(token);
    } catch (AuthenticationException e) {
        logger.error(e.getMessage());
        errors.reject("error.login.generic", "Invalid username or password.  Please try again.");
    }

    if (errors.hasErrors()) {
        return showLoginForm(model, command);
    } else {
        return "redirect:/";
    }
}

From source file:org.davidmendoza.esu.web.admin.TrimestreController.java

@RequestMapping(value = "/nuevo", method = RequestMethod.POST)
public String nuevo(@ModelAttribute("trimestre") Trimestre trimestre, BindingResult bindingResult,
        RedirectAttributes redirectAttributes, Model model) {
    trimestreValidator.validate(trimestre, bindingResult);
    if (bindingResult.hasErrors()) {
        log.warn("No se pudo crear trimestre. {}", bindingResult.getAllErrors());
        return "admin/trimestre/nuevo";
    }//from   w  w w .  j av a 2  s . com

    try {
        trimestreService.crea(trimestre);
    } catch (Exception e) {
        log.error("No se pudo crear trimestre", e);
        bindingResult.reject("No se pudo crear trimestre. {}", e.getMessage());
        return "admin/trimestre/nuevo";
    }
    return "redirect:/admin/trimestre/ver/" + trimestre.getId();
}

From source file:org.davidmendoza.esu.web.admin.UsuarioController.java

@RequestMapping(value = "/nuevo", method = RequestMethod.POST)
public String nuevo(@ModelAttribute("usuario") Usuario usuario, BindingResult bindingResult,
        RedirectAttributes redirectAttributes, Model model) {
    usuarioValidator.validate(usuario, bindingResult);
    if (bindingResult.hasErrors()) {
        log.warn("No se pudo crear usuario. {}", bindingResult.getAllErrors());
        return "admin/usuario/nuevo";
    }/*from  ww  w .j  av a2 s  .c om*/

    try {
        usuarioService.crea(usuario);
    } catch (Exception e) {
        log.error("No se pudo crear usuario", e);
        bindingResult.reject("No se pudo crear usuario. {}", e.getMessage());
        return "admin/usuario/nuevo";
    }
    return "redirect:/admin/usuario/ver/" + usuario.getId();
}

From source file:org.davidmendoza.esu.web.admin.TrimestreController.java

@RequestMapping(value = "/editar", method = RequestMethod.POST)
public String editar(@ModelAttribute("trimestre") Trimestre trimestre, BindingResult bindingResult,
        RedirectAttributes redirectAttributes, Model model) {
    trimestreValidator.validate(trimestre, bindingResult);
    if (bindingResult.hasErrors()) {
        log.warn("No se pudo actualizar trimestre. {}", bindingResult.getAllErrors());
        return "admin/trimestre/editar";
    }/*  www.  j  a v  a2 s . c o m*/

    try {
        trimestreService.actualiza(trimestre);
    } catch (Exception e) {
        log.error("No se pudo actualizar trimestre", e);
        bindingResult.reject("No se pudo actualizar trimestre. {}", e.getMessage());
        return "admin/trimestre/editar";
    }
    return "redirect:/admin/trimestre/ver/" + trimestre.getId();
}

From source file:com.amazon.aws.samplecode.travellog.web.TravelLogController.java

@RequestMapping("/createAccount.do")
public ModelAndView doCreateAccount(User user, BindingResult result, ModelMap map,
        @RequestParam("password2") String password2) {

    //Verify user info submission
    if (user.getUsername().equals("")) {
        result.reject("username", "Username cannot be blank");
    }//from   www .  ja  v a2 s. c om
    if (user.getPassword().equals("")) {
        result.reject("password", "Password cannot be blank");
    }
    if (!user.getPassword().equals(password2)) {
        result.reject("password", "Passwords do not match");
    }

    if (result.hasErrors()) {
        doHome(map);
        return new ModelAndView("home", map);
    }

    // check to make sure we don't have a user account already
    List<User> users = dao.getUsers();
    if (users.size() > 0) {
        result.reject("username", "The admin user already exists");
        return new ModelAndView("home", map);
    } else {
        dao.saveUser(user);
        map.addAttribute("usercreated", true);
    }

    doHome(map);
    return new ModelAndView("redirect:home.do");

}

From source file:org.davidmendoza.esu.web.admin.UsuarioController.java

@RequestMapping(value = "/editar", method = RequestMethod.POST)
public String editar(@ModelAttribute("perfil") Perfil perfil, BindingResult bindingResult,
        RedirectAttributes redirectAttributes, Model model) {
    perfilValidator.validate(perfil, bindingResult);
    if (bindingResult.hasErrors()) {
        log.warn("No se pudo actualizar usuario. {}", bindingResult.getAllErrors());
        return "admin/usuario/editar";
    }//from   w  w  w  .j a va2  s  .  c o  m
    Usuario usuario = perfil.getUsuario();

    try {
        perfilService.actualiza(perfil);
    } catch (Exception e) {
        log.error("No se pudo actualizar usuario", e);
        bindingResult.reject("No se pudo actualizar usuario. {}", e.getMessage());
        return "admin/usuario/editar";
    }
    return "redirect:/admin/usuario/ver/" + usuario.getId();
}

From source file:com.amazon.aws.samplecode.travellog.web.TravelLogController.java

@RequestMapping("/createJournal.do")
public ModelAndView doCreateJournal(Journal journal, BindingResult result,
        @RequestParam("preload") boolean preload, ModelMap map) {
    //If we're preloading the sample, just skip to the preload method
    if (preload) {
        preloadJournal();/*from  ww  w  . j  a  v a  2s  . c om*/
    } else {
        if (journal.getTitle().length() == 0) {
            result.reject("title", "Title cannot be blank");
        }

        //check to make sure we don't already have a journal
        if (dao.getJournals().size() > 0) {
            result.reject("title", "Journal already exists.  Please reload page.");
        }

        if (!result.hasErrors()) {
            dao.saveJournal(journal);
        }
    }

    doHome(map);
    if (result.hasErrors()) {
        return new ModelAndView("home", map);
    } else {
        return new ModelAndView("redirect:home.do");
    }
}