Presentation is loading. Please wait.

Presentation is loading. Please wait.

Spring MVC form handling annotation example

Similar presentations


Presentation on theme: "Spring MVC form handling annotation example"— Presentation transcript:

1 Spring MVC form handling annotation example

2 Agenda Spring Annotations Spring MVC 3 (2.5)

3 Spring Annotations Spring 2.x Data Access Annotations
Spring 2.x Aspects Spring 2.5 Context Annotations Spring 2.5 Stereotypes Spring 2.5 Factory Annotations JSR-250 javax.annotations Spring 2.5 MVC Annotations Spring 3.0 MVC Additions Spring 3.0 Annotations

4 What's new in Spring 2.5? Annotation-driven dependency injection through annotation and fine-grained auto-wiring control Support for JSR-250 annotations, for dependency injection of a named resource, as well for lifecycle methods. Auto-detection of Spring components that are annotated (or one of several stereotype annotations). An all-new annotation-driven Spring MVC programming model that greatly simplifies Spring web development. A new integration test framework that is based on JUnit 4 and annotations.

5 What's new in Spring 3.0? Full-scale REST support in Spring MVC
New annotations for Spring MVC, Support for declarative validation with JSR-303 (Bean Validation) annotations. Support for the new JSR-330 dependency injection specification. Annotation-oriented declaration of asynchronous and scheduled methods. A new annotation-based configuration model that allows for nearly XML-free Spring configuration. The Object-to-XML (OXM) mapping functionality from the Spring Web Services project has been moved into the core Spring Framework.

6 Spring 2.5 Context Annotations
@Scope Indicates the scope to use for annotated class instances Default == “singleton” Options: Singleton Prototype Web Options: Request Session Global session

7 Spring 2.5 Stereotypes @Component ** @Controller @Repository @Service
Indicates that a class is a component Class is a candidate for auto-detection Custom component extensions @Controller Specialized Component Typically used with RequestMapping annotation Discussed in section on web mvc @Repository 2.0 stereotype… previously mentioned Now an extension @Service Intended to be a business service facade

8 Spring 2.5 Factory Annotations
@Autowired Marks a constructor, field, setter or config method for injection. Fields are injected After construction Before config methods @Autowired(required=false) Config: AutowiredAnnotationBeanPostProcessor @Configurable Marks class as being eligible for Spring-driven configuration Used with AspectJ @Qualifier Qualifies a bean for autowiring May be customized @Required Marks a method as being injection required

9 Types of Injections Constructor Setter Field

10 New Injection Type configuration method with any number of arguments

11 Annotation Call Stack

12 @Configuration and @Bean
Annotating a class with indicates that the class can be used by the Spring IoC container as a source of bean definitions. @Bean is a method-level annotation and a direct analog of the XML <bean/> element.

13 @Configuration and @Bean
Spring <beans/> XML : <beans> <bean id="myService" class="com.acme.services.MyServiceImpl"/> </beans> Bean-annotated : @Configuration public class AppConfig { @Bean public MyService myService() { return new MyServiceImpl(); }

14 @Autowired You can apply annotation to "traditional" setter methods. At run time you will be able to access all methods of the class without worrying about how you got the class. This is known as Dependency Injection.

15 @Autowired public class SimpleMovieLister {
private MovieFinder movieFinder; @Autowired public void setMovieFinder(MovieFinder movieFinder) { this.movieFinder = movieFinder; } // ...

16 Spring MVC 3 (2.5)

17 Spring 2.5 MVC Annotations
@Controller Stereotype used to “Controller” of MVC Scanned for RequestMappings @RequestMapping Annotates a handler method for a request @RequestParam Annotates that a method parameter should be bound to a web request parameter @SessionAttributes Marks session attributes that a handler uses

18 Spring 3 Annotations (New Only)
@Value @RequestHeader @CookieValue @PathVariable @Async @Scheduled @Model @Bound @ActionMapping @EventMapping @RenderMapping @ResourceMapping @ExceptionHandler @Mapping @RequestBody @ResponseBody @ResponseStatus

19 Setting up Spring MVC Spring MVC in our application is to place the following <servlet> declaration in the web.xml file:

20 Spring MVC configuration
Use the spring-context schema as shown in the following XML snippet:

21 Spring MVC configuration (2)
In a Spring MVC application, you may configure a custom ConversionService instance explicity as an attribute of the annotation-driven element of the MVC namespace.

22 SimpleUrlHandlerMapping
In Spring configuration :

23 Handling Annotated Controllers
In Spring configuration : Both are automatically registered by DispatcherServlet

24 Spring MVC namespace New in Spring 3.0:

25 Automatically discovering beans
When you added to your Spring <context:annotation-config> configuration, you told Spring that you want it to honor a certain set of annotations in the beans that you declare and to use those beans to guide bean wiring.

26 Annotating beans for auto-discovery
By default, looks for classes that are annotated <context:component-scan> with one of a handful of special stereotype annotations: @Component - A general-purpose stereotype annotation indicating that the class is a Spring component. @Controller - Indicates that the class defines a Spring MVC controller. @Repository - Indicates that the class defines a data repository. @Service - Indicates that the class defines a service. Any custom annotation that is itself annotated

27 @Controller This annotation support is available for both Servlet MVC and Portlet MVC. Typically used annotation.

28 A simple controller

29 SimpleFormController vs @Controller
In XML-based Spring MVC web application, you create a form controller by extending the SimpleFormController class. In annotation-based, can also use to handle form.

30 SimpleFormController vs @Controller
Annotation public class CustomerController extends SimpleFormController{ //... } @Controller @RequestMapping("/customer.htm") public class CustomerController{ //... }

31 Mapping requests with @RequestMapping
The class-level annotation maps a specific request path (or path pattern) onto a form controller. The method-level annotations narrowing the primary mapping for a specific HTTP method request method ("GET"/"POST") or specific HTTP request parameters.

32 @RequestParam Annotation which indicates that a method parameter should be bound to a web request parameter.

33 Mapping requests By path By HTTP method By presence of query parameter
@RequestMapping(“path”) By HTTP method @RequestMapping(“path”, method=RequestMethod.GET) POST, PUT, DELETE, OPTIONS, and TRACE are are also supported By presence of query parameter @RequestMapping(“path”, method=RequestMethod.GET, params=”foo”) Negation also supported: params={ “foo”, “!bar” }) By presence of request header @RequestMapping(“path”, header=”content-type=text/*”)

34 Obtaining request data
A query parameter value @RequestParam(“name”) A path element value @PathVariable(“var”) A request header value @RequestHeader(“name”) A cookie value @CookieValue(“name”) The request body @RequestBody The request body and any request header HttpEntity<T>

35 formBackingObject() vs RequestMethod.GET
In SimpleFormController, you can initialize the command object for binding in the formBackingObject() method. In annotation-based, you can do the same by annotated the method name = RequestMethod.GET).

36 formBackingObject() vs RequestMethod.GET (cont.)
SimpleFormController @Override protected Object formBackingObject(HttpServletRequest request) throws Exception {   Customer cust = new Customer(); //Make "Spring MVC" as default checked value cust.setFavFramework(new String []{"Spring MVC"});   return cust; }

37 formBackingObject() vs RequestMethod.GET (cont.)
Annotation @RequestMapping(method = RequestMethod.GET) public String initForm(ModelMap model){   Customer cust = new Customer(); //Make "Spring MVC" as default checked value cust.setFavFramework(new String []{"Spring MVC"});   //command object model.addAttribute("customer", cust);   //return form view return "CustomerForm"; }

38 onSubmit() vs RequestMethod.POST
In SimpleFormController, the form submission is handle by the onSubmit() method. In annotation-based, you can do the same by annotated the method name = RequestMethod.POST).

39 onSubmit() vs RequestMethod.POST
SimpleFormController @Override protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception {   Customer customer = (Customer)command; return new ModelAndView("CustomerSuccess");   }

40 onSubmit() vs RequestMethod.POST
Annotation @RequestMapping(method = RequestMethod.POST) public String processSubmit( @ModelAttribute("customer") Customer customer, BindingResult result, SessionStatus status) {   //clear the command object from the session status.setComplete();   //return form success view return "CustomerSuccess";   }

41 referenceData() vs @ModelAttribute
In SimpleFormController, usually you put the reference data in model via referenceData() method, so that the form view can access it. In annotation-based, you can do the same by annotated the method name

42 referenceData() vs @ModelAttribute
SimpleFormController Spring’s form @Override protected Map referenceData(HttpServletRequest request) throws Exception {   Map referenceData = new HashMap();   //Data referencing for web framework checkboxes List<String> webFrameworkList = new ArrayList<String>(); webFrameworkList.add("Spring MVC"); webFrameworkList.add("Struts 1"); webFrameworkList.add("Struts 2"); webFrameworkList.add("JSF"); webFrameworkList.add("Apache Wicket"); referenceData.put("webFrameworkList", webFrameworkList);   return referenceData; } <form:checkboxes items=“${webFrameworkList}" path="favFramework" />

43 referenceData() vs @ModelAttribute
Annotation Spring’s form @ModelAttribute("webFrameworkList") public List<String> populateWebFrameworkList() {   //Data referencing for web framework checkboxes List<String> webFrameworkList = new ArrayList<String>(); webFrameworkList.add("Spring MVC"); webFrameworkList.add("Struts 1"); webFrameworkList.add("Struts 2"); webFrameworkList.add("JSF"); webFrameworkList.add("Apache Wicket");   return webFrameworkList; } <form:checkboxes items=“${webFrameworkList}" path="favFramework" />

44 initBinder() vs @InitBinder
In SimpleFormController, you define the binding or register the custom property editor via initBinder() method. In annotation-based, you can do the same by annotated the method name

45 initBinder() vs @InitBinder
SimpleFormController Annotation protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {   SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); } @InitBinder public void initBinder(WebDataBinder binder) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");   binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); }

46 From Validation In SimpleFormController, you have to register and map the validator class to the controller class via XML bean configuration file, and the validation checking and work flows will be executed automatically. In annotation-based, you have to explicitly execute the validator and define the validation flow in class manually.

47 From Validation SimpleFormController
<bean class="com.mkyong.customer.controller.CustomerController"> <property name="formView" value="CustomerForm" /> <property name="successView" value="CustomerSuccess" />   <!-- Map a validator --> <property name="validator"> <bean class="com.mkyong.customer.validator.CustomerValidator" /> </property> </bean>

48 From Validation Annotation @Controller
@RequestMapping("/customer.htm") public class CustomerController{   CustomerValidator customerValidator;   @Autowired public CustomerController(CustomerValidator customerValidator){ this.customerValidator = customerValidator; }   @RequestMapping(method = RequestMethod.POST) public String processSubmit( @ModelAttribute("customer") Customer customer, BindingResult result, SessionStatus status) {   customerValidator.validate(customer, result);   if (result.hasErrors()) { //if validator failed return "CustomerForm"; } else { status.setComplete(); //form success return "CustomerSuccess"; } } //...

49 Working with Session The annotation declares session attributes used by a specific handler.

50 More Crazy Stuff Annotation Access to: Headers Cookies

51 Reference Spring MVC form handling annotation example


Download ppt "Spring MVC form handling annotation example"

Similar presentations


Ads by Google