An Introduction To the Spring M.V.C. Framework Reference From Website By Tom Kochanowicz.

Slides:



Advertisements
Similar presentations
Introduction to the Spring Framework
Advertisements

JSP and web applications
Struts Portlet Copyright © Liferay, Inc. All Rights Reserved. No material may be reproduced electronically or in print without written permission.
Web Development with Karsten Schulz Terp-Nielsen Master Principal Sales Consultant Oracle Denmark.
Apache Struts Technology
Spring, Hibernate and Web Services 13 th September 2014.
Web MVC-2: Apache Struts Rimon Mikhaiel
Struts1 Apache Struts Dr Jim Briggs. Struts2 What is Struts? Struts is an open source framework for building Java web applications Supports MVC/Model.
JBoss Seam: Contextual Components Jason Bechtel
Web programming for project students Dr Jim Briggs.
Apache Struts Technology A MVC Framework for Java Web Applications.
Session-01. Layers Struts 2 Framework The struts 2 framework is used to develop MVC-based web application. Struts 1.0 was released in June The.
Introduction to Java web programming Dr Jim Briggs JWP intro1.
Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.
The Spring Framework: A brief introduction to Inversion of Control James Brundege
Intro to Spring CJUG - January What is Spring? “The Spring framework provides central transaction control of various objects.” This means that any.
The Spring Framework A quick overview. The Spring Framework 1. Spring principles: IoC 2. Spring principles: AOP 3. A handful of services 4. A MVC framework.
Struts 2.0 an Overview ( )
Struts. Agenda Preface Struts and its components An example The architecture required for Struts Applications.
Michael Rimov Centerline Computers Craig McClanahan Sun Microsystems O’Reilly Open Source Convention July , 2003.
UNIT-V The MVC architecture and Struts Framework.
Lecture 2 - Struts ENTERPRISE JAVA. 2 Contents  Servlet Deployment  Servlet Filters  Model View Controllers  Struts  Dependency Injection.
Web Development Methodologies Yuan Wang(yw2326). Basic Concepts Browser/Server (B/S) Structure Keywords: Browser, Server Examples: Websites Client/Server.
JavaServer Faces: The Fundamentals Compiled from Sun TechDays workshops (JSF Basics, Web-Tier Codecamp: JavaServer Faces, Java Studio Creator; IBM RAD)
CSCI 6962: Server-side Design and Programming Course Introduction and Overview.
Spring Overview, Application demo -Midhila Paineni 09/23/2011 Spring Overview, Application demo9/8/20151.
Copyright © 2012 Accenture All Rights Reserved.Copyright © 2012 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are.
Design Patterns Phil Smith 28 th November Design Patterns There are many ways to produce content via Servlets and JSPs Understanding the good, the.
OpusCollege and the use of Spring and iBatis
Basic Struts Architecture Client Server Database Request Response Control View Model Server Struts Framework.
CSCI 6962: Server-side Design and Programming Introduction to Java Server Faces.
JSP Architecture Outline  Model 1 Architecture  Model 2 Architecture.
|Tecnologie Web L-A Anno Accademico Laboratorio di Tecnologie Web Introduzione ad Eclipse e Tomcat
Anti Orgla, Nortal AS Spring Framework
JSF Introduction Copyright © Liferay, Inc. All Rights Reserved. No material may be reproduced electronically or in print without written permission.
Struts J2EE web application framework “ Model 2 ” Model View Controller Controller Servlet Key features XML metadata Struts taglib Simplified form validation.
Lecturer: Prof. Piero Fraternali, Teaching Assistant: Alessandro Bozzon, Advanced Web Technologies: Struts–
Spring Framework. Spring Overview Spring is an open source layered Java/J2EE application framework Created by Rod Johnson Based on book “Expert one-on-one.
J2EE Overview Web Programming CSCI J2EE multi-tier architecture Servlet: Java class loaded into Web server JSP page: enhanced HTML page that is.
Introduction to Web Dimitar Nenchev Ivan Nakov
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Forms with Spring MVC Handling Form.
JSF Framework Java Server Faces Presented by Songkran Totiya (6/10/2014)
Struts Framework Anna Paščenko. What is Struts?  An open source framework for building Java web applications.
CSC 2720 Building Web Applications Frameworks for Building Web Applications.
Chính phủ điện tử TS. Phạm Văn Tính Khoa CNTT, ĐH Nông Lâm TP.HCM
INTEGRATION OF BACKBONE.JS WITH SPRING 3.1. Agenda New Features and Enhancements in Spring 3.1 What is Backbone.js and why I should use it Spring 3.1.
Apache Struts. J2EE Web Application using MVC design pattern Why MVC? Separate components = easier maintenance – Model component holds object data – View.
Sakai WebApp Structure
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Spring MVC Essentials Getting started.
Spring and DWR Frameworks for Rich Web Enterprise Application Thomas Wiradikusuma Presentation to the 20 th.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JSP Application Models.
Chapter 6 Chapter 6 Server Side Programming (JSP) Part 1 1 (IS 203) WebProgramming (IS 203) Web Programming.
Bayu Priyambadha, S.Kom. Static content  Web Server delivers contents of a file (html) 1. Browser sends request to Web Server 3. Web Server sends HTML.
APACHE STRUTS ASHISH SINGH TOMAR ast2124. OUTLINE Introduction The Model-View-Controller Design Pattern Struts’ implementation of the MVC Pattern Additional.
Text 16 de mayo de 2009 Spring Framework Part III. Portable Service Abstractions and Spring MVC Buenos Aires, June 2009.
OOSD Using Java CBTS Framework. 11/2/04CBTS2 Servlet  A servlet is a Java program that can extends Web server’s functionality.  Servlets interact with.
Apache Struts Technology A MVC Framework for Java Web Applications.
CS 562 Advanced Java and Internet Application Computer Warehouse Web Application By Team Alpha :-  Puja Mehta (102163)  Mona Nagpure (102147)
CS520 Web Programming Spring – Web MVC Chengyu Sun California State University, Los Angeles.
CS520 Web Programming Spring – Web MVC Chengyu Sun California State University, Los Angeles.
An Introduction To the Spring M.V.C. Framework
Unit 6-Chapter 2 Struts.
Sakai WebApp Structure
Intro to Spring CJUG - January 2013.
Introduction to Struts
CS5220 Advanced Topics in Web Programming Spring – Web MVC
The Model Layer What is Model?
JavaServer Faces: The Fundamentals
CS5220 Advanced Topics in Web Programming Spring – Web MVC
Struts BY: Tejashri Udavant..
Presentation transcript:

An Introduction To the Spring M.V.C. Framework Reference From Website By Tom Kochanowicz

Outline Where we’ve been M.V.C. Frameworks Why Use Spring Framework IoC (Inversion of Control) Examples

Where we’ve been Web based programming “The Servlet Way” JSP or HTML Form  Submit to servlet  Servlet Processes data & Outputs information Works well for small applications but can quickly grow out of control because HTML, scrip-lets, java script, tag-libraries, database access, and business logic makes it difficult to organize. Put simply, lack of structure can cause a “soup” of different technologies. JSP’s compile to Servlet

“A Better Way?” Separate the Data Access, Business Logic and Presentation using a M.V.C. Framework. Choose a M.V.C. framework: WebWork, Spring, Struts, Java-Server- Faces, Tapestry plus “many more”

Things change: Struts, by far is the most popular; same creator of Struts (Craig McClanahan) is the co-spec leader of Java Server Faces. Webwork evolved to Webwork2 Tapestry - has been around for awhile. Spring – “newer” of the frameworks. Integrates well with any other framework or by itself.

Why Spring Framework? All frameworks integrate well with Spring. Spring offers an open yet structured framework, using dependency-injection, a type of inversion-of-control to integrate different technologies together. Consistent Configuration, open plug-in architecture Integrates well with different O/R Mapping frameworks like Hibernate Easier to test applications with. Less complicated then other frameworks. Active user community.

Want to integrate your existing web-app with a Spring middle tier? –Struts –Web Work ework+Integration –Tapestry pestry.html

What if I like Microsoft.NET? Then try Spring Framework.NET

Why I chose to learn the Spring framework Because of IoC/Dependency Injection you can easily change configurations. Addresses end-to-end requirements, not just one part. Spring is well organized and seems easier to learn then struts. Portable across deployment environments. Integrates well with Hibernate Meant to wet your appetite and note be compressive.

Downsides: Not as mature as other frameworks (but very stable). Market share is small at this time, but rapidly growing. dependency-injection (Inversion-of-control) is a different way of thinking (This is actually a plus). Not a-lot of tool support for Spring yet. A plug-in for Eclipse is available. Depends on who you ask.

Spring is not just a Presentation M.V.C. Framework: Persistence support: Spring also supports A JDBC Framework that makes it easier to create JDBC Apps. Supports O/R mapping Frameworks making it easier to use O/R tools like Hibernate & JDO Spring offers Connection Pooling for any POJO. Supports transaction framework Has good support for aspect-oriented-programming Plus much more.

What is dependency-injection & why use it? Dependency-injection (a type of IoC) is when you let your framework control your application. Spring links objects together instead of the objects linking themselves together. Spring object linking is defined in XML files, allowing easy changes for different application configurations thus working as a plug in architecture. Dependency injection is where the control of the application is inverted to the framework. This control is configured in the framework with an XML file.

Without Dependency-Injection/IoC Object A Object B Object C creates An object creating its dependencies without IoC leads to tight object coupling.

Object A Object B Object C setB(IB) setC(IC) Object A contains setter methods that accept interfaces to objects B and C. This could have also been achieved with constructors in object A that accepts objects B and C. With Dependency-Injection/IoC Allows objects to be created at higher levels and passed into object so they can use the implementation directly

Spring supports two types of dependency injection “setter-based” and “constructor based” injection Code Example of setter based injection: *** beans are accessed by there “bean name” Interpretation of the above code: Person person = new Person(); This code creates a Person object and calls the set () method, passing in the string defined as a value.

Constructor based injection <bean name="fileDataProcessor“ class="examples.spring.DataProcessor" singleton="true"> /data/file1.data Interpretation of the above code: FileDataReader fileDataReader = new FileDataReader(“/data/file1.data”); DataProcessor fileDataProcessor = new DataProcessor(fileDataReader);

Spring provides a JDBC Template that manages your connections for you. *** Simple example of connecting to a datasource. *** ProductManagerDaoJdbc implements ProductManagerDao { public void setDataSource(DataSource ds) { this.ds = ds; } *** No need to change java code when changing datasource; change in ‘Spring bean’ XML file below. jdbc:mysql://localhost/test

Spring Web Key Concepts

Spring Web Controllers In an MVC architecture your controllers handle all requests. Spring uses a ‘DispatcherServlet” defined in the web.xml file to analyze a request URL pattern and then pass control to the correct Controller by using a URL mapping defined in a “ Spring bean” XML file.

Spring Web Container Setup In your Web Container, the Spring “bean” XML file exists in the same directory as your web.xml file with a “-servlet.xml” appended to it. webapps /tradingapp /WEB-INF/tradingapp-servlet.xml, web.xml) /classes /lib (all jar files) The dispatcher servlet is mapped to the name “tradingapp” so it knows to look in the “tradingapp- servlet.xml” file to look-up a URL-to- Controller match.

Example of web.xml file tradingapp DispatcherServlet tradingapp *.htm *** Any URL ending with an “.htm” pattern is routed to the DispatcherServlet, the DispatcherServlet loads the tradingapp- servlet.xml file and routes the user to the correct controller.

Our Demo Logon Form at URL

The tradingapp-servlet.xml file a.k.a. Spring beans XML file is where the majority of your configuration is done. For example: If working with the URL: /logon.htm Because the URL ends with.htm the DispatcherServlet loads the tradingapp-servlet.xml file to determine which controller to use. The tradingapp-servlet.xml file uses Springs SimpleUrlHandlerMapping class to map the URL to a controller, in this case the LogonFormController Next…what the tradingapp-servlet.xml looks like.

tradingapp-servlet.xml true credentials </property com.tradingapp.Credentials logon portfolio.htm This class extends Springs SimpleFormController Which defines a setSuccessView() method If it passes “validator” then successView, passes to portfolio.htm page

Review of the process so far User goes to this URL: Since the URL ends with “.htm”, the tradingapp-servlet.xml file is loaded to determine what controller to use. The says to refer to the Since the LogonFormController extends SimpleFormController we can use the methods defined in the SimpleFormController class to do all kinds of form checking, e.g. validation.

What our LogonFormController Looks Like. public class LogonFormController extends SimpleFormController { public ModelAndView onSubmit(Object command) throws ServletException { return new ModelAndView(new RedirectView(getSuccessView())); } Remember our tradingapp-servler.xml file? true credentials </property com.tradingapp.Credentials logon portfolio.htm If no validation errors, go here

successView /portfolio.htm

Where do I go if there is a validation error in my logon page? tradingapp-servler.xml true credentials </property com.tradingapp.Credentials logon portfolio.htm *** Your LogonFormController will check the validation “first” without writing any additional code because your LogonFormController extends Springs SimpleFormController. Next: The LogonValidator implements Springs Validator interface. On error go back to formView, that is where you started.

Logon page with error message Next: code for LogonValidator implements Springs Validator

Example code of validator tradingapp-servler.xml credentials </property com.tradingapp.Credentials logon portfolio.htm public class LogonValidator implements Validator { public void validate(Object obj, Errors errors) { Credentials credentials = (Credentials) obj; if (credentials.getPassword().equals("guest") == false) { errors.rejectValue("password", "error.login.invalid-pass", null, "Incorrect Password."); } } Next: Command/Form Backing Bean Command / form backing bean

Command/Form Backing Bean is a POJO public class Credentials { private String username; private String password; public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } Next: Why its called a “command” or “form backing bean”

Command/Form Backing Bean is a POJO logon.htm form Username: Password: public class Credentials { private String username; private String password; public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } The logon form is “backed” by the Credentials bean and given a commandName of “credentials” defined in out springapp-servlet.xml file. “credentials” will be our “command object” we will use to bind the form to the bean. Next: another look at springapp- servlet.xml file

springapp-servlet.xml file credentials </property com.tradingapp.Credentials logon portfolio.htm We use the commandName “credentials” with Spring’s tag library, to bind the Credentials bean to the logon form. Next: Code that shows logon form binding to commandName

logon form binding to commandName using Springs Tag Library DevX.com Stock-Trading System Logon <input type="text" name="username" Spring’s taglib has bound the bean to the form