+ All Categories
Home > Education > Vinod sir struts 2 part 1

Vinod sir struts 2 part 1

Date post: 17-May-2015
Category:
Upload: vinod-pillai
View: 2,760 times
Download: 1 times
Share this document with a friend
Description:
Basic Java Web Application using Eclipse, MVC Architecture & Struts Architecture.
Popular Tags:
59
Prof. Vinod Pillai. [email protected] http://vinodthebest.wordpress.com www.youtube.com/vinodthebest
Transcript
Page 1: Vinod sir struts 2   part 1

Prof. Vinod Pillai.

[email protected]

http://vinodthebest.wordpress.com

www.youtube.com/vinodthebest

Page 2: Vinod sir 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 sir 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 sir struts 2   part 1

Part – IUnderstanding

Basics Web Concepts &

Struts

4Prof. Vinod Pillai

Page 5: Vinod sir 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 sir 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 sir struts 2   part 1

Java Web Application General flow of client request and response:

7Prof. Vinod Pillai

Page 8: Vinod sir 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 sir 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 sir struts 2   part 1

JDK 1.7

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

Page 11: Vinod sir struts 2   part 1

Eclipse Java EE IDE

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

Page 12: Vinod sir struts 2   part 1

Apache Tomcat 7

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

Page 13: Vinod sir struts 2   part 1

Application Dev. [Eclipse] Starting Eclipse IDE

13Prof. Vinod Pillai

Page 14: Vinod sir struts 2   part 1

Getting Started

Selecting the workspace name:

14Prof. Vinod Pillai

Page 15: Vinod sir struts 2   part 1

Application Dev. [Eclipse] Eclipse Main Window

15Prof. Vinod Pillai

Page 16: Vinod sir struts 2   part 1

Hello World

HTML in

Eclipse16Prof. Vinod Pillai

Page 17: Vinod sir struts 2   part 1

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

17Prof. Vinod Pillai

Page 18: Vinod sir 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 sir 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 sir struts 2   part 1

HelloWorld Application Folder Structure

20Prof. Vinod Pillai

Page 21: Vinod sir struts 2   part 1

Creating HTML/JSP/Servlet:

21Prof. Vinod Pillai

Page 22: Vinod sir struts 2   part 1

Creating HTML/JSP/Servlet:

22Prof. Vinod Pillai

Page 23: Vinod sir struts 2   part 1

Running the application [Static / Dynamic Project]:

23Prof. Vinod Pillai

Page 24: Vinod sir struts 2   part 1

Running the application [Static / Dynamic Project]:

24Prof. Vinod Pillai

Page 25: Vinod sir struts 2   part 1

Final Output:

25Prof. Vinod Pillai

Page 26: Vinod sir struts 2   part 1

HTML Page

calling

Servlet26Prof. Vinod Pillai

Page 27: Vinod sir 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 sir struts 2   part 1

Creating Servlet:

28Prof. Vinod Pillai

Page 29: Vinod sir struts 2   part 1

Servlet Code:

29Prof. Vinod Pillai

Page 30: Vinod sir struts 2   part 1

Telling HTML to call the Servlet:

30Prof. Vinod Pillai

Page 31: Vinod sir struts 2   part 1

JSP Page

calling

Servlet31Prof. Vinod Pillai

Page 32: Vinod sir 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 sir struts 2   part 1

Creating JSP:

33Prof. Vinod Pillai

Page 34: Vinod sir struts 2   part 1

Creating JSP:

34Prof. Vinod Pillai

Page 35: Vinod sir 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 sir struts 2   part 1

Java Bean:

36Prof. Vinod Pillai

Page 37: Vinod sir 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 sir struts 2   part 1

web.xml:

38Prof. Vinod Pillai

Page 39: Vinod sir struts 2   part 1

MVC

Architecture

39Prof. Vinod Pillai

Page 40: Vinod sir 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 sir 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 sir struts 2   part 1

Model View Controller (MVC)

42Prof. Vinod Pillai

Page 43: Vinod sir struts 2   part 1

MVC Application [Add two integer values]

43Prof. Vinod Pillai

Page 44: Vinod sir struts 2   part 1

Controller [Servlet]:

44Prof. Vinod Pillai

Page 45: Vinod sir struts 2   part 1

Model [Java Class / Bean or Both]:

45Prof. Vinod Pillai

Page 46: Vinod sir struts 2   part 1

View [Result JSP]:

46Prof. Vinod Pillai

Page 47: Vinod sir struts 2   part 1

Output:

47Prof. Vinod Pillai

Page 48: Vinod sir struts 2   part 1

Struts

Framework

48Prof. Vinod Pillai

Page 49: Vinod sir 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 sir 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 sir 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 sir 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 sir 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 sir struts 2   part 1

Struts 2 Architecture

54Prof. Vinod Pillai

Page 55: Vinod sir 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 sir 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 sir 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 sir struts 2   part 1

Part – I

Completed

58Prof. Vinod Pillai

Page 59: Vinod sir struts 2   part 1

Thank You

[email protected]

59Prof. Vinod Pillai


Recommended