28
Introduction to Struts2

01 introduction to struts2

Embed Size (px)

Citation preview

Page 1: 01 introduction to struts2

Introduction to Struts2

Page 2: 01 introduction to struts2

Course Objectives• After completing this course, you should be able

to do the following:About Struts 2 Framework vs Pattern MVC Struts 2 MVC About Struts 2 Struts 2 features Struts 2 Architecture Request Life cycle Struts 2 application component Struts 2 Hello World example

Page 3: 01 introduction to struts2

About Struts 2

• Its MVC framework.• Version 2• Gives us prebuilt classes for MVC that we can use / extend.• The Struts 2 provides supports to POJO based actions,

Validation Support, AJAX Support, Integration support to various frameworks such as Hibernate, Spring, Tiles etc, support to various result types such as Freemarker, Velocity, JSP etc.

Page 4: 01 introduction to struts2

Struts 2 Features

The important features of struts 2 framework are as follows:• Configurable MVC components• POJO based actions• AJAX support• Integration support• Various Result Types• Various Tag support• Theme and Template support

Page 5: 01 introduction to struts2

Framework vs Pattern

• Pattern is the way you can architect your application.

• Framework provides foundation classes and libraries.

• Gets us quickly started.• Leverages industry best practices.

Page 6: 01 introduction to struts2

MVC

Page 7: 01 introduction to struts2

What if you have to desing 100 MVC web applications?

Page 8: 01 introduction to struts2
Page 9: 01 introduction to struts2

Struts 2 MVC Framework

Page 10: 01 introduction to struts2

History of MVC:

• With invention of JSP’s people are so happy.• They used to write lot of JSP’s with lot of java

code inside them.• These JSP’s are very difficult to debug.• So writing lot of java code in JSP’s is not good.

Page 11: 01 introduction to struts2

• To avoid this difficulty experts introduced JSTL.• As well as we can write our own custom tags by

using which we can avoid java code from JSP.• But it is also too difficult to develop lot of our

own custom tags.

Page 12: 01 introduction to struts2

• So we decided to use combination of JSP and servlet.

• To execute java code use servlet and to display any thing use JSP’s.

• According to this if we give request to any thing first one servlet will be executed.

Page 13: 01 introduction to struts2

• That servlet call all the service code and DAO code.

• Finally servlet will get some data that has to be displayed back to browser.

• That data is called as model.

• Then servlet forwards request to a JSP along with the data object.

Page 14: 01 introduction to struts2

• Then JSP will display the data.

• So in JSP write only presentation logic don’t write business logic.

• So all of our presentation logic should be in JSP’s.

• And this architecture is called as Model1 architecure.

Page 15: 01 introduction to struts2

• So according to model 1 architecture our application will have lot of JSP’s with buttons and links pointing to other JSP’s.

• Tomorrow if we change any one JSP page name then we need to change that in all other places which is not possible.

Page 16: 01 introduction to struts2

• So Model1 is having some problems.

• So model1 is also called as page centric.

• That’s why we have one more architecture called as Model2 architecture.

Page 17: 01 introduction to struts2

Models are two types:

• Models are two types.

- Data class

- Service class

Page 18: 01 introduction to struts2

MVC:

• M – Model

• V – View

• C – Controller.

• Explanation of MVC

Page 19: 01 introduction to struts2

• Model:

- Explain model here

Page 20: 01 introduction to struts2

• Controller:

- Controller is a class which decides what logic to be executed when ever your application getting a request.

- Controller is a class which decides what view to be displayed when ever your application getting a request.

Page 21: 01 introduction to struts2

• Generally controller can be a servlet or filter.

Page 22: 01 introduction to struts2

• View:

- View is the page or some thing else that is used for generating output that is visible to user or client.

- Generally and mostly we use JSP as controller.

- But in the market there are lot of other view technologies are also available. ex: velocity.

Page 23: 01 introduction to struts2

• Explain first example

Page 24: 01 introduction to struts2

• Now explain struts 1.x which will give us ready made controller.

• And struts history.

• Explain Struts 2.x.

Page 25: 01 introduction to struts2

• In struts 1.x controller is a servlet.

• In struts 2.x filter is a controller.

• Why:

- Because if we use filter as a controller, then that filter will be executed even we are giving request for a static file like CSS.

Page 26: 01 introduction to struts2

- If controller is a servlet that will be executed only for dynamic requests.

Page 27: 01 introduction to struts2

• Explain second example by showing filter as a controller.

Page 28: 01 introduction to struts2

Summary

• In this lesson, you should have learnt about