Example usage for org.springframework.validation BindingResult hasErrors

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

Introduction

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

Prototype

boolean hasErrors();

Source Link

Document

Return if there were any errors.

Usage

From source file:com.mascova.caliga.controller.UserController.java

@RequestMapping(value = "/profile", method = RequestMethod.POST)
public String profileSave(@Valid UserForm userForm, BindingResult result, Model model) {
    if (result.hasErrors()) {
        model.addAttribute("userForm", userForm);
        model.addAttribute("auths", authorityService.getAll());
        return "user/user-profile";
    }//from   w  w  w.ja  v  a2 s .co  m
    User user = userForm.bindToModel(new User());
    Set<Authority> authorityList = authorityService.findByUserId(user.getLogin());
    user.setAuthorities(authorityList);
    userService.saveOrUpdate(user);

    model.addAttribute("statusMessageKey", new StringBuilder("User created: ").append(user.getFirstName())
            .append(" ").append(user.getLastName()).toString());
    return "user/user-profile";
}

From source file:com.puglieseweb.app.web.templates.components.PurchaseFormComponent.java

@RequestMapping(value = "/purchase", method = RequestMethod.POST)
public String handleSubmit(@Valid PurchaseForm form, BindingResult result, HttpSession session) {

    if (result.hasErrors()) {
        return "components/purchaseForm.jsp";
    }//from   w ww . j  a  va  2  s.com

    ShoppingCart shoppingCart = ShoppingCart.getShoppingCart(session);

    List<OrderRow> rows = new ArrayList<OrderRow>();
    for (ShoppingCartItem cartItem : shoppingCart.getItems()) {
        rows.add(new OrderRow(cartItem.getProduct().getArticleCode(), cartItem.getQuantity()));
    }

    Customer customer = new Customer();
    customer.setFirstName(form.getFirstName());
    customer.setLastName(form.getLastName());
    customer.setAddressLine1(form.getAddressLine1());
    customer.setAddressLine2(form.getAddressLine2());
    customer.setPhoneNumber(form.getPhoneNumber());
    customer.setEmail(form.getEmail());

    Order order = new Order(customer, rows);

    salesApplicationWebService.placeOrder(order);

    shoppingCart.clear();

    return "components/purchaseFormSubmitted.jsp";
}

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 w  w. jav  a2 s. co m

    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:controller.DepartController.java

@RequestMapping(params = "btnInsert")
public String insertdp(ModelMap model, @Validated @ModelAttribute("depart") Depart depart,
        BindingResult errors) {
    if (errors.hasErrors()) {
        model.addAttribute("message", "Vui lng sa cc li sau y !");
    } else {//w  ww  .  j  a v a  2  s  .com
        Session session = factory.openSession();
        Transaction t = session.beginTransaction();
        try {
            session.save(depart);
            t.commit();
            model.addAttribute("message", "Thm mi thnh cng !");
        } catch (Exception e) {
            t.rollback();
            model.addAttribute("message", "Thm mi tht bi !");
        } finally {
            session.close();
        }

    }
    model.addAttribute("departs", getDeparts());
    return "admin/depart";
}

From source file:controller.DepartController.java

@RequestMapping(params = "btnUpdate")
public String updatedp(ModelMap model, @Validated @ModelAttribute("depart") Depart depart,
        BindingResult errors) {
    if (errors.hasErrors()) {
        model.addAttribute("message", "Vui lng sa cc li sau y !");
    } else {/*from   w w w. j  a  v a  2  s . com*/
        Session session = factory.openSession();
        Transaction t = session.beginTransaction();
        try {
            session.update(depart);
            t.commit();
            model.addAttribute("message", "Cp nht thnh cng !");
        } catch (Exception e) {
            t.rollback();
            model.addAttribute("message", "Cp nht tht bi !");
        } finally {
            session.close();
        }

    }
    model.addAttribute("departs", getDeparts());
    return "admin/depart";
}

From source file:info.harmia.polyglot.springapp.mvc.web.controller.EmployeeController.java

@RequestMapping(value = "/employees/add", method = RequestMethod.POST)
public String addEmployee(@ModelAttribute("employee") @Valid EmployeeForm employeeForm, BindingResult result,
        ModelMap model, HttpSession session) {
    if (result.hasErrors()) {
        FlashMessage.createErrorMessage(session, "employees.add.error", employeeForm.getLastName(),
                employeeForm.getFirstName());
        return listEmployees(employeeForm, model, session);
    }/*from   w w w  . ja v  a  2s.co  m*/
    employeeService.addEmployee(employeeForm);
    FlashMessage.createSuccessMessage(session, "employees.add.success", employeeForm.getLastName(),
            employeeForm.getFirstName());
    return "redirect:/employees/";
}

From source file:info.rmapproject.webapp.controllers.SearchController.java

/**
 * Processes the POSTed search form.//from   w ww. j a  v  a 2s .  c o  m
 *
 * @param search the search command
 * @param result the form results
 * @return the resource page
 * @throws Exception the exception
 */
@RequestMapping(method = RequestMethod.POST)
public String searchResults(@Valid @ModelAttribute("search") SearchCommand search, BindingResult result,
        Model model) throws Exception {
    if (result.hasErrors()) {
        model.addAttribute("search", search);
        model.addAttribute("notice", "There was an error in your search entry.");
        return "search";
    }

    String resourceUri = search.getSearch().trim();
    return "redirect:/resources/" + URLEncoder.encode(resourceUri, "UTF-8");
}

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 .  j  a v a  2  s. co m*/
    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:sample.web.cart.CartController.java

@RequestMapping(value = "", method = RequestMethod.POST)
public String updateAll(@Validated CartForm cartForm, BindingResult result, Model model) {
    if (result.hasErrors()) {
        model.addAttribute("cart", cart);
        model.addAttribute("cartForm", cartForm);
        return "cart/list";
    }/*from  w  w  w .  j a va 2s. c o  m*/
    for (Map.Entry<String, CartItemForm> entry : cartForm.getItems().entrySet()) {
        String itemId = entry.getKey();
        CartItem cartItem = cart.getCartItem(itemId);
        if (cartItem == null) {
            continue;
        }
        CartItemForm cartItemForm = entry.getValue();
        if (cartItemForm.getQuantity() < 1) {
            cart.removeItemById(itemId);
        } else {
            cart.setQuantityByItemId(itemId, cartItemForm.getQuantity());
        }
    }
    return "redirect:/cart";
}

From source file:gallery.web.controller.pages.types.EMailSendType.java

@Override
public UrlBean execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
    SendEmail command = new SendEmail();
    BindingResult res = validator.bindAndValidate(command, request);
    if (res.hasErrors()) {
        request.setAttribute(res.MODEL_KEY_PREFIX + config.getContentDataAttribute(), res);
        common.CommonAttributes.addErrorMessage("form_errors", request);
    } else {/*ww  w.j  av a 2s. c  o  m*/
        command.setEmail_to(email_adresses);
        mail_service.postMail(gallery.web.Config.SITE_NAME + ": " + command.getEmail_from(), command);
        common.CommonAttributes.addHelpMessage("operation_succeed", request);
    }
    request.setAttribute(config.getContentDataAttribute(), command);

    UrlBean url = new UrlBean();
    url.setContent(contentUrl);
    return url;
}