3. Spring MVC
Another Web MVC Framework?
After learning Struts, JSF, DDUI …….
…………….. now Spring MVC ?
4. 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
5. 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
14. 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.
15. 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.
19. Spring Framework Fundamentals
What is Spring Framework ?
● Spring Framework is …..
● Open Source Meta Framework
● Provides frameworks and container
● Simplify the development
20. 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
21. Spring Framework
Benefits:
● Light Weight
● Promotes good OO design
● Container Agnostic Framework : Insulates the
application from its hosting container.
27. Spring MVC
Front Controller Design Pattern:
● DispatcherServlet is the Front Controller handles Client
Requests.
● ActionServlet in Struts
● FacesServlet in JSF
● AppServlet in DDUI
28. 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
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 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>
31. 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>
32. Spring MVC
Developing App using Spring MVC:
● Step 3: spring-mvc.xml : Component Scan
<beans>
<context:component-scan base-package=“com.sf.web"/>
</beans>
33. 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” ;
}
}
34. 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;
35. 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";
}
}