Struts 2 & Eclipse - First Steps
MyStrutsHelloWorld
(Free Web Tutorial)
by Michael Thomas
Struts Home Page
This web page will help you get started with Struts framework
running on Tomcat Web Application Server using the Eclipse IDE.
This tutorial covers: Struts 2 - 2.1.8.1
running on Apache Tomcat
6.0.29 using
Eclipse Helios ( IDE for Java EE Developers )
(1.3.0) (as of 08/12/2010)
Prerequisites
- Struts 2 - Web App container that supports:
Servlet API 2.4 - (ie: Tomcat 5.x or higher)
JSP API 2.0 - (ie: Tomcat 5.x or higher)
Java 5 - (ie: Java 5 or higher)
- If you need help with the pre-requests go
to the home page of "Free Web Tutorials on Java & Web Technologies".
- This tutorials has been tested in the following environment:
Date: 08/12/2010,
OS: Win 7,
Eclipse Helios: 1.3.0
JDK: jdk1.6.0_21
Web Server: Apache Tomcat
6.0.29
Framework: Struts .2.1.8.1
(Older Tutorial -
Date: 05/25/08, OS: Win XPro, JDK: jdk1.6.0_03; Web
Server: Tomcat 5.5; Framework: Struts .1.2.8; Eclipse Classic:
3.1.1)
Objectives
- Use Eclipse to create a Struts "Hello World" web application that runs
on Tomcat from scratch.
Reference info - Refer to this
section as needed. Now skip to the next section.
- Tomcat Tutorial - if you need more information on Tomcat see our
tutorial.
- Stop/Start the "Apache Tomcat" service - (Start, Control Panel,
Administrative Tools, Services). You can stop and/or start the service
"Apache Tomcat" here.
- Reload a Web App (using Tomcat Manager)
- Goto: http://localhost:8080/
- Click on "Tomcat Manager"
- User: admin
Pswd: <enter your password>
- Click "Reload" (Or you can click Stop, then Start)
- Note: You will need to do a "Reload" when ever you do the
following:
- Change a property file.
- Change a Java source/class file.
- Change an XML file used for configuration: web.xml, struts-config.xml,
etc...
- Note: You DO NOT need to "Reload" when you do the following:
- Change a JSP file.
- Change a HTML file.
- Additional Notes
- Caching problems - Tomcat does keep a working directory copy
of the webapps. If you reuse directory names, copy files with
older dates into a directory, or are having weird cache issues delete
the working directory for the web app. Make sure you stop the
"Apache Tomcat" service first. You may want to try Stop/Starting
the web app first.
C:\<tomcat directory>\work\Catalina\localhost\
First Steps - Struts & Eclipse (IDE)
View the Final "Hello World" Web App
- Download the final solution
-
mystruts2-helloworld-final.zip - right click and save to your local.
Next extract the zip file and then place
the .war file in the webapps directory of your Tomcat install.
- Run the Web App - Learn the Business Rules for the Web App.
- Run the web app so that you know what the business rules will be for the
web app you will create from scratch.
- Launch the web app:
http://localhost:8080/mystruts2-helloworld-final
- Click on the "Login" link.
- Enter the following:
User: test
Password: password
- You should see the Main Menu.
- Go back to the logon screen and enter a bad user name and/or password to
see the "Validation Error" screen.
- Test leaving the username and/or password empty.
- Click on the "Register" link and you'll see that feature is not
completed.
- Click on the 2 Help links.
- Click on the "Espanol" and test the above items. Click on
"English" and see the change.
Setup Eclipse & the Struts Webapp
- struts2-blank Web App - Test the web app.
- After completing the Struts installation you should have the file
"struts2-blank.war"
in your Tomcat webapps directory.
If not, review the Tutorial. Launch from the Struts Tutorial home
page.
To create our new "Hello World" web app we will start from the
struts2-blank web app by making a copy and then making changes.
- Launch the application with the following URL:
http://localhost:8080/struts2-blank-2.1.8.1
- You should see a welcome page.
- Get familiar with the struts2-blank web app. We will be
modifying this web app.
- Click "Espanol" and the title will display in Spanish.
- Click "English" and the title will display in English.
- Create a blank Struts Web Application called: mystruts2-helloworld
Choose one of the following options A or B.
- Option A - copying a directory
- Stop the "Apache Tomcat" service (see Reference
Info).
**Note:
If the "Apache Tomcat" service is running and you make a copy of a
directory you will not be able to rename the directory because
Tomcat will immediately take use of the directory.
If you want to leave the "Apache Tomcat" service running, in the
steps below you could create an empty directory called "mystruts-helloworld"
and then copy the contents of the struts blank directory to the new
directory.
- In Win Explorer go to the Tomcat install directory and locate
the "webapps" folder (see Reference info above).
- Make a copy of the directory "struts2-blank-2.1.8.1" to a
directory
called "mystruts-helloworld". (If the Tomcat
service is still running read the note above -
**Note)
- Start the "Apache Tomcat" service.
- Option B - using a .war file.
- In Win Explorer go to the Tomcat install directory and locate
the "webapps" folder (see Reference info above).
- Make a copy of the file "struts2-blank-2.1.8.1.war" to a
temporary directory (ie: c:\temp
etc...) and rename the file to: "mystruts-helloworld.war".
You can find the .war file in the Tomcat install directory (see
Reference info above).
- Copy the file back to the Tomcat install directory.
Note: Tomcat will automatically create the web application
(directories & files) from the .war file.
- At your convenience you can remove the .war file by following
the following steps:
- Stop the "Apache Tomcat" service (see
Reference Info).
- Delete the .war file
- Start the "Apache Tomcat" service.
- Note: In may past I've noted that If you don't stop the service first Tomcat may remove the directory also (weird in my
opinion).
- mystruts2-helloworld Web App - Test the web app.
- Make sure the web app works. Goto:
http://localhost:8080/mystruts2-helloworld
- You should see a Welcome page that was identical to the web app
"struts2-blank". Test the web app.
Screen Ex: Struts is up and running ...
- Eclipse - Create a Java Project
- Launch Eclipse
- File, New, Project, Java Project
- Project Name: mystruts2-helloworld
- UnCheck - Use default location.
- Click "Browse" and locate the directory in the Tomcat WebApp
directory.
Click "OK"
Ex Directory:
C:\Tomcat\Tomcat6.0\webapps\mystruts2-helloworld
- Click "Next"
- On the "Java Settings" screen check the "Default output folder":
mystruts2-helloworld/WEB-INF/classes
- Click "Finish"
- Eclipse - Build Path - Tomcat lib jars files - add to the
java build path so that you do not get compile errors in Eclipse because of
missing class files that are related to the container that runs the web
apps.
- Right click on the project.
- Click "Build Path", "Configure Build Path", then
click the tab "Libraries"
- Check to make sure that the "JRE System
Library" is listed.
If not, click "Add Library", "JRE System
Library", "Next", "Finish".
Notes:
Under the tab "Order and Export" this lib file should be directly under
the WEB-INF/src directory.
You can't compile .java files without this library!!!
- Click "Add External JARs"
- Browse to the Tomcat common library directory.
Ex: C:\Tomcat\Tomcat6.0\lib
- Select all of the JAR files (Ctrl-A), then click "Open".
- FYI Note:
Remove any of the .JAR files that are listed with an X (missing) because
they are pointing to the defaulted missing directory
"C:\Program Files\ ....".
- Click "OK" to leave the "Java Build Path" screen.
- There should be no errors with the App at this point.
- Eclipse - Struts Library Files - FYI
- Eclipse - Source Folder
- Currently we have a sources folder for .java files but we need to
make sure it was chosen correctly when you created the project.
- In Eclipse, Right click on the mystruts2-helloworld project and
choose "Properties".
- Click on the tab "Source"
- Make sure it says: mystruts2-helloworld/WEB-INF/src/java
(Note: If not, click on the source build path and choose "Edit" to
correct.)
Code Changes via Eclipse (IDE) & Code Walk-through
- For the purpose of learning we will strip down the source code
of the web app to the bare bones.
- Through out this tutorial we will constantly view the web app to see the
changes we make.
- Warning: During testing remember the
items below. If you see "Reload the Web App and test" and you only changed a
JSP page then a reload is not necessary.
- JSP changes - no reload or stop/start of the webapp is necessary.
- XML, Java, property file changes - you must reload the webapp for the
changes to take place.
- Delete files copied over from the "struts2-blank" web app
Delete directory: /mystruts2-helloworld/example
Delete directory: /mystruts2-helloworld/WEB-INF/src/java/example
Delete file: /mystruts2-helloworld/WEB-INF/src/java/example.xml
Delete file: /mystruts2-helloworld/WEB-INF/src/java/struts.xml
- Create the file: test.html (View the
final version)
- Let's create a simple test.html file so that we can see simple
HTML served out.
- Right click on the web app then choose: new, file, test.html,
finish
(File Location: /mystruts2-helloworld/test.html)
- Click the "View" link above and place the contents into this file.
- Test the file you created:
http://localhost:8080/mystruts2-helloworld/test.html
- Edit WEB-INF\web.xml - (View the
final version)
- Edit the WEB-INF\web.xml
- Click the "View" link above and place the contents into this file.
- Make a note of the following changes:
- Modify the display-name tag: - This display name shows up in the
"Tomcat Web Application Manager".
<display-name>My Struts Hello World Example</display-name>
- Modify the welcome-file tag - this tag tells struts which file
should be the first file served out as the welcome page. For
our application we will have "index.html" be the first file served
out.
This may not have changed.
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
- FYI - Here is the sequence Struts2 will take if there is no
welcome-file tag info in the web.xml file until it finds where
to go next.
1. web.xml - <welcome-file> tag
2. index.html
3. index.jsp
4. ??? struts.xml - maybe an action in the package that extends
the "struts-default":
ex: extends="struts-default"
- How it works:
- When the web app loads the container reads the "web.xml" file
(Web Application Deployment Descriptor) .
- All requests are filtered through the class
"org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter".
Notice the "filter" tag below which is in the web.xml file.
This tag needs to be in the web.xml file for Strut2 to be enabled.
<filter>/
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
- Action File Extensions - filter-mapping -
This "filter-mapping" tag "/*" will all Struts2 to have a chance to
process all requests so no requests are excluded.
(FYI: The default extension for actions in Struts2 is ".action". )
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
- INFO ONLY: Action File Extensions - the default extension
for Struts2 is ".action". If you want to change the default
file extension for actions to ".do" for compatibility with Struts1
or for any other reason here is an example of how to do that:
<filter-mapping>
<filter-name>Struts2</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
Note: It is my experience that "/*" will also process a ".do"
request. It was common in Struts1 web apps to have this entry
so you may want it for compatibility if converting from Struts1 to
Struts2.
- To play with this concept change the "index.html" to "test.html"
in the <welcome-file> tag and
test your web app. Then change it back.
Reload the Web App and test:
http://localhost:8080/mystruts2-helloworld
(Note: Make sure you change the code back to "index.html".
- Modify the file: index.html - (View the
final version)
- Edit the file: /mystruts2-helloworld/index.html
- Click the "View" link above and place the contents into this file.
- Note the key code below: mystrutshelloworld/Welcome.action
<head>
<META
HTTP-EQUIV="Refresh"
CONTENT="0;URL=mystrutshelloworld/Welcome.action">
</head>
- How it works:
- The meta tag redirects to "mystrutshelloworld/Welcome.action".
(Note: We have not created this directory or file yet.)
- In Struts2 the default extension for an action is ".action".
- We will discuss how it works after we create the struts.xml (action
mapping file).
- OPTIONAL: Create the JSP file: index.jsp - (View the
final version)
- This step is optional. This JSP page does the same thing as the "index.html" page above.
I've included this just incase you would rather use a JSP page instead
of a HTML page.
- Right click on the web app then choose: new, file, index.jsp
- Click the "View" link above and place the contents into this file.
- Create the directory: mystrutshelloworld - we will place our JSP
pages here.
- Right click on web app then choose: new, folder,
mystrutshelloworld
Click finish
(Note: Properties = /mystruts2-helloworld/mystrutshelloworld)
- Create the JSP file: Welcome.jsp - (View version
1)
- Right click on the directory "mystrutshelloworld" then choose:
new, file, Welcome.jsp
(Note: Properties = /mystruts2-helloworld/mystrutshelloworld/Welcome.jsp)
- Click the "View" link above and place the contents into this file.
- How it works:
- Well, it can't work yet because we have not created any mapping info for
the action: mystrutshelloworld/Welcome.action
- However, let's test anyway to see the error.
- Reload the Web App and test:
http://localhost:8080/mystruts2-helloworld
- Error:
HTTP Status 404 - There is no Action mapped for action name Welcome.
--------------------------------------------------------------------------------
type Status report
message There is no Action mapped for action name Welcome.
description The requested resource (There is no Action mapped for action
name Welcome.) is not available.
--------------------------------------------------------------------------------
Apache Tomcat/6.0.29
- Create the XML file: struts.xml - (View version
1)
- Create the struts.xml file:
- Right click on the directory "WEB-INF/src/java" then choose:
new, file, struts.xml
- Click the "View" link above and place the contents into this file.
- How it works:
- When the web app starts up the struts.xml file is read to establish the
Action mapping information.
- The include command pulls in all the Action mapping information from the
file: mystrutshelloworld.xml
<include file="mystrutshelloworld.xml"/>
(Note: Next we will create that file!)
- Create the XML file: mystrutshelloworld.xml - (View version
1)
- Since
we are planning on creating a package called "com.michaelthomas.mystrutshelloworld"
for Java, validation xml & property files let's create a Struts action mapping XML file to
place our action mappings in called "mystrutshelloworld.xml". We can then
include that file into the struts.xml file that we will create.
- Create the file mystrutshelloworld.xml in the same directory as
the struts.xml file.
- Right click on the directory "WEB-INF/src/java" then choose:
new, file, struts.xml
(Note: I like to edit XML files using the Eclipse Text editor instead of
the default XML editor. You may want to open the struts.xml file with
the text editor. To copy and paste from the link above.)
(File location: mystrutshelloworld.xml)
- Click the "View" link above and place the contents into this file
- How it works:
- Review the code:
<action name="*">
<result>/mystrutshelloworld/{1}.jsp</result>
</action>
-
If an action has not been evaluated by the time it hits this
tag, the wildcard "*" allows all actions to enter this tag and the result
will be passed to: /mystrutshelloworld/{1}.jsp
-
Since we are looking for "Welcome.action" the {1} equals
"Welcome" so Struts passes the results to: /mystrutshelloworld/Welcome.jsp
- Reload the Web App and test:
http://localhost:8080/mystruts2-helloworld
- You should see:
Welcome to "My Struts Hello World"
- How it Works
- In the Struts framework a JSP page is not loaded directly. The
Struts framework looks in the Action Mappings file (struts.xml) to find what
Java class (action) to fire off and then based on the results passed back
the Action Mappings file usually renders the results to generally to a JSP
page (could be a PDF, Excel, etc...).
- With this design we now have separated the workflow into 3 well-known
concerns: View, Model, Controller
View - JSP pages
Model - Java classes, xml validation, property files etc...
Controller - action mappings file (struts.xml and any included files).
- Setting a Default Page for the Default Package - struts.xml - (View
final version)
- This section is optional unless you desire the feature it provides.
- Let's make the Welcome.jsp page be the default page that is loaded if an
action mapping can't be found from the default package (<webapp>/<something>.action).
- See the error for the following link:
http://localhost:8080/mystruts2-helloworld/badaction
type Status report
message /mystruts2-helloworld-final/mystrutshelloworld/badaction.jsp
description The requested resource (/mystruts2-helloworld-final/mystrutshelloworld/badaction.jsp)
is not available.
- Let's add some code so that if an action can't be found then the
Welcome.jsp page will display.
Key Code:
<default-action-ref name="index" />
- The "index" action is then mapped:
Key Code:
<action name="index">
<result type="redirectAction">
<param name="actionName">Welcome</param>
<param name="namespace">/mystrutshelloworld</param>
</result>
</action>
- Click the "View" link above and place the contents into this file.
- Reload the Web App and test:
http://localhost:8080/mystruts2-helloworld/badaction
Note: Should display the Welcome message now!
- How it Works
- Welcome.jsp - How it Works!
- When the web app is loaded the web.xml file points to the index.html
file as the welcome file.
- The index.html file redirects to: mystrutshelloworld/Welcome.action
- Struts sees the 2 directives at the top of the file that indicate the
file is a JSP page and can use struts tags.
- The Struts frame work sees the ".action" extension and knows to treat
this file as a Struts action.
- (Key information - read carefully)
Struts looks at the struts.xml file to see if there is a mapping for the
action.
The struts.xml file also includes the mystrutshelloworld.xml file.
In the mystrutshelloworld.xml there is no direct mapping but there is an action name="*" tag that will
work.
There is no mention of a class property so Struts does not look for a
class file to process.
Next struts looks at <result> tag which says to go to:
/mystrutshelloworld/{1}.jsp
This means to evaluate {1} to Welcome and therefore goes to:
/mystrutshelloworld/Welcome.jsp
- The rest of the content is HTML at this point.
- Cascading Style Sheets (CSS) -
webappmain.css - (View the
final version)
- We want to use CSS so let's add that to the Welcome.jsp and create the
CSS.
- Right click on web app then choose: new, folder, css
- Right click on the "css" directory and choose: new, file,
webappmain.css
(file location: /mystruts2-helloworld/css/webappmain.css)
- Click the "View" link above and place the contents into this file.
- Add CSS to Welcome.jsp - (View version
2)
- Edit Welcome.jsp
- Click the "View" link above and place the contents into this file.
- Notice the code added.
<link
href="<s:url
value="/css/webappmain.css"/>"
rel="stylesheet"
type="text/css"/>
- Reload the Web App and test:
http://localhost:8080/mystruts2-helloworld
- Create a Package for Java, Property files & XML Validation - com.michaelthomas.mystrutshelloworld - we will
place our Java source & property files here.
- We need to create a Package where we can place our Java Source code,
property and XML validation files.
- In Eclipse, Right click on the "WEB-INF/src/java" source folder and
choose "New", "Package".
Source Folder = mystruts2-helloworld/WEB-INF/src/java (Note:
default to this!)
Name = com.michaelthomas.mystrutshelloworld
Click "Finish"
(Note: The following directory structure was just created:
/WEB-INF/src/java/com/michaelthomas/mystrutshelloworld
)
- Multilanguage Support - Property Files - to use for multiple language support
- Let's create a 2 property files used for Multilanguage support: one for
English and one for Spanish.
- package.properties - (View the
final version)
- This is the default language version (English).
- Right click on the package you created "com.michaelthomas.mystrutshelloworld"
and choose: new, file, package.properties
(File Location: WEB-INF/src/java/com/michaelthomas/mystrutshelloworld/package.properties)
- Click the "View" link above and place the contents into this file.
- package_es.properties - (View the
final version)
- This is the Spanish language version in which I just placed the words (es)
before each line so you get the idea.
- Right click on the package you created "com.michaelthomas.mystrutshelloworld"
and choose: new, file, package_es.properties
(File Location: WEB-INF/src/java/com/michaelthomas/mystrutshelloworld/package_es.properties)
- Click the "View" link above and place the contents into this file.
- Add Multilanguage Support to Welcome.jsp - (View version
3)
- Edit Welcome.jsp
- Change the title to:
<title><s:text
name="Welcome.title"/></title>
- Change the Heading to:
<h3><s:text
name="Welcome.heading"/></h3>
- At this time the JSP page doesn't have access to the property file yet.
Let's go ahead and see what our web app displays.
- Reload the Web App and test:
http://localhost:8080/mystruts2-helloworld
- Notice that the value from the Property file was not used. We will
fix that in the next steps.
Currently Displays: Welcome.heading
- Create a WebApp Support Java file - WebappSupport.java - (View the
final version)
- Right click on the package you created "com.michaelthomas.mystrutshelloworld"
and choose: new, class
Name: WebappSupport
Click "Finish".
(File Location: WEB-INF/src/java/com/michaelthomas/mystrutshelloworld/package.properties)
- Click the "View" link above and place the contents into this file.
- We have one more change before explaining how it works.
- To use WebappSupport.java modify mystrutshelloworld.xml - (View version
2)
- Edit the mystrutshelloworld.xml file.
- Click the "View" link above and place the contents into this file.
- Note the addition of the class property to the action tag:
<action name="*" class="com.michaelthomas.mystrutshelloworld.WebappSupport">
- Reload the Web App and test:
http://localhost:8080/mystruts2-helloworld
- Displays:
Welcome to "My Struts Hello World"
- How it works
- The index.html file redirects to: mystrutshelloworld/Welcome.action
- The Struts frame work sees the ".action" extension and knows to treat
this file as a Struts action.
- Struts looks at the struts.xml file and any included xml files (ie:
mystrutshelloworld.xml) to see if there is a mapping for the
action.
There is no direct mapping but there is an wildcard action name="*" tag that will
work.
Because there is a class property Struts loads the class file (WebappSupport) and runs the
"execute()" method which this java file did not override from the class it
inherited "ActionSupport".
Struts also loads the proper property file which is
package.properties
(English version).
Now the JSP will have access to the property file.
Next struts looks at <result> tag which says to go to: /mystrutshelloworld/{1}.jsp
This means to evaluate {1} to Welcome and therefore goes to:
/mystrutshelloworld/Welcome.jsp
The big change at this point is that the Welcome.jsp has access to the
Java file and the property file.
- Modify Welcome.jsp with Multilanguage Choice and Menu Links - (View version
4)
- Edit Welcome.jsp
- View the contents of the final version and place that into your file.
- Reload the Web App and test:
http://localhost:8080/mystruts2-helloworld
- Click on "Espanol" - the value pairs in the property file has (es) in front of every line
to indicate it could be Espanol. In a real
environment someone who knows Espanol will but the proper translation.
- Click on "English" - we are now using the other property file.
- How it works - Multilanguage Support
- Struts created the url with parm request_locale=es:
http://localhost:8080/mystruts2-helloworld/Welcome.action?request_locale=es
Key Code used to create the link:
<s:url
id="url"
action="Welcome">
<s:param
name="request_locale">es</s:param>
</s:url>
- The struts framework sees the parm "request_locale=es" and will use
values out of the "package_es.properties" when the <s:text> tag is used
until another "request_locale" parm is used.
- To see this concept follow the steps below:
Step 1:
http://localhost:8080/mystruts2-helloworld/Welcome.action?request_locale=es
(Note: Now in Espanol)
Step 2: http://localhost:8080/mystruts2-helloworld/Welcome.action
(Note: Still in Espanol)
Step 3:
http://localhost:8080/mystruts2-helloworld/Welcome.action?request_locale=en
(Note: Now in English)
Step 4: http://localhost:8080/mystruts2-helloworld/Welcome.action
(Note: Still in English)
- Struts looked at the struts.xml (via mystrutshelloworld.xml) file and mapped the Welcome.action to
the java file "WebappSupport.java" which has access to the property files.
The WebappSupport class does not override the execute() method of the
ActionSupport class so the default results is returned.
The struts.xml file then mapped the
default results to /mystrutshelloworld/Welcome.jsp. (<result>/mystrutshelloworld/{1}.jsp</result>)
In the Welcome.jsp the "s:text" tags uses the property file based on the language and then
grabbed the values out based on the value/name pars.
ex:
<s:text
name="Welcome.heading"/>
- Click on the 4 links at the top and you'll see that the links go to
resources that are not available yet.
Next we will create stubs for those links.
- Create Stubs for Links
- Let's create some stub JSP pages that we can later populate with
content. We can use this concept if you have multiple developers
working on a project. Stub out the application and let the UI
people work on the JSP pages etc...
- We will create one JSP that we can include into other JSP pages.
- Create Missing.jsp - (View the
final version)
- Right click on the directory "mystrutshelloworld" then choose:
new, file, Missing.jsp
(Note: Properties = /mystruts2-helloworld/mystrutshelloworld/Missing.jsp)
- Click the "View" link above and place the contents into this file.
- Create Login.jsp - (View the
version 01)
- Right click on the directory "mystrutshelloworld" then choose:
new, file, Login.jsp
(Note: Properties = /mystruts2-helloworld/mystrutshelloworld/Login.jsp)
- Click the "View" link above and place the contents into this file.
- Key code:
<s:include value="Missing.jsp"/>
- Create Register.jsp - (View the
final version)
- Right click on the directory "mystrutshelloworld" then choose:
new, file, Register.jsp
(Note: Properties = /mystruts2-helloworld/mystrutshelloworld/Register.jsp)
- Click the "View" link above and place the contents into this file.
- Key code:
<s:include value="Missing.jsp"/>
- Create Help.jsp - (View version
01)
- Right click on the directory "mystrutshelloworld" then choose:
new, file, Help.jsp
(Note: Properties = /mystruts2-helloworld/mystrutshelloworld/Help.jsp)
- Click the "View" link above and place the contents into this file.
- Key code:
<s:include value="Missing.jsp"/>
- Reload the Web App and test:
http://localhost:8080/mystruts2-helloworld
- Click the "Login" link - notice that the stub info shows up.
- Click the "Register" link - notice that the stub info shows up.
- Click the "Help (JSP)" link - notice that the stub info shows up.
- How it works
- It works just like Welcome.jsp. The only difference is that
the content comes from a file that was included from "Missing.jsp"
- Create the Help Links
- Help.jsp - (View the
final version)
- Modify the Help.jsp file.
- Click the "View" link above and place the contents into this file.
- help.html - (View the
final version)
- Right click on the directory "mystrutshelloworld" then choose:
new, file, help.html
(Note: Properties = /mystruts2-helloworld/mystrutshelloworld/Help.jsp)
- Click the "View" link above and place the contents into this file.
- Reload the Web App and test:
http://localhost:8080/mystruts2-helloworld
- Click "Help (Jsp)" link
- Click "Espanol" then click "Help (Jsp)" link - notice that this
link supports Multilanguage.
- Click "English" then click "Help (Jsp)" link - back
to English.
- Click the "Help (Html)" link - HTML doesn't support Multilanguage.
- Login Screen - Login.jsp - (View the
final version)
- Let's create the Login Screen
- Click the "View" link above and place the contents into this file.
- Reload the Web App and test:
http://localhost:8080/mystruts2-helloworld
- Click the "Login" link.
- The login screen should display with 2 fields: User Name
and Password.
- Leave the 2 fields blank and click "Submit". Notice that
there was no basic form validation.
- How it works
-
Any errors sent back from the Java action (Login.java -
not created yet) will be displayed here.
<s:actionerror/>
-
This code displays a form and also will interact with
the XML validation (not created yet).
<
s:form
action="Login"
validate="true">
<s:textfield
key="username"
size="16"
maxlength="18"/>
<s:password
key="password"
showPassword="true"
size="16"
maxlength="18"/>
<s:submit/>
</s:form>
- Before we finish up the Login process by creating a Java file for
authentication and an xml file for validation let's create thVe Menu screen
that will be displayed if the user enters a valid username & password.
- Create Menu.jsp - (View the
final version)
- Let's create the Menu screen that will display after a successful
login.
- Right click on the directory "mystrutshelloworld" then choose:
new, file, Menu.jsp
(Note: Properties = /mystruts2-helloworld/mystrutshelloworld/Login.jsp)
- Click the "View" link above and place the contents into this file.
- Login Authentication - Login.java (View version
1)
- Let's create a stub for the java
file that Struts will read and use to do login authentication at the action
level (java backend).
- Right click on the package you created "com.michaelthomas.mystrutshelloworld"
and choose: new, class
Name: Login
(File Location: WEB-INF/src/java/com/michaelthomas/mystrutshelloworld/Login.java)
- View the contents of the final version and place that into your file.
package
com.michaelthomas.mystrutshelloworld;
public
class Login
extends
WebappSupport {
public
String execute() throws
Exception {
return
SUCCESS;
}
}
- Reload the Web App and test:
http://localhost:8080/mystruts2-helloworld
- Click the "Login" link, then "Submit"
- Notice that we are still on the Login page. We need to add
to the Action Mapping file to look for the SUCCESS return value and
then go to the Menu.jsp.
- You should go to the Menu page. Later we will add
authentication code to the Java file.
- Add SUCCESS to the Action Mapping - mystrutshelloworld.xml -
(View version
3)
- Edit the mystrutshelloworld.xml file.
- Click the "View" link above and place the contents into this file.
- Note the additions:
<action name="Login" class="com.michaelthomas.mystrutshelloworld.Login">
<result type="redirectAction">Menu</result>
</action>
- We just created a mapping to the action "Login" which will take the
result "SUCCESS" and redirect to the Menu action.
But when you test you'll see that we don't get a chance to enter any
values into the form because we are immediately sent to the menu.
- Reload the Web App and test:
http://localhost:8080/mystruts2-helloworld
- Click the "Login" link
- Ooops we didn't get a chance to see the form because the action was
evaluated as SUCCESS and we were sent to the menu.
- How it works:
- We need to add code to the action mapping that will create 2 modes:
Input & Process.
- In the "Input" mode the form will be displayed so we can enter data or
see validation errors and correct data.
- In the "Process" mode we will validate and then send off to the menu
etc...
- Add INPUT mode to the Action Mapping - mystrutshelloworld.xml -
(View final
version)
- Edit the mystrutshelloworld.xml file.
- Click the "View" link above and place the contents into this file.
- Note the additions:
<action name="Login_*" method="{1}" class="com.michaelthomas.mystrutshelloworld.Login">
<result name="input">/mystrutshelloworld/Login.jsp</result>
<result type="redirectAction">Menu</result>
</action>
- Now we need to have the "Welcome.jsp" page call the Login action with
the 'input" mode.
- Call an Action in Input Mode - Welcome.jsp - (View
final version)
- Edit Welcome.jsp.
- Click the "View" link above and place the contents into this file.
- Notice the following code change:
<s:url action="Login_input"/>
- "_input" was added to the end of the action. The struts framework
sees this and then displays the form vs processing the form.
- Reload the Web App and test:
http://localhost:8080/mystruts2-helloworld
- Click the "Login" link
- Now the login page displays.
- Click the "Submit" link. You should go to the Menu page. Later we will add
authentication code to the Java file.
- How it works:
- We have added just enough code for the UI developer to continue his
work and later the Java developer will add authentication.
- The Login class extends WebappSupport which extends ActionSupport
which is required to create a Struts action.
- WebappSupport may also store variables that we want to use through
out the web app: name, password etc...
- The Welcome.jsp page called the "Login_input" action so that the
struts framework will display the form vs processing the form.
- The Login.jsp displays the form.
- When the user clicks "Submit" the form is passed to the "Login"
action which then processes the form.
- The Login execute() method is executed and currently no validation
takes place so we just return SUCCESS.
- The struts framework looks at the action mapping
(mystrutshelloworld.xml) for SUCCESS and then redirects to the Menu
action.
- There is no Menu.java file so struts looks at the action mapping for
Menu and then redirects to: mystrutshelloworld/Menu.jsp.
- Login Validation - Login-validation.xml - (View the
final version)
- Let's create an XML file that Struts will read and use to do basic
form validation at the action level (java backend).
- Right click on the package you created "com.michaelthomas.mystrutshelloworld"
and choose: new, file, Login-validation.xml
(File Location: WEB-INF/src/java/com/michaelthomas/mystrutshelloworld/package.properties)
- View the contents of the final version and place that into your file.
- Reload the Web App and test:
http://localhost:8080/mystruts2-helloworld
- Click the "Login" link, then "Submit"
- See the 2 validation errors:
User Name is
required.
Password is required.
- If you want, play with language "Espanol"
and do the same thing.
- How it works:
- Since the Struts frame work noticed a validation xml file tied to
the action it validated form and because the username and/or password was
empty the Struts framework returned a result of "input" which
redisplayed the form and added the validation errors. Struts
did all of that for you!
- Struts has a validation template it uses and will display the
errors on the line directly above the line that caused the error.
(Note: You can change the behavior of the validation template.)
- Add Authentication to Login.java - (View the
final version)
- Let's now add some authentication to the Login.java file.
Normally you would hit a database and authenticate the user & password.
For this training example we will just validate inside a method.
- Edit the Login.java file.
- View the contents of the file and place that into your file.
- Reload the Web App and test:
http://localhost:8080/mystruts2-helloworld
- Click the "Login" link.
- Click "Submit". Notice the XML validation errors.
- Type "Help" in the User Name and click "Submit". Notice
there is still an XML validation error.
- Type "Help" in the User Name and "Me" in the Password and click
Submit.
Notice that there is no more XML validation errors but there is some
errors that came from the Login class.
- Type "test" in the User Name and "Me" in the Password and click
Submit.
Notice that the Login class identified that the user name was
invalid. Again, no XML validation errors.
- To see the Menu screen enter the following:
User Name: test
Password: password
- How it works:
- The struts framework calls the "Login" action without the "_input"
appended to the framework ran the execute() method.
- In the execute() method you return "Input" if there is a problem so that
the input screen is redisplayed with the errors.
Ex: if (isInvalidUserPassword(getUsername(),
getPassword())) return
INPUT;
- When an error is detected you add to the "Action Error" object and/or to
the "Field Error" object.
"Action Error" - this.addActionError(getText("error_action.login.invalid_user_password"));
"Field Error" - this.addFieldError("username",
getText("error_field.username.invalid"));
- In the Login.jsp page the "Action Error(s)" are diplayed where you see
the tag: <s:actionerror/>
- The "Field Error(s)" are displayed where the validation template
displays them just like when you use XML validation.
Congratulations, You have completed your Struts Webapp!