Title: Spring MVC
1Spring MVC
2What Is The Spring Framework?
- Spring is a non-invasive application development
framework that aims to make the life easier for
Java/J2EE developers, by - Providing a DI (or IoC) container.
- Providing an AOP framework that delivers services
such as declarative transaction management. - Providing abstraction for more complicated (J2EE)
services and APIs, which removes lot of
boilerplate code. - DI AOP Service Abstraction Power to the POJO
3Modules Of The Spring Framework
4Spring MVC At A Glance
- Springs own implementation of Web MVC (Model2)
- Integrates nicely with the middle-tier via DI
- The components of the web framework is loosely
coupled - Supports several view technologies
- JSP/Tiles, Velocity, FreeMarker
- Support integration with other MVC frameworks
- Struts, Tapestry, JavaServerFaces, WebWork
- Provides a JSP Tag Library
5A Typical Life Of A Request
6Creating A Spring MVC Application
- Add the Spring dispatcher servlet to the web.xml
- Configure additional bean definition files in
web.xml - Write Controller classes and configure them in a
bean definition file, typically
META-INF/ltapplgt-servlet.xml - Configure view resolvers that map view names to
to views (JSP, Velocity etc.) - Write the JSPs or other views to render the UI.
7The Spring MVC Example The Buzzword Application
- Keep track of a list of buzzwords
- The user may
- List all buzzwords,
- View details of a specific buzzword,
- Add a new buzzword, or
- Delete a buzzword
- A buzzword consists of
- A name/acronym
- A description
8The DispatcherServlet
- In META-INF/web.xml
- ltservletgt
- ltservlet-namegtbuzzwordslt/servlet-namegt
- ltservlet-classgt
- org.springframework.web.servlet.DispatcherSe
rvlet - lt/servlet-classgt
- ltload-on-startupgt1lt/load-on-startupgt
- lt/servletgt
- ltlistenergt
- ltlistener-classgt
- org.springframework.web.context.ContextLoade
rListener - lt/listener-classgt
- lt/listenergt
- ltcontext-paramgt
- ltparam-namegtcontextConfigLocationlt/param-namegt
- ltparam-valuegt/WEB-INF/buzzwords-service.xmllt/p
aram-valuegt - lt/context-paramgt
9The BuzzwordController Class
- public class BuzzwordsController implements
Controller - private BuzzwordService facade
- public void setFacade(BuzzwordService facade)
- this.facade facade
-
- public ModelAndView handleRequest(HttpServletReq
uest request, -
HttpServletResponse response) - throws Exception
- return new ModelAndView("buzzwords",
"buzzwordService", facade)
10The BuzzwordController Config
- In META-INF/buzzwords-servlet.xml
- ltbean id"buzzwordsController
- class"BuzzwordsController"gt
- ltproperty name"facade"gt
- ltref bean"buzzwordService"/gt
- lt/propertygt
- lt/beangt
11The SimpleUrlHandlerMapping
- In META-INF/buzzwords-servlet.xml
- ltbean id"urlMapping" class"SimpleUrlHandlerMappi
ng"gt - ltproperty name"mappings"gt
- ltpropsgt
- ltprop key"/buzzwords.htm"gt
- buzzwordsControllerlt/propgt
- ltprop key"/editBuzzword.htm"gt
- editBuzzwordControllerlt/propgt
- ltprop key"/newBuzzword.htm"gt
- editBuzzwordControllerlt/propgt
- ltprop key"/submitBuzzword.htm"gt
- submitBuzzwordFormControllerlt/propgt
- ltprop key"/deleteBuzzword.htm"gt
- deleteBuzzwordFormControllerlt/propgt
- lt/propsgt
- lt/propertygt
- lt/beangt
12The InternalResourceViewResolver
- In META-INF/buzzwords-servlet.xml
- ltbean id"viewResolver"
- class"org.springframework...InternalResourceVie
wResolver"gt - ltproperty name"viewClass"gt
- ltvaluegtorg.springframework...JstlViewlt/valuegt
- lt/propertygt
- ltproperty name"prefix"gtltvaluegt/WEB-INF/jsp/lt/va
luegtlt/propertygt - ltproperty name"suffix"gtltvaluegt.jsplt/valuegtlt/pro
pertygt - lt/beangt
13The buzzwords.jsp
- lthtmlgtltheadgtlttitlegtltcout value"Buzzwords"/gtlt/tit
legtlt/headgt - ltbodygt
- lth1gtltcout value"Buzzwords"/gtlt/h1gt
- ltcforEach items"buzzwordService.buzzwo
rds" var"buzzword"gt - lta href"editBuzzword.htm?buzzwordNameltc
out value"buzzword.name"/gt"/gtltcout
value"buzzword.name"/gt - lta href"deleteBuzzword.htm?buzzwordNamelt
cout value"buzzword.name"/gt"/gtltcout value"
delete"/gt - ltbr/gt
- lt/cforEachgt
- ltbr/gt
- lta href"newBuzzword.htm"gtAdd a new
buzzwordlt/agt - lt/bodygt
- lt/htmlgt
14The Buzzword Application
15Testing Your Web Application
- All parts of the web application can be tested in
isolation without any container - The Controller
- The Validator
- Any in-house developed HandlerMapping class
- Any in-house developed ViewResolver class
16Errors Is Not An Option, It Comes With The
Software
- ltbean id"exceptionResolver" class..SimpleMappin
gExceptionResolver"gt - ltproperty name"exceptionMappings"gt
- ltpropsgt
- ltprop key"java.lang.RuntimeException"gtindex
lt/propgt - lt/propsgt
- lt/propertygt
- lt/beangt
17So, What Do You Think About Spring MVC?