Download - Introduction to Spring MVC (Emmanuel Garcia)

Transcript
Page 1: Introduction to Spring MVC (Emmanuel Garcia)

Copyright @ 2011 LANDBANK eBSD

Introduction to Spring MVC

Emmanuel S. Garcia

eBSD, ADTST

Page 2: Introduction to Spring MVC (Emmanuel Garcia)

Copyright @ 2011 LANDBANK eBSD

What is a Framework

• When you start development, you can– Build everything from scratch– Reuse code from an existing application– Use a framework to build around it

“a set of programs demanding a specific design pattern from which you can build the rest of your application”

e.garcia

Spring MVC is an architectural design pattern used for web development

Page 3: Introduction to Spring MVC (Emmanuel Garcia)

Copyright @ 2011 LANDBANK eBSD

What is a Framework

The Spring Framework is a lightweight solution and a potential one-stop-shop for building your enterprise-ready applications.

Spring Framework is a Java platform that provides comprehensive infrastructure support for developing Java applications.

Spring handles the infrastructure so you

can focus on your application

Page 4: Introduction to Spring MVC (Emmanuel Garcia)

Copyright @ 2011 LANDBANK eBSD

So…. tell me,

What is a Model?

What is a View?

What is a Controller?

Page 5: Introduction to Spring MVC (Emmanuel Garcia)

Copyright @ 2011 LANDBANK eBSD

Model-View-Controller (MVC)

ModelObjects which represent something

EntityBusiness RuleA ValidatorA ServiceA POJOData Access Object

Page 6: Introduction to Spring MVC (Emmanuel Garcia)

Copyright @ 2011 LANDBANK eBSD

Model-View-Controller (MVC)

View

Things that display something/anythingModels (Dynamic Data)BlurbsStatic Data

Page 7: Introduction to Spring MVC (Emmanuel Garcia)

Copyright @ 2011 LANDBANK eBSD

Model-View-Controller (MVC)

ControllerThings that orchestrate the models and views

Page 8: Introduction to Spring MVC (Emmanuel Garcia)

Copyright @ 2011 LANDBANK eBSD

Model-View-Controller (MVC)

Page 9: Introduction to Spring MVC (Emmanuel Garcia)

Copyright @ 2011 LANDBANK eBSD

Basics of Spring MVC

Spring Configuration Files

web.xml All calls go through the DispatcherServlet Defines the Spring config file (*-servlet.xml by default)

*-servlet.xml Where the rest of your application components are

registered Where you define the Spring objects you need in your

application

Page 10: Introduction to Spring MVC (Emmanuel Garcia)

Copyright @ 2011 LANDBANK eBSD

Basics of Spring MVCStep 1 – User sends request to server by

submitting form / by clicking

hyperlink etc. Request is

initially given to WEB.XML.

Step 2 – WEB.XML routes request to

DispatcherServlet by looking at

<servlet-mapping> tag.

Step 3 – Inside DispatcherServlet,

‘HandlerMapping’ hands over request

to suitable ‘Controller’.

Step 4 – Controller maps request to proper

Model class. All BUSINESS LOGIC is

done inside Model classes.

Step 5 – If database operation is needed then

Model class will route request to

suitable DAO. All database operations

should be carried out in DAO.

Step 6 – If needed, attach attributes into request

/session/application scope and return

back to Model.

Step 7 – If needed, attach attributes into request

/session/application scope and return

back to Controller.

Step 8 – Controller simply returns View

(JSP/HTML etc).

Step 9 – JSP/Html is viewed back to user

Page 11: Introduction to Spring MVC (Emmanuel Garcia)

Copyright @ 2011 LANDBANK eBSD

Basics of Spring MVC

Page 12: Introduction to Spring MVC (Emmanuel Garcia)

Copyright @ 2011 LANDBANK eBSD

Basics of Spring MVC

Spring Configuration Files

web.xml All calls go through the DispatcherServlet Spring config file (*-servlet.xml by default)

*-servlet.xml Where the rest of your application components are

registered Where you define the Spring objects you need in your

application

Page 13: Introduction to Spring MVC (Emmanuel Garcia)

Copyright @ 2011 LANDBANK eBSD

web.xml

web.xml sample

Page 14: Introduction to Spring MVC (Emmanuel Garcia)

Copyright @ 2011 LANDBANK eBSD

Basics of Spring MVC

Spring Configuration Files

web.xml All calls go through the DispatcherServlet Spring config file (*-servlet.xml by default)

*-servlet.xml Where the rest of your application components are

registered Where you define the Spring objects you need in your

application

Page 15: Introduction to Spring MVC (Emmanuel Garcia)

Copyright @ 2011 LANDBANK eBSD

*-servlet.xml

Page 16: Introduction to Spring MVC (Emmanuel Garcia)

Copyright @ 2011 LANDBANK eBSD

*-servlet.xml

org.springframework.web.servlet.handler.SimpleUrlHandlerMapping what controller to call given a URL

org.springframework.web.servlet.view.InternalResourceViewResolver how to determine what view to show

org.springframework.context.support.ReloadableResourceBundleMessageSource support for blurbsorg.springframework.web.multipart.MultipartResolver

how to handle filesorg.springframework.web.servlet.HandlerExceptionResolver

what to do with an Exceptionorg.springframework.web.servlet.ThemeResolver

where to get css, images, pages fromorg.springframework.web.servlet.HandlerAdapter

wrapper around the controller (or servlet)

Spring Configuration Support Goodies

Page 17: Introduction to Spring MVC (Emmanuel Garcia)

Copyright @ 2011 LANDBANK eBSD

*-servlet.xml (Handler Mapping)

Given a URL, figures out what Controller to use:

SimpleUrlHandlerMapping

define mappings with Map or PropertiesBeanNameUrlHandlerMapping

bean names have same names as URLCommonsPathMapHandlerMapping

use Commons Attributes to determine mapping

Handling Requests with Handler Mapping

Page 18: Introduction to Spring MVC (Emmanuel Garcia)

Copyright @ 2011 LANDBANK eBSD

*-servlet.xml (Messages)

Define the bean messageSource with a MessageSource to set the resources:

ResourceMessageBundleMessageSource - load messages from .properties filesReloadableResourceMessageBundleMessageSource - same as above, but reloads!StaticMessageSource - set messages within the object

The spring tag <spring:message> picks the resource

Messages/Blurbs

Page 19: Introduction to Spring MVC (Emmanuel Garcia)

Copyright @ 2011 LANDBANK eBSD

*-servlet.xml (Views)

JstlView

map to a JSP pageRedirectView

Perform an HTTP RedirectTilesView, TilesJstlView

integration with tilesVelocityLayoutView, VelocityToolboxView, VelocityView

Integration with the Velocity templating toolFreeMarkerView

use the FreeMarker templating toolJasperReportsView, JasperReportsMultiFormatView,

JasperReportsMultiFormatView, JasperReportsPdfView, JasperReportsXlsView

Support for Jasper Reports

Different Views

Page 20: Introduction to Spring MVC (Emmanuel Garcia)

Copyright @ 2011 LANDBANK eBSD

*-servlet.xml (Form Handling)

Page 21: Introduction to Spring MVC (Emmanuel Garcia)

Copyright @ 2011 LANDBANK eBSD

Spring Controllers

Page 22: Introduction to Spring MVC (Emmanuel Garcia)

Copyright @ 2011 LANDBANK eBSD

Spring Controllers

AbstractController

basic controller, knows about caching, turning on/off

get/set/post/headParameterizableViewController

always go to the same viewUrlFileNameViewController

parses the URL to return a view (http://blah/foo.html -> foo)SimpleFormController

for form handling, hooks for attaching commands, validatorAbstractWizardFormController

easy wizard controllerServletWrappingController delegates to a servlet

Spring MVC includes lots of Controllers to extend from:

Page 23: Introduction to Spring MVC (Emmanuel Garcia)

Copyright @ 2011 LANDBANK eBSD

Is this picture still hazy?

Page 24: Introduction to Spring MVC (Emmanuel Garcia)

Copyright @ 2011 LANDBANK eBSD

Project ExplorerSample Spring Project Artifacts

Page 25: Introduction to Spring MVC (Emmanuel Garcia)

Copyright @ 2011 LANDBANK eBSD

Thank you!

Page 26: Introduction to Spring MVC (Emmanuel Garcia)

Copyright @ 2011 LANDBANK eBSD

Introduction to Spring Annotations

Emmanuel S. Garcia

eBSD, ADTST

Page 27: Introduction to Spring MVC (Emmanuel Garcia)

Copyright @ 2011 LANDBANK eBSD

Annotations

“annotations make life easier by reducing bean registration on the Spring xml configuration file”

e.garcia

Page 28: Introduction to Spring MVC (Emmanuel Garcia)

Copyright @ 2011 LANDBANK eBSD

Annotations

*-servlet.xml configurations

Page 29: Introduction to Spring MVC (Emmanuel Garcia)

Copyright @ 2011 LANDBANK eBSD

Annotations

@Controller

Page 30: Introduction to Spring MVC (Emmanuel Garcia)

Copyright @ 2011 LANDBANK eBSD

Annotations

The @Controller annotation is used to mark any java class as a controller

The @RequestMapping annotation is used to map the web request "/userRegistration.htm" to the UserController class.

The @SessionAttributes annotation is used to store the model object in

the session. In our case the model object is user

In the controller class you need to specify two methods, one for handling

the HTTP GET request and the other for handling the HTTP POST request. The @RequestMapping(method) annotation is used to indicate the type of HTTP request

Notes:

Page 31: Introduction to Spring MVC (Emmanuel Garcia)

Copyright @ 2011 LANDBANK eBSD

Annotations

@Controller

Page 32: Introduction to Spring MVC (Emmanuel Garcia)

Copyright @ 2011 LANDBANK eBSD

Annotations

Page 33: Introduction to Spring MVC (Emmanuel Garcia)

Copyright @ 2011 LANDBANK eBSD

Annotations

@Controller

@ModelAttribute

Page 34: Introduction to Spring MVC (Emmanuel Garcia)

Copyright @ 2011 LANDBANK eBSD

Annotations

@Controller

@ModelAttribute

Page 35: Introduction to Spring MVC (Emmanuel Garcia)

Copyright @ 2011 LANDBANK eBSD

Annotations

@Autowired

Dependency Injection Overview

Page 36: Introduction to Spring MVC (Emmanuel Garcia)

Copyright @ 2011 LANDBANK eBSD

Annotations

@Autowired

Dependency Injection Overview

Page 37: Introduction to Spring MVC (Emmanuel Garcia)

Copyright @ 2011 LANDBANK eBSD

Annotations

@Autowired

Dependency Injection Overview

Page 38: Introduction to Spring MVC (Emmanuel Garcia)

Copyright @ 2011 LANDBANK eBSD

Annotations

@AutowiredDependency Injection Overview Summary

Java components / classes should be as independent as possible of other Java classes.

This increases the possibility to reuse these classes and to test them independently

of other classes(Unit Testing).

To decouple Java components from other Java components the dependency to a certain

other class should get injected into them rather that the class itself instantiates this object.

Class A has a dependency to class B if class uses class B as a variable.

If dependency injection is used then the class B is given to class A via the constructor of the class A - this is then called construction injectiona setter - this is then called setter injection

The general concept between dependency injection is called Inversion of Control.

A class should not configure itself but should be configured from outside.

Page 39: Introduction to Spring MVC (Emmanuel Garcia)

Copyright @ 2011 LANDBANK eBSD

Annotations

@Autowired

Page 40: Introduction to Spring MVC (Emmanuel Garcia)

Copyright @ 2011 LANDBANK eBSD

Annotations

@Autowired

Dependency Injection in Spring

@Autowired - autowires by type @Qualifier - provide more specific autowiring behavior if there will be multiple beans of the same type in the IoC container /spring xml file (e.g. multiple data source beans connecting to different DBs) @Configurable - used to mark a class eligible for Spring dependency injection, but it's typically used with classes instantiated outside of the IoC container @Resource - autowires by name and is from JSR-250, which is the specification for Commons Annotations

Page 41: Introduction to Spring MVC (Emmanuel Garcia)

Copyright @ 2011 LANDBANK eBSD

Annotations

Annotations from org.springframework.stereotype

Annotation Target Description

@Component ClassIndicates this class is eligible for registration as a Spring bean when detected by a classpath scanner (context:component-scan).

@Controller ClassIndicates this class is a Web Controller eligible for registration as a Spring bean when detected by a classpath scanner (context:component-scan). It is a specialized form of @Component.

@Repository ClassIndicates this class is a Repository (DAO) eligible for registration as a Spring bean when detected by a classpath scanner (context:component-scan). It is a specialized form of @Component.

@Service ClassIndicates this class is a Service (business service facade) eligible for registration as a Spring bean when detected by a classpath scanner (context:component-scan). It is a specialized form of @Component.

Page 42: Introduction to Spring MVC (Emmanuel Garcia)

Copyright @ 2011 LANDBANK eBSD

Annotations

Annotations from org.springframework.context.annotation

Annotation Target Description

@Scope Class Indicates scope of the bean

Page 43: Introduction to Spring MVC (Emmanuel Garcia)

Copyright @ 2011 LANDBANK eBSD

Thank you!