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.2.1.1
running on Apache Tomcat
6.0.32 using
Eclipse Helios ( Helios Service Release 2 )
(as of 03/20/2011)
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: 03/20/2011,
OS: Win 7,
Eclipse Helios: Helios Service Release 2
JDK: jdk1.6.0_21
Web Server: Apache Tomcat
6.0.29
Framework: Struts .2.2.1.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:
- If the struts.devMode is true in "struts.xml" then you do not need
to reload the Web App to test.
- If the struts.devMode is false in "struts.xml" then do the
following:
- JSP & HTML changes - reload not needed.
- XML, Java, property file changes - you must reload the webapp for the
changes to take place.
- 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.2.1.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 - Create directory, then copy files
- WARNING: Do not copy the directory by right clicking on
the directory and choosing copy.
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.
- In Win Explorer go to the Tomcat install directory and locate
the "webapps" folder (see Reference info above).
- Under the "webapps" folder create the directory called "mystruts-helloworld"
- Now copy the contents (files and subfolders) of the directory "struts2-blank-2.2.1.1" to a
directory
called "mystruts-helloworld".
- 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.2.1.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"
- 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\ ....".
- JRE System Library
- Check to make sure that the "JRE System
Library" is listed.
If not, click "Add Library", "JRE System
Library", "Next", "Finish".
- Click on the tab "Order and Export". Move the
directory to be directly under
the WEB-INF/src directory.
Note:
You can't compile .java files without this library!!!
- 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
Congratulations, You have completed your Struts Webapp!
Courseware developer notes:
- Add username from Login action on Menu.jsp page
- Add content for default web page for bad actions in subdirectories. Ex:
http://localhost:8080/mystruts2-helloworld/mystrutshelloworld/badaction