01 introduction to struts2

Preview:

Citation preview

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

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.

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

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.

MVC

What if you have to desing 100 MVC web applications?

Struts 2 MVC Framework

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.

• 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.

• 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.

• 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.

• 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.

• 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.

• 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.

Models are two types:

• Models are two types.

- Data class

- Service class

MVC:

• M – Model

• V – View

• C – Controller.

• Explanation of MVC

• Model:

- Explain model here

• 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.

• Generally controller can be a servlet or filter.

• 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.

• Explain first example

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

• And struts history.

• Explain Struts 2.x.

• 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.

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

• Explain second example by showing filter as a controller.

Summary

• In this lesson, you should have learnt about