>> Delivering Value

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

Introduction
Good Evening Guys! We are into the third day. Many of you might be wondering about the fact that we have not even started thinking about Struts2

I agree we have been slow to start with. But that is with a reason. If you understand the concepts clearly then the Struts2 journey would be much easier and pleasant. Today we will gradually start talking about Struts2 Lets start with a recap of the first 2 days.

2

Dumbo Vs. Jumbo
Thanks Natasha. Jumbo! Can we do a recap of first two days?

On Day1, HTTP, its attributes and disadvantages Servlet, its attributes, advantages and disadvantages JSP, its attributes, advantages and disadvantages MVC Design Pattern, Struts

3

Dumbo Vs. Jumbo

On Day2: Filters Dependency Injection Annotation Command Pattern

4

Mr. Thinker
What are the attributes of HTTP? HTTP is stateless and text based

5

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

6

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

7

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

8

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

9

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

10

Mr. Thinker
What is the View layer generally made of? View Layer is generally implemented using JSPs

11

Dumbo Vs. Jumbo
What puzzle does MVC solve?

Separation of concerns. The three layers are totally separated

12

Dumbo Vs. Jumbo
Jumbo! Tell something about Struts?

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

13

Mr. Thinker
How do Filters help? Repetitive tasks can be moved to a Filter. (Eg. Session checking, authentication etc)

14

Mr. Thinker
Can filters act on both request and response? Yes Filters can act on prerequest as well as postresponse.

15

Mr. Thinker
How is dependency injection achieved generally? Dependency Injection is achieved generally by performing Setter Injection.

16

Dumbo Vs. Jumbo
Lets see, whether you can say this Jumbo, What are annotations?

Annotations are syntactic form of metadata. Annotations are added to the Java Source Code. Annotations are not method calls

17

Dumbo Vs. Jumbo
Command Pattern is basis of what framework?

Command Pattern is the basic building of Struts Framework

18

Mr. Thinker
What is the problem that Command pattern attempts to solve? Decoupling the sender of a command from the receiver of the command. The sender does not know anything specific about the intricacies of the command handled by the receiver

19

Ms. Natasha
Enough guys you have taken so much of time that I feel guilty of involving you in the first place. Let them learn more. I will call you back after some time. Now, I leave the dais to Mr. Trainer to start the session

20

Enter the World of Struts

21

22

Enter the World of Struts

23

24

History of Struts
 Struts was launched by Craig Mc Clanahan in May 2000  Version 1.0 officially launched in July 2001  Struts 1.x is still evolving. Struts 1.3. 10 was released on Dec 2008  In order to get away with the disadvantages / limitations of Struts 1.x, Struts

2.0 was developed

25

Birth of Struts 2
 Just before Struts 2 was built, there was a movement to achieve a common

web framework for Java

 Struts Framework’s community and Webwork framework’s community

started working together and achieved what is now called Struts 2

 Web work was merged with Struts  Web Work provides the base technology for Struts 2  Web Work in turn is a wrapper of a framework called XWork

WEB WORK XWORK

+

Struts

Struts 2

26

About XWork and Web Work
 Context independent and provides features such as – XWork is a generic command framework

 Actions  Validations  Interceptors  Dependency Injection mechanism – Webwork is a wrapper around XWork – Context dependent specific to Web Applications

27

About Struts 2
 Struts 2 was built from scratch with Web Work as the base technology  Current Release – Version 2.1.6 on Jan 2009

28

Dumbo Vs. Jumbo
Oh! I see. That means Struts team is a Copy Cat. They have just copied everything from a framework called Web Work. So, they accept their defeat. They accept that their framework was inferior

Why are you so pessimistic and sarcastic? Struts was so popular and had a huge developer base. So, it was not difficult to find out the limitations of the framework. As soon as the limitations were found, it was so easy to come up with a solution. It was a Win-Win for both Struts and Web Work, since Web Work was not very popular but had a robust technology

29

Software Pre-requisites
 Struts 2 was built using Java 5.0  Hence JDK/JRE 1.5 is required  A webserver / servlet container that supports java 1.5 is required. (Eg.

Tomcat 5.5 or 6.0)

30

What is already installed in your systems?
 MySQL Database 5.1  Tomcat 6.0  Java 1.5  Net Beans IDE  Struts 2 Libraries

31

Tomcat Setup
 Open TomcatRoot\conf\tomcat-users.xml  Add the following entry before </tomcat-users>  <user username="admin" password="admin" roles="manager,admin"/>  Save and Close

32

Net Beans Setup
 Click on File  New Project  Choose Java Web  Click Next  Enter the Project Details: – Project Name = StrutsTraining – Project Location, Project Folder = Select an appropriate location  Click Next

33

Net Beans Setup (Contd..)
 In the Server and Settings Dialogue, click on Add (Server) – In Choose Server dialog, select Tomcat 6.0 – Click Next – Select Catalina Home as the root of the tomcat – Enter user name and password as admin – Click Finish – Click Next – Click Finish (Do not select Struts 1.2.9)  Now a new project would be created

34

Starting Tomcat
 Click on Services Tab  Expand Servers link  Right Click on Tomcat 6.0  Click on Start  In few seconds you should receive a message in the log (at the bottom of

the IDE) as “INFO: Server startup in nnn ms”

 To check if the server is up, open a browser and enter http://localhost:8080  You should receive an index page from Tomcat

35

Struts Setup – Adding the Libraries
 Explode the “struts-2.0.14-all.zip” file to an appropriate location  Right click on Libraries  Click on Add Jar/Folder  Choose the lib folder of the exploded Struts directory  Keep adding the following jar files – Commons-logging-1.0.4.jar – Freemarker-2.3.8.jar – Ognl-2.6.11.jar – Struts-core-2.0.14.jar – Xwork-2.0.7.jar

36

Struts Setup – Web.xml changes
 Add the following in web.xml of the new project that you created  This should be added under the web-app element
<filter> <filter-name>struts2</filter-name> <filterclass>org.apache.struts2.dispatcher.FilterDispatcher< /filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>

37

Struts Setup – Web.xml changes (Contd..)
 Save the web.xml  Right click on the project and click “Clean and Build”  After successful build, click on Run  This should open a new browser and display “Hello World!”  If so, your Struts Setup is successful

38

Saying Hello to Struts2 – The Hello World Project

39

Dumbo Vs. Jumbo
Oh! Cool! I do not even know ABCD of struts but I am going to do a project. That’s great. But how!!

Don’t have high hopes. This project is basically Struts from 20000 feet. Probably the easiest thing in Struts 2. He might have taken this route just not to dash the confidence of you Dumbo . Jokes apart, I think it would be a great idea to kick start our Struts mission with a simple Hello World Program in Struts 2 and that is exactly what this silly project is all about. Let us move on.
40

What is the functionality of this project?
 First Page will display a text box where you can enter your name and submit

using a submit button previous page>

 The second page will then display “Hello!! <the name you entered in the

41

Dumbo Vs. Jumbo
Wow! What a great project! I should be privileged to take part in the development

Do not be sarcastic. You will truly appreciate the importance of this project a while from now. Let us all start the development. All of us will manually type whatever is mentioned in the following PPTs by following the instructions. The idea is simple. To give you a feel of Struts 2.

42

Step 1 - The Hello World Page
 Right click on Web Pages and click New  JSP  Enter the File Name as “HelloWorld”  Click Finish  Type the following in the HelloWorld.jsp

43

Step 1 - The Hello World Page
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>Hello World!!</title> </head> <body> <s:form action="helloWorld" method="post"> <H3>Hello!! Enter your name:</H3> <s:textfield name="myName" label="My Name is" size="30"/> <s:submit label="Submit" name="submit"/> </s:form> </body> </html>

44

Step 2 – The Struts XML File
 Expand Source Packages  Right click on default package  Click New – Other  Choose File type as XML  Select File Type on the right as XML Document  Enter the file name as struts  Click Finish  Type the following in struts.xml

45

Step 2 – The Struts XML File
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="training" extends="struts-default"> <action name="helloWorld" class="com.aigss.training.struts.action.HelloWorldAction"> <result name="success">/HelloWorldSuccess.jsp</result> </action> </package> </struts>

46

Step 3 - The Hello World Action
 Right click on Source Packages  Click on New – Java Package  Enter Package Name as “com.aigss.training.struts.action”  Right click on the just created package and click on New – Java Class  Enter the class name as “HelloWorldAction”  Type the following in HelloWorldAction.java

47

Step 3 - The Hello World Action
package com.aigss.training.struts.action; public class HelloWorldAction { private String myName; public String execute() { return "success"; } public String getMyName() { return myName; } public void setMyName(String myName) { this.myName = myName; } }

48

Step 4 – The Result Page
 Right click on Web Pages and click New  JSP  Enter the File Name as “HelloWorldSuccess”  Click Finish  Type the following in the HelloWorldSuccess.jsp

49

Step 4 – The Result Page
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>Hello World!!</title> </head> <body> <H2>Hello!! <s:property value="%{myName}"/></H2> </body> </html>

50

Step 5 – Running the Hello World Project
 Right click on the project (StrutsTraining)  Click on Run  This will open up a new browser and display the index page of the project  Now change the URL as

http://localhost:8080/StrutsTraining/HelloWorld.jsp

 Enter your name and submit  You should get a new page with the contents “Hello!! <Your Name>”

51

Step 6 – Time to celebrate

52

Life without Struts2
• How life would have been without Struts. Let us rewind to the end of 20th

century

 HelloWorld.jsp – Manually code all the HTML Tags including labels for text boxes – Make the action of the form to point to a Servlet – On click of the submit button, submit the form which will take the

request to the servlet

 HelloWorld Servlet – Receive the name entered in the Hello World page and store it in a string

variable – String myName = request.getParameter(“myName”); – Store the string in the session with a key – Forward or redirect the request to the Result JSP

53

Life without Struts2
 Hello World Success JSP – Get the “myName” attribute from the session using <% %> – Display the name in the JSP page

54

Dumbo Vs. Jumbo
Sorry Jumbo if I am missing something. I am a beginner in Struts 2. However going through the above slide, I do not feel that Life without Struts 2 is equally easier. What does Struts 2 provide me? How does it make my life easier?

I know many of us would feel the same. And there is nothing wrong in having this thought. It would be the responsibility of the trainer to justify himself.

55

Life with Struts
 Comparing the above with Struts enabled code we see the following

differences – No request.getParameter(“”) – No forward or redirects anywhere – JSPs do not have bean definition, session access etc

56

Dumbo Vs. Jumbo
I agree that there are few positives like the ones mentioned above. But still I am not excited about it

I cannot help the trainer here. Let use see what he has to say.

57

Ph.D. in Mathematics
 Let us now focus on a little bit more complex problem  Our objective is to achieve Ph.D in mathematics  Let us see how Struts helps towards that cause  The thesis given for Ph.D is adding two numbers and displaying the sum. • The screens to be developed are as shown below:

58

Ph.D. in Mathematics

SCREEN 1

59

Ph.D. in Mathematics

SCREEN 2

60

Ph.D. in Mathematics – Solution
 Now, you should be clear on what we are expecting. So, what are you waiting

for??

 No, this time, you will do the entire coding yourself. It is not difficult at all  Hints: – The approach and the steps are the same as that of Hello World

Program – So, just copy paste the contents from that program and modify accordingly – At the end you would have done the following:  Create 2 JSP files one for input and the other for results  Add a new entry in Struts.xml  Create a new action class
 Happy Coding!!

61

Dumbo Vs. Jumbo
This guy clearly knows how to kill time man. We are just on Day 1 of Struts session but he has already given us an exercise. I do not like this.

I know what you mean! But I feel it is a no-brainer. That’s why you find it tough 

62

Ph.D. in Mathematics – Solution
 I hope all of you have completed this exercise successfully. It is time to

celebrate again

63

Ph.D. in Mathematics – Solution

64

Ph.D – Life Without Struts2
 Let us now look back again and check how we would have done this without

Struts 2 with traditional servlet/jsp based programming – The servlet would have become more complex because I need to get the numbers as String from request object and then convert them back to numbers (float)

HTTP considers everything as text but servlet helps to overcome this by converting them to corresponding types. But please note, it is not Automatic. The type conversion in servlet needs to be done manually .

65

Ph.D – The Magic of Struts 2 – No Explicit Type Conversion
 Did you notice this?  We just declared the number variables as floats in the action class  We never converted the screen parameters from string to float. It happened

automatically!!

66

Consolidated Struts 2 positives – Till Now
 No request.getParameters anywhere in the application  No type conversions anywhere  No forward or redirects in the code  No need to explicitly access session or request or page (i.e. various scopes)

in many scenarios

67

Dumbo Vs. Jumbo
OK Man. I give it to him. I now slowly realize that there is a little bit of advantage in using Struts 2. But are these the only reasons why I should spend so much of time learning a new framework and then start using it? I am reluctant

Your point is well taken. I hope there are more such positives with using Struts. Let us wait and watch

68

Dumbo Vs. Jumbo
That’s ok. I have one more very valid concern. We did two projects today one of them on our own. But that is like a blind-folded journey. I did not understand the background. This ain’t fair on his part

Yeah! Me too. I did not expect this from the trainer

69

 History of Struts

Dumbo, Your point is very valid. Tomorrow you will learn more about the internals of the above two projects and also we will be delving more in depth

 Birth and ancestors of

Struts2

 Tomcat/NetBeans/Struts/Pr

oject Setup Projects

 The Hello World and Ph.D

But now, let us do a quick recap on what we did today.

70

Session 3
You guys have a very good night and we will meet again in the next session with lots of fun 

71

Sign up to vote on this title
UsefulNot useful