Struts 2 & Eclipse - Example Webapps
(Free Web Tutorial)
by Michael Thomas
Struts Home Page
In this tutorial you will setup Eclipse IDE projects so that you
can edit and compile the Struts Example webapps that you optionally installed
with Struts.
Note: On the "Struts Home Page", you can download
the whole site (all content, tutorials & examples) !!!
This tutorial covers: Struts 2.0.11.1
running on Apache Tomcat
6.0.16 (as of 05/25/08)
Prerequisites
- Web App container that supports Struts 2.0.11.1:
Servlet API 2.4 - (ie: Tomcat 5.x or higher)
JSP API 2.0 - (ie: Tomcat 5.x or higher)
Java 5
- Note: On the home page of "Free Web Tutorials on Java & Web
Technologies" we have a tutorial to install Java JDK & Tomcat.
- Know how to Stop & Start the "Apache Tomcat" service.
- This tutorials has been tested in the following environment:
Date: 05/25/08
OS: Win XPro
JDK: jdk1.6.0_03
Objectives
- Setup an Eclipse project for the Struts Example webapps that come with
Struts so that you can edit & compile them.
Table of Contents (Struts Webapps)
I would setup an Eclipse project for each of these webapps so you can edit
& compile changes to files. The option is yours.
- struts2-blank-2.1.8.1 - good webapp to copy from if you are
creating a new Struts Webapp. Example of using English & Spanish language.
- struts2-showcase-2.1.8.1 - see examples in isolation (not a best
practices guide).
- struts2-rest-showcase-2.1.8.1 -
- struts2-mailreader-2.1.8.1 - a best practices Struts webapp
example.
- struts2-portlet-2.1.8.1 - (under
construction) I need to look at this webapp.
Eclipse - Generic Steps for each Example Web App.
- Use these generic steps to create a Java Project for each web app listed
in the "Table of Contents" above.
Below I'll use the "struts2-blank-2.1.8.1" as an example.
- Eclipse - Create a Java Project
- Launch Eclipse
- File, New, Project, Java Project
- Project Name: <pick the appropriate web app name>
ex: struts2-blank-2.1.8.1
- UnCheck - Use default location.
- Click "Browse" and locate the directory in the Tomcat WebApp
directory.
Click "OK"
<pick the appropriate directory name>
ex: C:\Tomcat\Tomcat6.0\webapps\struts2-blank-2.1.8.1
- Click "Next"
- On the "Java Settings" screen check the "Default output folder":
<pick the appropriate web app name>/WEB-INF/classes
ex: struts2-blank-2.1.8.1/WEB-INF/classes
- Click "Finish"
- 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 web app (ex: mystruts2-helloworld) project and
choose "Properties".
- Click on the tab "Source"
- Make sure it says: <web app name>/WEB-INF/src/java
ex:mystruts2-helloworld/WEB-INF/src/java
(Note: If not, click on the source build path and choose "Edit" to
correct.)
- 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.
- (Optional As Needed) Eclipse - Struts Library Files -
(FYI Only)
-
At a minimum you will need the following jar file(s):
struts2-core-2.1.8.1.jar
-
FYI (For Your Information) - If you find you need access
to any additional struts library files other than the ones that were
copied over when you copied the web app "struts2-blank", you can
follow the steps below:
- Here is the concept. Copy the jar files from the Struts
library that has the classes that you need to compile your .java
program to the lib directory of your web app.
- Example:
Copy all (or the ones you need) of the Struts library files from the
Struts install we did earlier in the
Struts - First
Steps.
Copy from: C:\java\apache\stuts2\struts-2.1.8.1\lib
Copy to: C:\Tomcat\Tomcat6.0\lib\webapps\<yourwebappname>\WEB-INF\lib
- Compile Errors
- Check to see if you have a red "X" by the web app.
- In the sections below I have comments about any compile errors you may
encounter for these examples.
- Test your Web App
ex:
http://localhost:8080/struts2-blank-2.1.8.1
struts2-blank-2.1.8.1 - Apache Strut's example web
app.
- Test your Web App
ex:
http://localhost:8080/struts2-blank-2.1.8.1
- Property file change
- Edit the file: /struts2-blank-2.1.8.1/WEB-INF/src/java/example/package.properties
- Make the following change:
HelloWorld.message=
Struts is
up
and
running
...
(Prop Change)
- Java file change
- Edit the file: /struts2-blank-2.1.8.1/WEB-INF/src/java/example/HelloWorld.java
- Make the following change:
setMessage(getText(MESSAGE)
+ "(Java Change)");
- Reload the web app using the "Tomcat Web Application Manager".
(Or you can Stop/Start the web application or Tomcat).
- Test the web app. You should see the change to the "English"
version.
- You're finished.
struts2-mailreader-2.1.8.1 - Apache Strut's example web
app.
- This web app is a best practices Struts 2 example which give you
an example of a login screen, create/edit/view persistent data (User &
Subscriptions) in a XML file. You can create a user account and then
for each user create/edit/delete/view mail subscriptions. If
completed, the application would let users read mail from their accounts.
- Test your Web App
ex:
http://localhost:8080/struts2-mailreader-2.1.8.1
- Tour of the MailReader web app
- Compile Errors:
- ServletContextListener cannot be resolved to a type
File: /struts2-mailreader-2.1.8.1/WEB-INF/src/java/mailreader2/ApplicationListener.java
- Resolution: Add the Tomcat lib files to the Java Build
Path (see above)
- You're finished.
- Application Notes
- Users installed with app:
Username: user
Password: pass
- Database of Users - /struts2-mailreader-2.1.8.1/WEB-INF/database.xml
struts2-showcase-2.1.8.1 - Apache Strut's example web
app.
- See examples in isolation (not a best practices guide).
- Test your Web App
ex:
http://localhost:8080/struts2-showcase-2.1.8.1
- Compile Errors:
- All of the classes are already defined.
- Description of problem:
This is because in the .war file the source (.java) files were placed in 2
directories.
C:\Tomcat\Tomcat6.0\webapps\struts2-showcase-2.1.8.1\WEB-INF\src\java
C:\Tomcat\Tomcat6.0\webapps\struts2-showcase-2.1.8.1\WEB-INF\classes
Why? I'm not sure yet. It could be tied to displaying the source files
within the showcase application.
- Resolution: Remove the "struts2-showcase-2.1.8.1/WEB-INF/classes"
as a source folder.
Right click on the Project and choose "Properties".
Click "Java Build Path"
Click the tab "Source"
Remove "struts2-showcase-2.1.8.1/WEB-INF/classes"
- The import javax.servlet.http.HttpServletRequest cannot be resolved
File: C:\Tomcat\Tomcat6.0\webapps\struts2-showcase-2.1.8.1\WEB-INF\src\java\org\apache\struts2\showcase\xslt\JVMAction.java
- Resolution: Add the Tomcat lib files to the Java Build Path (see
above)
- The project was not built due to "Could not delete
'/struts2-showcase-2.1.8.1/WEB-INF/classes/myTemplateDir'.". Fix the
problem, then try refreshing this project and building it since it may be
inconsistent struts2-showcase-2.1.8.1 Unknown Java Problem
- Resolution: Not sure at this time. At some point it
went away for me.
- You're finished.
struts2-rest-showcase-2.1.8.1 - Apache Strut's example web
app.
- This web app is an example of view/add/edit/delete data from an object
stored in a HashMap.
- Test your Web App
ex:
http://localhost:8080/struts2-rest-showcase-2.1.8.1
- Notes
- Order Data: Order data is placed in the "orders"
object and stored in a HashMap found in the .java file below.
The object is static and is therefore persistent at the class level until
the web app is Stopped or Reloaded. To test try opening up more than one
browser and play with creating and listing orders.
File: WEB-INF/src/java/org/apache/struts2/rest/example/OrdersService.java
- You're finished.
struts2-portlet-2.1.8.1 - Apache Strut's example web
app.
- Test your Web App
ex:
http://localhost:8080/struts2-portlet-2.1.8.1
(Note: This Web App doesn't work for me. I need to figure out why.
Could be designed to run with JBoss web server using Spring technology.)
- Compile Errors:
- Multiple markers at this line
- The import javax.portlet cannot be resolved
- The import javax.portlet cannot be resolved
File: /struts2-portlet-2.1.8.1/WEB-INF/src/java/org/apache/struts2/portlet/example/SavePrefsAction.java
Import statements with issues:
import
javax.portlet.ActionRequest;
import javax.portlet.PortletPreferences;
- Resolution: Download the jar: portlet-api-2.0.jar and
place it in the web apps "WEB-INF\lib" directory.
Then go to "Java Build Path", click on the "Libraries" tab, "Add
External Jar" and then locate the jar file.
- Download Oracles Jar file
- Another Download site -
www.java2s.com
- http://www.java2s.com/Code/Jar/MNOPQR/Downloadportletapi20jar.htm
portlet-api-2.0.jar - download this file!
- You're finished.