Web Development Using Spring MVC
Web Development Using Spring MVC
Web Development Using Spring MVC
Project Description
Mobile Member Portal
for Express Scripts, Inc. (ESI)
Main Requirements:
Built in ESI Framework -- Spring MVC/Castrol/XSL Capable of being viewed from the Blackberry (and iPhone) mobile web browser
Development Phases
Develop using Servlets and JSP
Teaching Java EE basics Get experience in web development Two months
Development Tools
Database: HSQLDB IDE: Eclipse Build tool: Ant Server: Tomcat Framework: Spring Persistence: Hibernate
9
10
11
12
15
Spring MVC: spring.jar, spring-webmvc.jar JSTL: jstl.jar, standard.jar Hibernate3: hibernate3.jar, and some more Hibernate tools:
hibernate-tools.jar, freemaker.jar
Servlets: servlet-api.jar
16
18
19
20
21
24
26
27
28
30
32
33
34
36
38
The view and model data is encapsulated in a ModelAndView object. Every controller execution method must return a ModelAndView object.
39
40
Building a Controller:
LoginESImobileController (1)
package com.javux.esimobile.controllers; import import import import import import import import import com.javux.esimobile.commands.LoginCommand; com.javux.esimobile.dao.UserDAO; com.javux.esimobile.services.AuthenticationService; javax.servlet.http.HttpServletRequest; javax.servlet.http.HttpServletResponse; javax.servlet.http.HttpSession; org.springframework.validation.BindException; org.springframework.web.servlet.ModelAndView; org.springframework.web.servlet.mvc.SimpleFormController;
public class LoginESImobileController extends SimpleFormController { private AuthenticationService authenticationService; private UserDAO userDAO;
41
Building a Controller:
LoginESImobileController (2)
protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception{ HttpSession session = request.getSession( true ); LoginCommand loginCommand = (LoginCommand) command; String username = loginCommand.getUserId(); session.setAttribute("username",username); if ( authenticationService.authenticate(request, loginCommand, userDAO) ) { return new ModelAndView(getSuccessView()); } else { request.setAttribute("pass","fail"); return showForm(request, response, errors); } }
42
Building a Controller:
LoginESImobileController (3)
public void setAuthenticationService( AuthenticationService authenticationService) { this.authenticationService = authenticationService; } public void setUserDAO(UserDAO udao) { userDAO = udao; } 43
DI way:
Objects are given their dependencies at creation time by the container Loose coupling 44
InterfaceB
implementation2
45
47
49
How DI Works
When creating bean loginESImobileController
Instantiating an instance of UserDAO UserDAO userDAO = new UserDAO(); Injecting userDAO into loginESImobileController loginESImobileController.setUserDAO(userDAO);
52
In esiMobile-servlet.xml
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessag eSource"> <property name="basename" value="resources/messages"/> </bean>
In JSP
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<spring:message code="esimobile.title"/> 53
54
55
Summarize
Dependency Injection Loose coupling Spring MVC configuration
Bean (service) configuration Controller configuration
Thank You!