spring mvc form handling annotation example. agenda spring annotations spring mvc 3 (2.5)

51
Spring MVC form handling annotation example

Upload: shonda-boone

Post on 21-Dec-2015

278 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Spring MVC form handling annotation example. Agenda Spring Annotations Spring MVC 3 (2.5)

Spring MVC form handling annotation example

Page 2: Spring MVC form handling annotation example. Agenda Spring Annotations Spring MVC 3 (2.5)

Agenda

Spring AnnotationsSpring MVC 3 (2.5)

Page 3: Spring MVC form handling annotation example. Agenda Spring Annotations Spring MVC 3 (2.5)

Spring Annotations

Spring 2.x Data Access AnnotationsSpring 2.x AspectsSpring 2.5 Context AnnotationsSpring 2.5 StereotypesSpring 2.5 Factory AnnotationsJSR-250 javax.annotationsSpring 2.5 MVC AnnotationsSpring 3.0 MVC AdditionsSpring 3.0 Annotations

Page 4: Spring MVC form handling annotation example. Agenda Spring Annotations Spring MVC 3 (2.5)

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.

Page 5: Spring MVC form handling annotation example. Agenda Spring Annotations Spring MVC 3 (2.5)

What's new in Spring 3.0?

Full-scale REST support in Spring MVC New annotations for Spring MVC, including @CookieVal

ue and @RequestHeader. Support for declarative validation with JSR-303 (Bean V

alidation) annotations. Support for the new JSR-330 dependency injection speci

fication. Annotation-oriented declaration of asynchronous and sch

eduled methods. A new annotation-based configuration model that allows

for nearly XML-free Spring configuration. The Object-to-XML (OXM) mapping functionality from th

e Spring Web Services project has been moved into the core Spring Framework.

Page 6: Spring MVC form handling annotation example. Agenda Spring Annotations Spring MVC 3 (2.5)

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

Page 7: Spring MVC form handling annotation example. Agenda Spring Annotations Spring MVC 3 (2.5)

Spring 2.5 Stereotypes

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

Page 8: Spring MVC form handling annotation example. Agenda Spring Annotations Spring MVC 3 (2.5)

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

Page 9: Spring MVC form handling annotation example. Agenda Spring Annotations Spring MVC 3 (2.5)

Types of Injections

Constructor

Setter

Field

Page 10: Spring MVC form handling annotation example. Agenda Spring Annotations Spring MVC 3 (2.5)

New Injection Type

configuration method

with any number of arguments

Page 11: Spring MVC form handling annotation example. Agenda Spring Annotations Spring MVC 3 (2.5)

Annotation Call Stack

Page 12: Spring MVC form handling annotation example. Agenda Spring Annotations Spring MVC 3 (2.5)

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

Page 13: Spring MVC form handling annotation example. Agenda Spring Annotations Spring MVC 3 (2.5)

@Configuration and @Bean

@Configurationpublic class AppConfig { @Bean public MyService myService() { return new MyServiceImpl(); }}

<beans> <bean id="myService" class="com.acme.services.MyServiceImpl"/></beans>

Spring <beans/> XML :

Bean-annotated :

Page 14: Spring MVC form handling annotation example. Agenda Spring Annotations Spring MVC 3 (2.5)

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

Page 15: Spring MVC form handling annotation example. Agenda Spring Annotations Spring MVC 3 (2.5)

@Autowired

public class SimpleMovieLister {

private MovieFinder movieFinder;

@Autowired public void setMovieFinder(MovieFinder movieFinder) { this.movieFinder = movieFinder; }

// ...}

Page 16: Spring MVC form handling annotation example. Agenda Spring Annotations Spring MVC 3 (2.5)

Spring MVC 3 (2.5)

Page 17: Spring MVC form handling annotation example. Agenda Spring Annotations 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

Page 18: Spring MVC form handling annotation example. Agenda Spring Annotations Spring MVC 3 (2.5)

Spring 3 Annotations (New Only)

@Value @RequestHeader @CookieValue @PathVariable @Async @Scheduled @Model @Bound @ActionMapping @EventMapping @RenderMapping

@ResourceMapping @ExceptionHandler @Mapping @RequestBody @ResponseBody @ResponseStatus

Page 19: Spring MVC form handling annotation example. Agenda Spring Annotations Spring MVC 3 (2.5)

Setting up Spring MVC

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

Page 20: Spring MVC form handling annotation example. Agenda Spring Annotations Spring MVC 3 (2.5)

Spring MVC configuration

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

Page 21: Spring MVC form handling annotation example. Agenda Spring Annotations Spring MVC 3 (2.5)

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.

Page 22: Spring MVC form handling annotation example. Agenda Spring Annotations Spring MVC 3 (2.5)

SimpleUrlHandlerMapping

In Spring configuration :

Page 23: Spring MVC form handling annotation example. Agenda Spring Annotations Spring MVC 3 (2.5)

Handling Annotated Controllers

In Spring configuration :

Both are automatically registered by DispatcherServlet

Page 24: Spring MVC form handling annotation example. Agenda Spring Annotations Spring MVC 3 (2.5)

Spring MVC namespace

New in Spring 3.0:

Page 25: Spring MVC form handling annotation example. Agenda Spring Annotations Spring MVC 3 (2.5)

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.

Page 26: Spring MVC form handling annotation example. Agenda Spring Annotations Spring MVC 3 (2.5)

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 annotatio

n 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 re

pository. @Service - Indicates that the class defines a service. Any custom annotation that is itself annotated with @Co

mponent

Page 27: Spring MVC form handling annotation example. Agenda Spring Annotations Spring MVC 3 (2.5)

@Controller

This annotation support is available for both Servlet MVC and Portlet MVC.

Typically used with @RequestMapping annotation.

Page 28: Spring MVC form handling annotation example. Agenda Spring Annotations Spring MVC 3 (2.5)

A simple controller

Page 29: Spring MVC form handling annotation example. Agenda Spring Annotations Spring MVC 3 (2.5)

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.

Page 30: Spring MVC form handling annotation example. Agenda Spring Annotations Spring MVC 3 (2.5)

SimpleFormController vs @Controller

SimpleFormController

Annotation

public class CustomerController extends SimpleFormController{ //...}

@Controller @RequestMapping("/customer.htm") public class CustomerController{ //... }

Page 31: Spring MVC form handling annotation example. Agenda Spring Annotations Spring MVC 3 (2.5)

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.

Page 32: Spring MVC form handling annotation example. Agenda Spring Annotations Spring MVC 3 (2.5)

@RequestParam

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

Page 33: Spring MVC form handling annotation example. Agenda Spring Annotations Spring MVC 3 (2.5)

Mapping requests

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

Page 34: Spring MVC form handling annotation example. Agenda Spring Annotations Spring MVC 3 (2.5)

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>

Page 35: Spring MVC form handling annotation example. Agenda Spring Annotations Spring MVC 3 (2.5)

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).

Page 36: Spring MVC form handling annotation example. Agenda Spring Annotations Spring MVC 3 (2.5)

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

Page 37: Spring MVC form handling annotation example. Agenda Spring Annotations Spring MVC 3 (2.5)

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

Page 38: Spring MVC form handling annotation example. Agenda Spring Annotations Spring MVC 3 (2.5)

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).

Page 39: Spring MVC form handling annotation example. Agenda Spring Annotations Spring MVC 3 (2.5)

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

Page 40: Spring MVC form handling annotation example. Agenda Spring Annotations Spring MVC 3 (2.5)

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

Page 41: Spring MVC form handling annotation example. Agenda Spring Annotations Spring MVC 3 (2.5)

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.

Page 42: Spring MVC form handling annotation example. Agenda Spring Annotations Spring MVC 3 (2.5)

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

Page 43: Spring MVC form handling annotation example. Agenda Spring Annotations Spring MVC 3 (2.5)

referenceData() vs @ModelAttribute

Annotation

Spring’s form <form:checkboxes items=“${webFrameworkList}" path="favFramework" />

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

Page 44: Spring MVC form handling annotation example. Agenda Spring Annotations Spring MVC 3 (2.5)

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.

Page 45: Spring MVC form handling annotation example. Agenda Spring Annotations Spring MVC 3 (2.5)

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

Page 46: Spring MVC form handling annotation example. Agenda Spring Annotations Spring MVC 3 (2.5)

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.

Page 47: Spring MVC form handling annotation example. Agenda Spring Annotations Spring MVC 3 (2.5)

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>

Page 48: Spring MVC form handling annotation example. Agenda Spring Annotations Spring MVC 3 (2.5)

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

Page 49: Spring MVC form handling annotation example. Agenda Spring Annotations Spring MVC 3 (2.5)

Working with Session

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

Page 50: Spring MVC form handling annotation example. Agenda Spring Annotations Spring MVC 3 (2.5)

More Crazy Stuff

Annotation Access to: Headers Cookies

Page 51: Spring MVC form handling annotation example. Agenda Spring Annotations Spring MVC 3 (2.5)

Reference

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