Design & Development
of Web Applications
using Spring MVC
by Naresh Chintalcheru
Spring MVC
Another Web MVC Framework?
Spring MVC
Another Web MVC Framework?
After learning Struts, JSF, DDUI …….
…………….. now Spring MVC ?
Spring MVC
● Benefit of using a Web Framework
● Removes the need of boilerplate code
● Gather HTTP Request Parameters
● Validation & Conversions
● Update the Model
● Standard code level design for multiple
applications in an enterprise
● Navigation Flow
● Validations
Spring MVC
● Benefit of using a Web Framework
● No Web Application Component Design
● Use Web Framework as Design Templates
and plugin the View, Model and Controllers
Spring MVC
● Benefit of using a Web Framework
● Speed up the development
Spring MVC
Web MVC Frameworks
● Struts
● JSF
● Wicket
● DDUI
● Spring MVC
Spring MVC
Presentation Layer Frameworks
● Web MVC Framework
● Template Framework
● View Technologies
● UI Widget Framework
Spring MVC
Template Frameworks
● Tiles
● Facelets
● SiteMesh
View Technologies
● JSP
● XHTML
● XSLT
UI Widget Frameworks
● JQuery
● DOJO
● SFX
Spring MVC
Why Spring MVC Framework?
Spring MVC
Spring MVC is a action-based or request-based
framework similar to Struts.
JSF is a component-based framework.
Spring MVC
Spring MVC
Spring MVC
Spring MVC vs JSF
● JSF is part of JEE Specification
● JSF is based on component architecture and
has learning curve
● Limited HTTP GET method support in JSF
● REST Web Services Support in Spring MVC.
Spring MVC
Spring MVC vs Struts
● Struts forces concrete Inheritance of
Action/ActionForm, taking away single shot of
Inheritance in Java.
● Spring MVC based on Interfaces and
Annotations.
● Spring MVC is truly view-agnostic. Many
options in View Technologies.
● Use Spring IoC Container from Presentation
Tier to Integration Tier.
Spring MVC
Spring MVC is only one module in the Spring Framework
Spring Framework Fundamentals
Spring Framework Fundamentals
Spring Framework Fundamentals
What is Spring Framework ?
● Spring Framework is …..
● Open Source Meta Framework
● Provides frameworks and container
● Simplify the development
Spring Framework Fundamentals
Spring framework is a Layered Architecture
Choose the modules needed and eliminate other
modules
All modules are built on the top of its core
container
Spring Framework
Benefits:
● Light Weight
● Promotes good OO design
● Container Agnostic Framework : Insulates the
application from its hosting container.
Spring Framework Fundamentals
Spring Has Two Web Frameworks
● Spring MVC
● Spring Web Flow
Spring Framework Fundamentals
Spring Framework Web Flow
● Secondary Page Flow logic framework
● Can be used with Spring MVC, JSF and
Struts.
Spring Framework Fundamentals
Spring MVC Request Flow ….
Spring MVC
Spring MVC
Spring MVC
Spring MVC
Spring MVC
Front Controller Design Pattern:
● DispatcherServlet is the Front Controller handles Client
Requests.
● ActionServlet in Struts
● FacesServlet in JSF
● AppServlet in DDUI
Spring MVC
Controller Design Pattern in MVC:
● Controllers are components that are being called by the
Dispatcher Servlet for performing page level logic and
delegate to Service Objects.
● Action Classes in Struts
● Managed Beans & Backed Beans in JSF
● PageProcessor in DDUI
Spring MVC
View Resolver & Navigation Logic:
● Resolves view names to views. Resolves navigation
logic from spring-mvc.xml
● Struts-config.xml in Struts
● Jsf-config.xml in JSF
Spring MVC
Developing App using Spring MVC:
● Step 1: web.xml : Front Controller
<web-app version="2.4">
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.*</url-pattern>
</servlet-mapping>
</web-app>
Spring MVC
Developing App using Spring MVC:
● Step 2: web.xml : ApplicationContext
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc.xml</param-value>
</context-param>
Spring MVC
Developing App using Spring MVC:
● Step 3: spring-mvc.xml : Component Scan
<beans>
<context:component-scan base-package=“com.sf.web"/>
</beans>
Spring MVC
Developing App using Spring MVC:
● Step 4: Controller Classes
@Controller
public class EmployeeController {
@Autowired
private final EmployeeSO employeeSO;
@RequestMapping("/emplist")
public String empHandler(Model model) {
List empList = employeeSO.getAllEmp();
model.addAttribute(“empList”, empList);
return “ListEmpPage” ;
}
}
Spring MVC
Bean Validation Framework JSR-303:
public class Employee {
@NotNull
@Pattern(regexp = "[a-z-A-Z]*")
private String firstName;
@Size(min=2, max=15)
private String lastName;
@Past
private Date birthDate;
@Email (message=“Invalid Email “)
private String email;
Spring MVC
REST Web Services:
http://localhost:8080/SpringMVC/employee/John
@Controller @RequestMapping("/employee")
public class EmployeeController {
@RequestMapping( value="/{name}“ )
public String getEmp(@PathVariable String name, ModelMap
model) {
model.addAttribute(“emp", name);
return “EmpListPage";
}
}
Spring MVC
Question & Answers

Design & Development of Web Applications using SpringMVC

  • 1.
    Design & Development ofWeb Applications using Spring MVC by Naresh Chintalcheru
  • 2.
    Spring MVC Another WebMVC Framework?
  • 3.
    Spring MVC Another WebMVC Framework? After learning Struts, JSF, DDUI ……. …………….. now Spring MVC ?
  • 4.
    Spring MVC ● Benefitof using a Web Framework ● Removes the need of boilerplate code ● Gather HTTP Request Parameters ● Validation & Conversions ● Update the Model ● Standard code level design for multiple applications in an enterprise ● Navigation Flow ● Validations
  • 5.
    Spring MVC ● Benefitof using a Web Framework ● No Web Application Component Design ● Use Web Framework as Design Templates and plugin the View, Model and Controllers
  • 6.
    Spring MVC ● Benefitof using a Web Framework ● Speed up the development
  • 7.
    Spring MVC Web MVCFrameworks ● Struts ● JSF ● Wicket ● DDUI ● Spring MVC
  • 8.
    Spring MVC Presentation LayerFrameworks ● Web MVC Framework ● Template Framework ● View Technologies ● UI Widget Framework
  • 9.
    Spring MVC Template Frameworks ●Tiles ● Facelets ● SiteMesh View Technologies ● JSP ● XHTML ● XSLT UI Widget Frameworks ● JQuery ● DOJO ● SFX
  • 10.
    Spring MVC Why SpringMVC Framework?
  • 11.
    Spring MVC Spring MVCis a action-based or request-based framework similar to Struts. JSF is a component-based framework.
  • 12.
  • 13.
  • 14.
    Spring MVC Spring MVCvs JSF ● JSF is part of JEE Specification ● JSF is based on component architecture and has learning curve ● Limited HTTP GET method support in JSF ● REST Web Services Support in Spring MVC.
  • 15.
    Spring MVC Spring MVCvs Struts ● Struts forces concrete Inheritance of Action/ActionForm, taking away single shot of Inheritance in Java. ● Spring MVC based on Interfaces and Annotations. ● Spring MVC is truly view-agnostic. Many options in View Technologies. ● Use Spring IoC Container from Presentation Tier to Integration Tier.
  • 16.
    Spring MVC Spring MVCis only one module in the Spring Framework
  • 17.
  • 18.
  • 19.
    Spring Framework Fundamentals Whatis Spring Framework ? ● Spring Framework is ….. ● Open Source Meta Framework ● Provides frameworks and container ● Simplify the development
  • 20.
    Spring Framework Fundamentals Springframework is a Layered Architecture Choose the modules needed and eliminate other modules All modules are built on the top of its core container
  • 21.
    Spring Framework Benefits: ● LightWeight ● Promotes good OO design ● Container Agnostic Framework : Insulates the application from its hosting container.
  • 22.
    Spring Framework Fundamentals SpringHas Two Web Frameworks ● Spring MVC ● Spring Web Flow
  • 23.
    Spring Framework Fundamentals SpringFramework Web Flow ● Secondary Page Flow logic framework ● Can be used with Spring MVC, JSF and Struts.
  • 24.
  • 25.
  • 26.
  • 27.
    Spring MVC Front ControllerDesign Pattern: ● DispatcherServlet is the Front Controller handles Client Requests. ● ActionServlet in Struts ● FacesServlet in JSF ● AppServlet in DDUI
  • 28.
    Spring MVC Controller DesignPattern in MVC: ● Controllers are components that are being called by the Dispatcher Servlet for performing page level logic and delegate to Service Objects. ● Action Classes in Struts ● Managed Beans & Backed Beans in JSF ● PageProcessor in DDUI
  • 29.
    Spring MVC View Resolver& Navigation Logic: ● Resolves view names to views. Resolves navigation logic from spring-mvc.xml ● Struts-config.xml in Struts ● Jsf-config.xml in JSF
  • 30.
    Spring MVC Developing Appusing Spring MVC: ● Step 1: web.xml : Front Controller <web-app version="2.4"> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>*.*</url-pattern> </servlet-mapping> </web-app>
  • 31.
    Spring MVC Developing Appusing Spring MVC: ● Step 2: web.xml : ApplicationContext <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-mvc.xml</param-value> </context-param>
  • 32.
    Spring MVC Developing Appusing Spring MVC: ● Step 3: spring-mvc.xml : Component Scan <beans> <context:component-scan base-package=“com.sf.web"/> </beans>
  • 33.
    Spring MVC Developing Appusing Spring MVC: ● Step 4: Controller Classes @Controller public class EmployeeController { @Autowired private final EmployeeSO employeeSO; @RequestMapping("/emplist") public String empHandler(Model model) { List empList = employeeSO.getAllEmp(); model.addAttribute(“empList”, empList); return “ListEmpPage” ; } }
  • 34.
    Spring MVC Bean ValidationFramework JSR-303: public class Employee { @NotNull @Pattern(regexp = "[a-z-A-Z]*") private String firstName; @Size(min=2, max=15) private String lastName; @Past private Date birthDate; @Email (message=“Invalid Email “) private String email;
  • 35.
    Spring MVC REST WebServices: http://localhost:8080/SpringMVC/employee/John @Controller @RequestMapping("/employee") public class EmployeeController { @RequestMapping( value="/{name}“ ) public String getEmp(@PathVariable String name, ModelMap model) { model.addAttribute(“emp", name); return “EmpListPage"; } }
  • 36.