Struts2-Spring=Hibernate

53

description

Spring, Struts2, Hibernate

Transcript of Struts2-Spring=Hibernate

Page 1: Struts2-Spring=Hibernate
Page 2: Struts2-Spring=Hibernate

Get Trained on:

Page 3: Struts2-Spring=Hibernate

Workshop Agenda

Day 1

1.Understanding Struts2 2.Architecture of Struts2

3.Features of Struts2

4.Environment Setup

5.Understanding Configuration of Project

Page 4: Struts2-Spring=Hibernate

Day 1 Continue

6.Practical Lab

7.Introduction to Hibernate

8.Demo of Struts2 Application

Page 5: Struts2-Spring=Hibernate

Understanding Struts2

•Apache Struts 2 is an elegant, extensible framework for creating enterprise-ready Java web applications.

•The framework is designed to streamline the full development cycle, from building, to deploying, to maintaining applications over time.

•To make web development easier for the developers.

Page 6: Struts2-Spring=Hibernate

MVC Design Pattern

Page 7: Struts2-Spring=Hibernate

Architecture of Struts2

Page 8: Struts2-Spring=Hibernate

What kind of MVC is Struts2

Struts 2 is pull MVC

Page 9: Struts2-Spring=Hibernate

Request Life Cycle

• User sends a request to the server for requesting for some resource (i.e pages).

• The FilterDispatcher looks at the request and then determines the appropriate Action.

• Configured interceptors functionalities applies such as validation, file upload etc.

Page 10: Struts2-Spring=Hibernate

• Selected action is executed to perform the requested operation.

• Again, configured interceptors are applied to do any post-processing if required.

• Finally the result is prepared by the view and returns the result to the user.

Page 11: Struts2-Spring=Hibernate

Features of Struts2

• POJO forms and POJO actions

• Tag Support

• AJAX Support

• Easy Integration

Page 12: Struts2-Spring=Hibernate

Features continued…

• Template Support

• Plugin Support

• Profiling

• Easy to modify tags

• Promote less configuration

• View Technologies (JSP, Velocity, Freemarker,etc)

Page 13: Struts2-Spring=Hibernate

Environment Setup

4 Simple Steps:

•Step 1 - Setup Java Development Kit (JDK)

•Step 2 - Setup Apache Tomcat

•Step 3 - Setup Eclipse (IDE)

•Step 4 - Setup Struts2 Libraries

Page 14: Struts2-Spring=Hibernate

Understand Configuring Project 4 Simple Steps

•Create an Action Class

•Create a View

•Create a launch page

•Now configure all of them

Page 15: Struts2-Spring=Hibernate

Create an Action Classpublic class Student{ private String fullName;

public String execute() throws Exception { return "success"; } public String getFullName() { return name; }

public void setFullName(String name) { this.name = name; }}

Page 16: Struts2-Spring=Hibernate

Create View

<%@ page contentType="text/html; charset=UTF-8" %><%@ taglib prefix="s" uri="/struts-tags" %><html><head><title>Welcome to Team Go Getters</title></head><body> Hello, We are glad to have <s:property value=”fullName"/> as our student.</body></html>

Page 17: Struts2-Spring=Hibernate

Create a launch page<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%><%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><title>Struts2 Workshop</title></head><body> <h1>Welcome to Team Go Getters</h1> <form action=”welcome"> <label for="name">Please enter your name</label><br/> <input type="text" name=”fullName"/> <input type="submit" value=”Enroll"/> </form></body></html>

Page 18: Struts2-Spring=Hibernate

Configuration Files

Struts.xml file

•Mapping between URL ,Action classes and Result Types (View)

•Since Struts 2 requires struts.xml to be present in classes folder.

• So create struts.xml file under the WebContent/WEB-INF/classes folder.

Page 19: Struts2-Spring=Hibernate

Struts.xml

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts><constant name="struts.devMode" value="true" /> <package name=”workshop" extends="struts-default"> <action name=”welcome" class="com.teamgogetters.struts2.Student" method="execute"> <result name="success">/Welcome.jsp</result> </action> </package></struts>

Page 20: Struts2-Spring=Hibernate

Multiple struts.xml files

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts> <include file="my-struts1.xml"/> <include file="my-struts2.xml"/></struts>

Page 21: Struts2-Spring=Hibernate

Web.xml

• Entry point for any request to Struts 2

• Mapping FilterDispatcher

• Deployment Descriptor

• Security Parameters

Page 22: Struts2-Spring=Hibernate

Web.xml<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Struts 2</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class> </filter>

<filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping></web-app>

Page 23: Struts2-Spring=Hibernate

Interceptors

• Providing preprocessing logic before the action is called.

• Providing post-processing logic after the action is called.

• Catching exceptions so that alternate processing can be performed.

Page 24: Struts2-Spring=Hibernate

List of Interceptors

• timer• params• checkbox• createSession• logger• fileUpload• scope• alias

Page 25: Struts2-Spring=Hibernate

Snippet<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts> <constant name="struts.devMode" value="true" /> <package name=”workshop" extends="struts-default"> <action name=”welcome" class="com.teamgogetters.struts2.Student" method="execute"> <interceptor-ref name="params"/> <interceptor-ref name="timer" /> <result name="success">/welcome.jsp</result> </action> </package></struts>

Page 26: Struts2-Spring=Hibernate

Result and Result Types

• <results> tag plays the role of a view in the Struts2 MVC framework. The action is responsible for executing the business logic. The next step after executing the business logic is to display the view using the <results> tag.

• Struts comes with a number of predefined result types and whatever we've already seen that was the default result type dispatcher, which is used to dispatch to JSP pages. Struts allow you to use other markup languages for the view technology to present the results and popular choices include Velocity, Freemaker, XSLT and Tiles.

Page 27: Struts2-Spring=Hibernate

Snippet

<result name="success" type="dispatcher"> <param name="location"> /HelloWorld.jsp </param ></result>

<result name="success" type="freemarker"> <param name="location"> /hello.fm </param></result>

<result name="success" type="freemarker"> <param name="location"> /hello.fm </param></result>

Page 28: Struts2-Spring=Hibernate

Practical Lab

Page 29: Struts2-Spring=Hibernate

JDBC is going obsolete?

• Complex if it is used in large projects

• Large programming overhead

• No encapsulation

• Hard to implement MVC concept

• Query is DBMS specific

Page 30: Struts2-Spring=Hibernate

What’s Next ?

Page 31: Struts2-Spring=Hibernate

Hibernate: ORM

• Hibernate is a high-performance Object/Relational persistence and query service

• Hibernate takes care of the mapping from Java classes to database tables

• Hibernate data query and retrieval facilities

Page 32: Struts2-Spring=Hibernate

ORM New Generation

1. Let business code access objects rather than DB tables.

2. Hides details of SQL queries from OO logic.

3. Based on JDBC 'under the hood’

4. No need to deal with the database implementation.

Page 33: Struts2-Spring=Hibernate

ORM New Generation…

5. Entities based on business concepts rather than database structure.

6. Transaction management and automatic key generation.

7. Fast development of application.

Page 34: Struts2-Spring=Hibernate

Visual presentation of Hibernate

Page 35: Struts2-Spring=Hibernate

Demo

Page 36: Struts2-Spring=Hibernate

Day 2 Agenda

1.Day 1 Glimpse 2.Architecture of Hibernate

3.Core classes of Hibernate

4.Hibernate configuration

5.States of Persistent Class Instances

Page 37: Struts2-Spring=Hibernate

7. Overview of Caching in Hibernate

8. Understanding Spring

9. Why Spring?

10. Core Concepts of Spring

11. Spring Architecture

12. Architecture of Live Project

Page 38: Struts2-Spring=Hibernate

Architecture of Hibernate

Page 39: Struts2-Spring=Hibernate

Detailed Architecture

Page 40: Struts2-Spring=Hibernate

Core classes

• Configuration : Loads Configuration.

• SessionFactory : Only one per database. Thread safe.

• Session : Not thread safe.

• Transaction : Handle transaction management.

• Query : SQL or HQL

• Criteria: Object oriented retrieval from database.

Page 41: Struts2-Spring=Hibernate

Hibernate Configuration

Page 42: Struts2-Spring=Hibernate

States of Persistent Class Instance

• transient: A new instance of a a persistent class which is not associated with a Session and has no representation in the database and no identifier value is considered transient by Hibernate.

• persistent: You can make a transient instance persistent by associating it with a Session. A persistent instance has a representation in the database, an identifier value and is associated with a Session.

• detached: Once we close the Hibernate Session, the persistent instance will become a detached instance.

Page 43: Struts2-Spring=Hibernate

Overview of Caching

Page 44: Struts2-Spring=Hibernate

Understanding Spring• Spring framework is an open source Java platform that

provides comprehensive infrastructure support for developing robust Java applications very easily and very rapidly.

• Spring framework is an open source Java platform and it was initially written by Rod Johnson and was first released under the Apache 2.0 license in June 2003.

• It is simple because it just requires POJOs

Page 45: Struts2-Spring=Hibernate

Why Spring?• Spring is organized in a modular fashion.

• Spring does not reinvent the wheel

• Testing an application written with Spring is simple because environment-dependent code is moved into this framework

• Spring's web framework is a well-designed web MVC framework

• Spring provides a consistent transaction management interface

Page 46: Struts2-Spring=Hibernate

Core Concepts

• Dependency Injection

• Aspect oriented programming

Page 47: Struts2-Spring=Hibernate

Dependency Injection “Simple. Inject dependent objects.”

Page 48: Struts2-Spring=Hibernate

Aspect Oriented Programming ““Isolate function (cross cutting concerns) from objects they

affect””

Page 49: Struts2-Spring=Hibernate

Spring Architecture

Page 50: Struts2-Spring=Hibernate

Architecture of Live Project

Page 51: Struts2-Spring=Hibernate

Demo of Live Project

Page 52: Struts2-Spring=Hibernate

Thank you from :

Page 53: Struts2-Spring=Hibernate

Reach Us

Name: Jay Shah(Founder of Team Go Getters)

Contact No: +918980965112

Personal Email: [email protected]