+ All Categories
Home > Documents > Vinod Struts 2 Part 1

Vinod Struts 2 Part 1

Date post: 13-Apr-2015
Category:
Upload: sasirekha-perumalvijayan
View: 29 times
Download: 4 times
Share this document with a friend
59
Prof. Vinod Pillai. [email protected] http://vinodthebest.wordpress.com www.youtube.com/vinodthebest
Transcript
Page 1: Vinod Struts 2 Part 1

Prof. Vinod Pillai.

[email protected]

http://vinodthebest.wordpress.com

www.youtube.com/vinodthebest

Page 2: Vinod Struts 2 Part 1

Agenda Part – I : Understanding Basics Web concepts & Struts

Part – II : Setting Up Struts & Running basic App.

Part – III : Struts 2 UI Tags & Integrating Tiles.

Part – IV : Database + Session & Request Object.

2Prof. Vinod Pillai

Page 3: Vinod Struts 2 Part 1

About Me: Currently I am working with Chaudhari Technical Institute – MCA as Assistant

Professor since 2008.

Academic counselor in MCA & BCA Department of Indira Gandhi National OpenUniversity (IGNOU), Ahmedabad since 2009.

Visiting Faculty for MBA - Udaybhansinhji Regional Institute of CooperativeManagement (URICM), Gandhinagar since 2009.

Having 2.0 years of Industry experience + 4.0 teaching experience.

Done Master of Computer Science (Wollongong University - Australia) with 70%.

Done Bachelor of Computer Application (Gujarat University) with 87%.

Cleared SCJP (Java Standard Edition 6 Programmer Certified Professional) with95%.

Major areas: C, C++, Java, Advance Java, Android, NT-II, C Project & C++ Project.

Regular Jury Member of National Institute of Fashion Technology (NIFT-Gandhinagar) since 2009.

Guided more than 25 MCA Groups for their Final Year Project.

Guided 30 MCA Groups for C & C++ Project.

Guided one of the MCA students for her NASA project which is being appreciatedby a certificate. 3Prof. Vinod Pillai

Page 4: Vinod Struts 2 Part 1

Part – IUnderstanding

Basics Web Concepts &

Struts

4Prof. Vinod Pillai

Page 5: Vinod Struts 2 Part 1

Part – I : Understanding Struts Architecture.

Overview of Web Application.

Servlet.

JSP.

Java Beans.

MVC Architecture.

Simple MVC Base Application.

Understanding Struts 2 Architecture.

Prof. Vinod Pillai 5

Page 6: Vinod Struts 2 Part 1

Overview of Web Application Java Application = Desktop || Web || Mobile.

Web Application :

Generation data dynamically not just simple static htmlpages.

Having some forms or login page.

Example: Gmail, Youtube, Facebook and many more.

Web Application :

Simple Web Application.

Complex Web Application.

6Prof. Vinod Pillai

Page 7: Vinod Struts 2 Part 1

Java Web Application General flow of client request and response:

7Prof. Vinod Pillai

Page 8: Vinod Struts 2 Part 1

Java Web Application Key players for Java Web Application development:

Web Server.

Static HTML pages + CSS + JavaScript + Images + Videos.

Servlet.

JSP.

web.xml.

Library [If required].

8Prof. Vinod Pillai

Page 9: Vinod Struts 2 Part 1

Setting Up Web Applicaiton*Before we start we need the following tools:

JDK 1.7 (Download)

Eclipse Java EE IDE for Web Developers (Indigo)

Tomcat 7 or any other container (Glassfish, JBoss,

Websphere, Weblogic etc) (Download)

9Prof. Vinod Pillai

Page 10: Vinod Struts 2 Part 1

JDK 1.7

http://www.oracle.com/technetwork/java/javase/downloads/index.html 10Prof. Vinod Pillai

Page 11: Vinod Struts 2 Part 1

Eclipse Java EE IDE

http://eclipse.org/downloads/ 11Prof. Vinod Pillai

Page 12: Vinod Struts 2 Part 1

Apache Tomcat 7

http://tomcat.apache.org/download-70.cgi 12Prof. Vinod Pillai

Page 13: Vinod Struts 2 Part 1

Application Dev. [Eclipse] Starting Eclipse IDE

13Prof. Vinod Pillai

Page 14: Vinod Struts 2 Part 1

Getting Started

Selecting the workspace name:

14Prof. Vinod Pillai

Page 15: Vinod Struts 2 Part 1

Application Dev. [Eclipse] Eclipse Main Window

15Prof. Vinod Pillai

Page 16: Vinod Struts 2 Part 1

Hello World

HTML in

Eclipse16Prof. Vinod Pillai

Page 17: Vinod Struts 2 Part 1

Application Dev. [Eclipse] Step 2: [Creating Dynamic Web Project]

17Prof. Vinod Pillai

Page 18: Vinod Struts 2 Part 1

Open Eclipse and go to File -> New -> Project and selectDynamic Web Project in the New Project wizard screen.

18Prof. Vinod Pillai

Page 19: Vinod Struts 2 Part 1

Project Name: HelloWorld. Target runtime: <Select New Runtime> & show the path where you have unzipped the Tomcat Server. Dynamic web module version: 2.5.

19Prof. Vinod Pillai

Page 20: Vinod Struts 2 Part 1

HelloWorld Application Folder Structure

20Prof. Vinod Pillai

Page 21: Vinod Struts 2 Part 1

Creating HTML/JSP/Servlet:

21Prof. Vinod Pillai

Page 22: Vinod Struts 2 Part 1

Creating HTML/JSP/Servlet:

22Prof. Vinod Pillai

Page 23: Vinod Struts 2 Part 1

Running the application [Static / Dynamic Project]:

23Prof. Vinod Pillai

Page 24: Vinod Struts 2 Part 1

Running the application [Static / Dynamic Project]:

24Prof. Vinod Pillai

Page 25: Vinod Struts 2 Part 1

Final Output:

25Prof. Vinod Pillai

Page 26: Vinod Struts 2 Part 1

HTML Page

calling

Servlet26Prof. Vinod Pillai

Page 27: Vinod Struts 2 Part 1

Java Web Application - Servlet Servlet

A servlet is a small Java program that runs within a Webserver. Servlets receive and respond to requests from Webclients, usually across HTTP, the HyperText TransferProtocol.

Important methods: void doGet(HttpServletRequest request, HttpServletResponse

response)

void doPost(HttpServletRequest request, HttpServletResponse response)

27Prof. Vinod Pillai

Page 28: Vinod Struts 2 Part 1

Creating Servlet:

28Prof. Vinod Pillai

Page 29: Vinod Struts 2 Part 1

Servlet Code:

29Prof. Vinod Pillai

Page 30: Vinod Struts 2 Part 1

Telling HTML to call the Servlet:

30Prof. Vinod Pillai

Page 31: Vinod Struts 2 Part 1

JSP Page

calling

Servlet31Prof. Vinod Pillai

Page 32: Vinod Struts 2 Part 1

Java Web Application - JSP JSP [JavaServer Pages]

JavaServer Pages (JSP) technology allows you to easilycreate Web content that has both static and dynamiccomponents. JSP technology projects all the dynamiccapabilities of Java Servlet technology but provides amore natural approach to creating static content.

32Prof. Vinod Pillai

Page 33: Vinod Struts 2 Part 1

Creating JSP:

33Prof. Vinod Pillai

Page 34: Vinod Struts 2 Part 1

Creating JSP:

34Prof. Vinod Pillai

Page 35: Vinod Struts 2 Part 1

Java Web Application – Beans Javabean class is a type of java classes that is follow

some rules for methods and naming.

Major Rules of JavaBean class:

No-argument constructor.

The properties variables are private and accessed due theset and get methods.

The name of setter function is set followed by theproperty name but every first letter is upper case. (ex:setId() and getId()).

Setters and getters are public.

Getters have no parameter.

Setters have void return type. 35Prof. Vinod Pillai

Page 36: Vinod Struts 2 Part 1

Java Bean:

36Prof. Vinod Pillai

Page 37: Vinod Struts 2 Part 1

Java Web Application – web.xml According to the Servlet 2.4 specification, every Web

application should include a deployment descriptor(web.xml file). This file must be placed in the WEB-INF/ directory of the Web application.

Be very careful when making modifications to this file(such as any additions or changes) because they willaffect all Web applications running.

37Prof. Vinod Pillai

Page 38: Vinod Struts 2 Part 1

web.xml:

38Prof. Vinod Pillai

Page 39: Vinod Struts 2 Part 1

MVC

Architecture

39Prof. Vinod Pillai

Page 40: Vinod Struts 2 Part 1

MVC Architecture The MVC (Model-View-Controller) architecture is a

way of decomposing an application into three parts:

Model

View

Controller.

Model : A model represents an application’s data andcontains the logic for accessing and manipulating thatdata. Any data that is part of the persistent state of theapplication should reside in the model objects.

40Prof. Vinod Pillai

Page 41: Vinod Struts 2 Part 1

MVC Architecture View: View represents the presentation of the

application. The view object refers to the model. It usesthe query methods of the model to obtain the contentsand renders it. The view modifies itself when a changein the model is communicated to the view.

Controller : Whenever the user sends a request forsomething then it always go through the controller. Thecontroller is responsible for intercepting the requestsfrom view and passes it to the model for the appropriateaction. After the action has been taken on the data, thecontroller is responsible for directing the appropriateview to the user.

41Prof. Vinod Pillai

Page 42: Vinod Struts 2 Part 1

Model View Controller (MVC)

42Prof. Vinod Pillai

Page 43: Vinod Struts 2 Part 1

MVC Application [Add two integer values]

43Prof. Vinod Pillai

Page 44: Vinod Struts 2 Part 1

Controller [Servlet]:

44Prof. Vinod Pillai

Page 45: Vinod Struts 2 Part 1

Model [Java Class / Bean or Both]:

45Prof. Vinod Pillai

Page 46: Vinod Struts 2 Part 1

View [Result JSP]:

46Prof. Vinod Pillai

Page 47: Vinod Struts 2 Part 1

Output:

47Prof. Vinod Pillai

Page 48: Vinod Struts 2 Part 1

Struts

Framework

48Prof. Vinod Pillai

Page 49: Vinod Struts 2 Part 1

Introduction Java Application = Desktop || Web || Mobile.

Struts 2= Apache Struts Web Framework.

Framework:

Peace of Software that Automates all tedioustask.

Use Design patterns commonly agreed byIndustry.

In built features that commonly needed by mostof project.

49Prof. Vinod Pillai

Page 50: Vinod Struts 2 Part 1

Jakarta Struts The Apache Struts web framework is a free open-

source solution for creating Java web applications.

Apache Struts was launched in May 2000, withversion 1.0 officially released in July 2001.

Why we need it?

Web applications based on JSP sometimes combinesdatabase code, page design code, and control flow code.

In practice, we find that unless these concerns areseparated, larger applications become difficult tomaintain.

50Prof. Vinod Pillai

Page 51: Vinod Struts 2 Part 1

Jakarta Struts One way to separate concerns in a software

application => Model-View-Controller (MVC)architecture.

MVC

Model = Business or database code

View = Page design code

Controller = Navigational code.

Struts framework is designed to help developerscreate web applications that utilize a MVCarchitecture.

51Prof. Vinod Pillai

Page 52: Vinod Struts 2 Part 1

Struts 2 Apache Struts Project offers two major versions:

Struts 1 is recognized as the most popular web applicationframework for Java.

Struts 2 was originally known as WebWork 2.WebWorkand Struts communities joined forces to create Struts 2.

Struts 2 is a pull-MVC framework. i.e. the data that is tobe displayed to user has to be pulled from the Action.

Action class in Struts 2 act as the model in the webapplication.

Unlike Struts, Struts 2 Action class are plain POJOobjects thus simplifying the testing of the code.

52Prof. Vinod Pillai

Page 53: Vinod Struts 2 Part 1

Struts 2 Struts2 also comes with power APIs to configure

Interceptors that reduce greatly the coupling inapplication.

The view part of Struts 2 is highly configurable and itsupports different result-types such as Velocity,FreeMarker, JSP, etc.

53Prof. Vinod Pillai

Page 54: Vinod Struts 2 Part 1

Struts 2 Architecture

54Prof. Vinod Pillai

Page 55: Vinod Struts 2 Part 1

Struts 2 Architecture1. Request is generated by user and sent to Servlet

container [web.xml].

2. Servlet container invokes FilterDispatcher filter which inturn transfer the call to [struts.xml] and finds theappropriate action .

3. One by one Intercetors are applied before calling theAction. Interceptors performs tasks such as Logging,Validation, File Upload, Double-submit guard etc.

4. Action is executed and the Result is generated by Actionif the state of Model is to be Change then do so.

55Prof. Vinod Pillai

Page 56: Vinod Struts 2 Part 1

Struts 2 Architecture5. The output of Action is rendered in the view (JSP,

Velocity, etc) and the result is ready to returned to theuser.

6. The response passes through the interceptors in reverseorder to perform any clean-up or additional processing.

7. Now the Control is with [Servlet Engine] and the resultis rendered to the user.

56Prof. Vinod Pillai

Page 57: Vinod Struts 2 Part 1

Part – I : Understanding Struts Architecture.

Overview of Web Application.

Servlet.

JSP.

Java Beans.

MVC Architecture.

Simple MVC Base Application.

Understanding Struts 2 Architecture.

Prof. Vinod Pillai 57

Page 58: Vinod Struts 2 Part 1

Part – I

Completed

58Prof. Vinod Pillai

Page 59: Vinod Struts 2 Part 1

Thank You

[email protected]

59Prof. Vinod Pillai


Recommended