Struts - Day3
Struts - Day3
Struts 2.0
Session - 3
May, 2009
Presentation By
Training & Development
Introduction
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
5
Mr. Thinker
6
Mr. Thinker
7
Dumbo Vs. Jumbo
Jumbo! Does JSPs completely replace Servlets?
8
Mr. Thinker
9
Mr. Thinker
10
Mr. Thinker
View Layer is generally
What is the View layer implemented using JSPs
generally made of?
11
Dumbo Vs. Jumbo
What puzzle does MVC solve?
12
Dumbo Vs. Jumbo
Jumbo! Tell something about Struts?
13
Mr. Thinker
14
Mr. Thinker
Yes Filters can act on pre-
Can filters act on both request as well as post-
request and response? response.
15
Mr. Thinker
16
Dumbo Vs. Jumbo
Lets see, whether you can say this Jumbo, What
are annotations?
17
Dumbo Vs. Jumbo
Command Pattern is basis of what framework?
18
Mr. Thinker
19
Ms. Natasha
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
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
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
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
29
Software Pre-requisites
Struts 2 was built using Java 5.0
30
What is already installed in your systems?
MySQL Database 5.1
Tomcat 6.0
Java 1.5
Struts 2 Libraries
31
Tomcat Setup
Open TomcatRoot\conf\tomcat-users.xml
32
Net Beans Setup
Click on File New Project
Click Next
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)
34
Starting Tomcat
Click on Services Tab
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
35
Struts Setup – Adding the Libraries
Explode the “struts-2.0.14-all.zip” file to an appropriate location
36
Struts Setup – Web.xml changes
Add the following in web.xml of the new project that you created
<filter>
<filter-name>struts2</filter-name>
<filter-
class>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
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
The second page will then display “Hello!! <the name you entered in the
previous page>
41
Dumbo Vs. Jumbo
Wow! What a great project! I should be
privileged to take part in the development
42
Step 1 - The Hello World Page
Right click on Web Pages and click New JSP
Click Finish
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
Click Finish
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
Right click on the just created package and click on New – Java Class
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
Click Finish
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
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
The thesis given for Ph.D is adding two numbers and displaying the sum.
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.
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)
65
Ph.D – The Magic of Struts 2 – No Explicit Type
Conversion
Did you notice this?
66
Consolidated Struts 2 positives – Till Now
No request.getParameters anywhere in the application
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
69
History of Struts
70
Session 3
71