Spring MVC form handling annotation example

Slides:



Advertisements
Similar presentations
Web Development with Karsten Schulz Terp-Nielsen Master Principal Sales Consultant Oracle Denmark.
Advertisements

Introduction to Spring Continued Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack.
Beginning Spring MVC Spencer Uresk. Notes This is a training, NOT a presentation Please ask questions This is being recorded
Spring MVC Part 2 Spencer Uresk. Notes This is a training, NOT a presentation Please ask questions This is being recorded
Intermediate Spring Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack – Basic.
Margus Hanni, Nortal AS Spring MVC Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov.
Introduction to Spring Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack – Basic.
Intermediate Spring Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack – Basic.
Spring MVC Margus Hanni, Nortal AS
Struts Basics SSE USTC Qing Ding. Agenda What is and Why Struts? Struts architecture – Controller: Focus of this presentation – Model – View Struts tag.
1 November 21st 2009 Shaun Abram An Introduction to Spring.
Stateful Web Application Development with Spring Web Flow John Case Senior Consultant Centare Group, Ltd.
Introduction to Spring Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack – Basic.
Spring Framework Benefits…
UNIT-V The MVC architecture and Struts Framework.
Lecture 2 - Struts ENTERPRISE JAVA. 2 Contents  Servlet Deployment  Servlet Filters  Model View Controllers  Struts  Dependency Injection.
SPRING Framework Marcin Sztec.
Intermediate Spring Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack – Basic.
Spring Overview, Application demo -Midhila Paineni 09/23/2011 Spring Overview, Application demo9/8/20151.
Separation of Concerns Technion – Institute of Technology Author: Gal Lalouche - Technion 2015 © 1.
Overview of Previous Lesson(s) Over View  ASP.NET Pages  Modular in nature and divided into the core sections  Page directives  Code Section  Page.
ASP.NET Web API Udaiappa Ramachandran NHDN-Nashua.NET/Cloud Computing UG Lead Blog:
Copyright © 2012 Accenture All Rights Reserved.Copyright © 2012 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are.
JDeveloper 10g and JavaServer Faces: High-Performance UIs on the Web Avrom Roy-Faderman Senior Programmer May, 2006.
JavaServer Faces Jeff Schmitt October 5, Introduction to JSF Presents a standard framework for building presentation tiers for web applications.
Parsley Introduction Kui Huang Oct. 13, Topics Background Dependency Injection Object Lifecycle Message Bus Sample FW Extensions.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Understanding the Bean Lifecycle An.
Spring core v3.x Prepared by: Nhan Le. History v3.0 Spring Expression Language Java based bean metadata v3.1 Cache Abstraction Bean Definition Profile.
© 2008 IBM Session ID: D19 Session Title: Annotated Portal Development with RAD and Spring Speaker(s): Ken Sipe, Technology Director, Perficient Peter.
Standalone Java Application vs. Java Web Application
Rendering Contexts and Components What is a uPortal3 context ? –Defines all aspects of a traditional portal instance Design, navigation, profiles Parameter.
PicoContainer Presented by: Jim O’Hara Ed Kausmeyer Jingming Zhang.
Anti Orgla, Nortal AS Spring Framework
Struts J2EE web application framework “ Model 2 ” Model View Controller Controller Servlet Key features XML metadata Struts taglib Simplified form validation.
Introduction to Spring Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack – Basic.
Chapter 3 Servlet Basics. 1.Recall the Servlet Role 2.Basic Servlet Structure 3.A simple servlet that generates plain text 4.A servlet that generates.
Creative Commons Attribution- NonCommercial-ShareAlike 2.5 License Sakai Programmer's Café Sakai Montreal CRIM Workshop Introduction to Spring Framework,
Fall 2007cs4201 Advanced Java Programming Umar Kalim Dept. of Communication Systems Engineering
Opus College – Techniques Spring Annotated Controllers.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Forms with Spring MVC Handling Form.
Chính phủ điện tử TS. Phạm Văn Tính Khoa CNTT, ĐH Nông Lâm TP.HCM
Intermediate Spring Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack – Basic.
INTEGRATION OF BACKBONE.JS WITH SPRING 3.1. Agenda New Features and Enhancements in Spring 3.1 What is Backbone.js and why I should use it Spring 3.1.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Spring MVC Essentials Getting started.
Spring and DWR Frameworks for Rich Web Enterprise Application Thomas Wiradikusuma Presentation to the 20 th.
1 Spring Framework April, 2012 Lam Ho Lam To. © 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 2 1.Spring Overview 2.Framework.
13 Copyright © 2004, Oracle. All rights reserved. Adding Validation and Error Handling.
11 Copyright © 2004, Oracle. All rights reserved. Customizing Actions.
Herzog August Bibliothek Wolfenbüttel Backend, Service, Listener VuFind's new SOLR connection Originally Presented By David Maus Herzog August Bibliothek.
HTTP protocol Java Servlets. HTTP protocol Web system communicates with end-user via HTTP protocol HTTP protocol methods: GET, POST, HEAD, PUT, OPTIONS,
2006/2007 Licence Apache 2.0 Castle.Igloo. Castle Igloo Basics Pre-require Concept Scopes PageFlow Configuration Controller View Exemple Castle.Igloo.
The NGS Grid Portal David Meredith NGS + Grid Technology Group, e-Science Centre, Daresbury Laboratory, UK
12 Copyright © 2004, Oracle. All rights reserved. Using ADF Struts Components.
Creative Commons Attribution- NonCommercial-ShareAlike 2.5 License Sakai Programmer's Café Sakai Montreal CRIM Workshop Introduction to Spring Framework.
CS520 Web Programming Spring – Web MVC Chengyu Sun California State University, Los Angeles.
CS520 Web Programming Spring – Web MVC Chengyu Sun California State University, Los Angeles.
CS520 Web Programming Spring – Inversion of Control Chengyu Sun California State University, Los Angeles.
Annotation-Based Spring Portlet MVC John A. Lewis Chief Software Architect Unicon, Inc. Jasig 2010 Conference 8 March 2010 © Copyright Unicon, Inc., 2010.
J2EE Lecture 6: Spring – IoC and Dependency Injection
Spring Boot Introduction
CS520 Web Programming Spring – Inversion of Control
J2EE Lecture 7: Spring – Spring MVC
Spring Cloud Netflix Sinisha Mihajlovski.
CS5220 Advanced Topics in Web Programming Spring – Web MVC
Spring MVC Part 2 Spencer Uresk.
CS5220 Advanced Topics in Web Programming Spring – Web MVC
Chengyu Sun California State University, Los Angeles
CS5220 Advanced Topics in Web Programming Angular – Services
Chengyu Sun California State University, Los Angeles
Presentation transcript:

Spring MVC form handling annotation example

Agenda Spring Annotations Spring MVC 3 (2.5)

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

What's new in Spring 2.5? Annotation-driven dependency injection through the @Autowired annotation and fine-grained auto-wiring control with @Qualifier. Support for JSR-250 annotations, including @Resource for dependency injection of a named resource, as well as @PostConstruct and @PreDestroy for lifecycle methods. Auto-detection of Spring components that are annotated with @Component (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.

What's new in Spring 3.0? Full-scale REST support in Spring MVC New annotations for Spring MVC, including @CookieValue and @RequestHeader. 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.

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

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 of @Component @Service Intended to be a business service facade

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

Types of Injections Constructor Setter Field

New Injection Type configuration method with any number of arguments

Annotation Call Stack

@Configuration and @Bean Annotating a class with the @Configuration 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.

@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(); }

@Autowired You can apply the @Autowired 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.

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

Spring MVC 3 (2.5)

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

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

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

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

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.

SimpleUrlHandlerMapping In Spring configuration :

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

Spring MVC namespace New in Spring 3.0:

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.

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 with @Component

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

A simple controller

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

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

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.

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

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/*”)

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>

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 with @RequestMapping(method = RequestMethod.GET).

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; }

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"; }

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 with @RequestMapping(method = RequestMethod.POST).

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");   }

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";   }

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 with @ModelAttribute.

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" />

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" />

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 with @InitBinder.

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)); }

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 the @Controller class manually.

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>

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"; } } //...

Working with Session The type-level @SessionAttributes annotation declares session attributes used by a specific handler.

More Crazy Stuff Annotation Access to: Headers Cookies

Reference Spring MVC form handling annotation example http://www.mkyong.com/spring-mvc/spring-mvc-form-handling-annotation-example/