BVB Source Codes

spring-mvc-showcase Show FormController.java Source code

Return Download spring-mvc-showcase: download FormController.java Source code - Download spring-mvc-showcase Source code - Type:.java
  1. package org.springframework.samples.mvc.form;
  2.  
  3. import javax.validation.Valid;
  4.  
  5. import org.springframework.mvc.extensions.ajax.AjaxUtils;
  6. import org.springframework.stereotype.Controller;
  7. import org.springframework.ui.Model;
  8. import org.springframework.validation.BindingResult;
  9. import org.springframework.web.bind.annotation.ModelAttribute;
  10. import org.springframework.web.bind.annotation.RequestMapping;
  11. import org.springframework.web.bind.annotation.RequestMethod;
  12. import org.springframework.web.bind.annotation.SessionAttributes;
  13. import org.springframework.web.context.request.WebRequest;
  14. import org.springframework.web.servlet.mvc.support.RedirectAttributes;
  15.  
  16. @Controller
  17. @RequestMapping("/form")
  18. @SessionAttributes("formBean")
  19. public class FormController {
  20.  
  21.         // Invoked on every request
  22.  
  23.         @ModelAttribute
  24.         public void ajaxAttribute(WebRequest request, Model model) {
  25.                 model.addAttribute("ajaxRequest", AjaxUtils.isAjaxRequest(request));
  26.         }
  27.  
  28.         // Invoked initially to create the "form" attribute
  29.         // Once created the "form" attribute comes from the HTTP session (see @SessionAttributes)
  30.  
  31.         @ModelAttribute("formBean")
  32.         public FormBean createFormBean() {
  33.                 return new FormBean();
  34.         }
  35.        
  36.         @RequestMapping(method=RequestMethod.GET)
  37.         public void form() {
  38.         }
  39.  
  40.         @RequestMapping(method=RequestMethod.POST)
  41.         public String processSubmit(@Valid FormBean formBean, BindingResult result,
  42.                                                                 @ModelAttribute("ajaxRequest") boolean ajaxRequest,
  43.                                                                 Model model, RedirectAttributes redirectAttrs) {
  44.                 if (result.hasErrors()) {
  45.                         return null;
  46.                 }
  47.                 // Typically you would save to a db and clear the "form" attribute from the session
  48.                 // via SessionStatus.setCompleted(). For the demo we leave it in the session.
  49.                 String message = "Form submitted successfully.  Bound " + formBean;
  50.                 // Success response handling
  51.                 if (ajaxRequest) {
  52.                         // prepare model for rendering success message in this request
  53.                         model.addAttribute("message", message);
  54.                         return null;
  55.                 } else {
  56.                         // store a success message for rendering on the next request after redirect
  57.                         // redirect back to the form to render the success message along with newly bound values
  58.                         redirectAttrs.addFlashAttribute("message", message);
  59.                         return "redirect:/form";                       
  60.                 }
  61.         }
  62.        
  63. }
  64.  
downloadFormController.java Source code - Download spring-mvc-showcase Source code
Related Source Codes/Software:
tushare - TuShare is a utility for crawling historical data ... 2017-01-12
raml-spec - RAML Specification http://raml.... 2017-01-12
android-stackblur - Android StackBlur is a library that can perform a ... 2017-01-12
sound-redux - A Soundcloud client built with React / Redux ... 2017-01-12
httpstat - curl statistics made simple 2017-01-12
discover-flask - Full Stack Web Development with Flask. ... 2017-01-12
micro - Async HTTP microservices made easy ... 2017-01-12
gobot - Golang framework for robotics, drones, and the Int... 2017-01-11
mas - 2017-01-13
Zappa - Serverless Python Web Services ... 2017-01-12
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top