Struts - Day2

  • July 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Struts - Day2 as PDF for free.

More details

  • Words: 2,207
  • Pages: 40
>> Delivering Value

Struts 2.0 Session - 2 May, 2009 Presentation By Training & Development

Introduction  On Day 1, we discussed the Welcome Back! Lets start with a small recap of Day 1

following: – Introduction to Software Framework – Struts Background

 Day 2 – Today we will be discussing

about things that you should know before you start learning Struts 2

2

Mr. Thinker What are the attributes of HTTP?

HTTP is stateless and text based

3

Mr. Thinker How does Servlet resolve the issue with HTTP?

Servlet can remember HTTP Requests using Http session and other means. Servlet can also handle numbers etc by converting to suitable types

4

Mr. Thinker What are the main problems with Servlets?

Separation of Concern is nonexistent. HTML code is embedded in Java making it difficult to maintain

5

Dumbo Vs. Jumbo Jumbo! Does JSPs completely replace Servlets?

No Dumbo. JSPs still get converted to Servlets. So, for each JSP we see, there will be a corresponding servlet in the background

6

Mr. Thinker What are the advantages of JSPs?

Problems with Servlets are solved to an extent. Changes to HTML are easier since JSPs are basically HTML + Java

7

Mr. Thinker What are the problems with JSPs?

Separation of concern is still a problem. Java code is embedded in HTML which is equally undesirable

8

Mr. Thinker What is the View layer generally made of?

View Layer is generally implemented using JSPs

9

Dumbo Vs. Jumbo What puzzle does MVC solve?

Separation of concerns. The three layers are totally separated

10

Dumbo Vs. Jumbo Jumbo! Tell something about Struts?

It implements MVC Design Pattern. It is a framework which helps reduce the development effort

11

Things you should know before you learn Struts 2

12

Filters  Before we jump into Struts 2.0 sessions we will be undertaking a crash

course on few of the concepts that all of us should be aware of.

 Filters belong to Servlet specification

13

Filters My name is Session filter. Like authentication filter, I also grab each request object before it reaches the servlet. My responsibility is to check whether the session is still alive. Only if alive, I allow the request to proceed further with the business process. Coming back to this request, yes the session is valid, over to the servlet

HTML Client Hi Server, Post these customer details to the database

Filters

My name is authentication filter. I just grab every request before it reaches the servlet. My responsibility is to check whether the user passing the request has been authenticated. Only if he is authenticated I will allow the request to proceed further. Coming back to this request, yes he is a valid user, so over to the next filter

Server and Servlet Yes my life has become simpler after these guys came into existence. I only concentrate on my job. All the redundant jobs are now passed on to Filters to handle. Coming back to this request, “abracadabra… Done!” 14

Filters in a nutshell  Filter is an object that can transform a request or modify a response  They are preprocessors of the request before it reaches the servlet or

postprocessors of the response leaving a servlet

15

Dependency Injection • Dependency Injection refers to the process of supplying an external

dependency to a software component

Traditional Love

I am in love with Juliet. I am so much dependent on her. I am going to propose to her MYSELF. After all I need her to run my Life 

16

Dependency Injection Dependency Injection Love I am in love with Juliet. I am so much dependent on her. After all I need her to run my Life. All is fine. But I am NOT going to propose to her DIRECTLY. I will give all details about her to the God who created me. I will pray to him to safely get her to me. Oh God! Please help. I want her!! Without her I am as good as dead.

17

Technically Speaking Traditional Love Class public class Romeo { private MyLove juliat; public void spendRestOfMyLife( ) { this.juliat = LoveFactory.getMyLove( ); //I have got what I want. Now my life will be sweet juliat.spendLifeTogether( ); }

18

Dependency Injection //Juliat prays to Love God using this interface. He expresses his desire through this interface public interface LovePrayer { public void setMyLove(MyLove loverObject); } //Juliat prays to Love God by implementing LovePrayer. //Love God promptly responds by setting Juliat into Romeo’s “MyLove” public class Romeo implements LovePrayer { private MyLove juliat; public void setMyLove(MyLove juliat) { this.juliat = juliat;

}

} public void spendRestOfMyLife( ) { //God has given me what I want. Now my life will be sweet juliat.spendLifeTogether( ); }

19

Dependency Injection in a Nutshell  Conventionally, if an object A needs to gain access to a particular object B,

object A takes responsibility to access Object B: either it holds a direct reference to B, or it goes to a known service locator and requests for a reference to B

 By contrast, using dependency injection, Object A simply provides a

property that can hold a reference to Object B; then, when the object A is created, a reference to Object B will automatically be injected into that property by an external mechanism.

20

Annotations  Annotations are syntactic form of metadata  Annotations are added to the Java Source Code  Annotations are not method calls  Annotations will not by themselves do anything and will be used by some

other program / container

21

Annotations - Example @Stateless public class CalculatorBean implements Calculator { public double calculate (int start, int end, double growthrate, double saving) { double tmp = Math.pow(1. + growthrate / 12., 12. * (end - start) + 1); return saving * 12. * (tmp - 1) / growthrate; } }

22

Annotations  In the above example, the annotation states that the EJB is a stateless

Session Bean

 That is all is required to make a normal class a stateless session bean  Without Annotation (i.e. @Stateless) there used to be too much of XML

configurations to achieve the same

23

Command Pattern

24

Dumbo Vs. Jumbo Oh No! One more concept. I am fed up with this guy!

This is going to be interesting, if you listen carefully. Moreover, this is a basic building block of Struts. So, more the reason to listen. I am all ears.

25

Command Pattern  As Mr. Jumbo, said this is a very important concept to understand before we

move on

 Let us try to explain this with an example

26

Command Pattern Hi everybody! We are here to make you understand the usage and beauty of command pattern. Hope we will be successful in our attempt”

27

Command Pattern I do two important things. Either I start rotating or stop rotating

It is my responsibility to either switch on or off the light. Like my peer neither am I a genius

It is my responsibility to either switch on the Fan or switch off the Fan. Don’t think that I am a genius. You will understand why I am not

I too do two important things. Either I start glowing or stop glowing

28

Command Pattern I am the end user. I use the Fan and the Light and I use the corresponding switches to switch them on or off. In other words, I interact with Fan and Light through the Switch

29

Command Pattern - Terminologies to Know  ON and OFF – These are the COMMANDS that the end user is interested

in

 Switch (Fan’s and Light’s) – We are the INVOKERs of the command  FAN and Light – RECEIVERs of the command. In other words only we

know how to perform the commands that we receive. The switches are mere postmen passing on the command that they receive from the end user to us. Now you know why they told they are not geniuses. They are in fact dumb like Mr. dumbo!!

30

Command Pattern - Terminologies to Know  ON and OFF – These are the COMMANDS that the end user is interested

in

 Switch (Fan’s and Light’s) – We are the INVOKERs of the command  FAN and Light – RECEIVERs of the command. In other words only we

know how to perform the commands that we receive. The switches are mere postmen passing on the command that they receive from the end user to us. Now you know why they told they are not geniuses. They are in fact dumb like Mr. dumbo!!

31

Dumbo Vs. Jumbo Wait, before we go any further, what problem are we trying to solve using command pattern? Why should I use command pattern? Without explaining this, this guy is in his own world!!

Thanks Jumbo, it was really helpful. Hope this guy learns from you!!

OK Cool down Mr. Dumbo. I understand. Let me explain. • Let us take the above example. “A Switch is a Switch”. In other words, a Switch can be used as a switch for a Fan or a Light or literally anything. • So, the switch should NOT know anything about how a FAN switches on or how a Light starts glowing. You know what I mean? • In philosophical terms, the Switch and the Fan should be detached in terms of knowing about each other. Even though they are connected, the switch should not know anything about the Fan or the light • How can we achieve this decoupling? This is the problem we are trying to solve!!

32

How is the Command Pattern Implemented?  Declare an interface called Command with a method called execute (These

are just names you can name them as you like)

 Have implementations for each command: – LightOnCommand – LightOffCommand – FanOnCommand – FanOffCommand  Please note: The above implementations will NOT actually implement the

logic for various commands in their execute method. It will simply invoke the business logic method of FAN or LIGHT

 Define a class called Switch – Constructor gets two command objects (one for on and one for Off) – Have two public methods one for on and one for off, which simply

invokes the corresponding on / off methods of the command

33

Sample Code class Fan { public void startRotate() { System.out.println("Fan is rotating"); } public void stopRotate() { System.out.println("Fan is not rotating"); } }

class Light { public void turnOn( ) { System.out.println("Light is on "); } public void turnOff( ) { System.out.println("Light is off"); } } 34

Sample Code class Switch { private Command UpCommand, DownCommand; public Switch( Command Up, Command Down) { UpCommand = Up; // concrete Command registers itself with t he invoker DownCommand = Down; } void flipUp( ) { // invoker calls back concrete Command, which executes the Command on the receiver UpCommand . execute ( ) ; } void flipDown( ) { DownCommand . execute ( ); } }

35

Sample Code public interface Command { public abstract void execute ( ); } class LightOnCommand implements Command { private Light myLight; public LightOnCommand ( Light L) { myLight = L; } public void execute( ) { myLight . turnOn( ); } } class LightOffCommand implements Command { private Light myLight; public LightOffCommand ( Light L) { myLight = L; } public void execute( ) { myLight . turnOff( ); } }

class FanOffCommand implements Command { private Fan myFan; public FanOffCommand ( Fan F) { myFan = F; } public void execute( ) { myFan . stopRotate( ); } } public class TestCommand { public static void main(String[] args) { Light testLight = new Light( ); LightOnCommand testLOC = new LightOnCommand(testLight); LightOffCommand testLFC = new LightOffCommand(testLight); Switch testSwitch = new Switch( testLOC,testLFC); testSwitch.flipUp( ); testSwitch.flipDown( ); Fan testFan = new Fan( ); FanOnCommand foc = newFanOnCommand(testFan); FanOffCommand ffc = new FanOffCommand(testFan); Switch ts = new Switch( foc,ffc); ts.flipUp( ); ts.flipDown( ); } } 36

Dumbo Vs. Jumbo For the benefit of others, can you please summarize the command pattern and its usage

Yes Sir. • As you can see from the above example, the Switch class does not know any implementation logic of Fan and Light • Because of the power of Interface and Call Back, everything seems so hidden from the Switch • The details are completely abstracted until the command reaches the Fan and Light classes where the commands are actually implemented

37

The Easy Going Life of a Switch I am bored with this Light. Tomorrow I am going to request the electrician to connect me to a Television. Thanks to the Command Pattern, it is so easy for me to switch to a different Receiver

38

Summary of Day 2 Hope you had a nice session today! Many of you might have been aware of what we taught today. However these are very important concepts which form the basic building block of Struts framework. Hence it was important for us to go through them in detail. From tomorrow we will start Struts 2. Till then it is bye bye from us

39

Session 2 Thank You !

40

Related Documents

Struts - Day2
July 2020 2
Day2
October 2019 21
Day2
July 2020 1
Struts
June 2020 26
Struts
November 2019 49
Struts
May 2020 36