|
|
|
|
MyEclipse Struts 2 Tutorial
|
|
Table of Contents
|
|
|
|
|
|
|
Welcome to the MyEclipse Struts 2 tutorial. This tutorial
will walk you through the process of creating a sample
Struts 2 application using MyEclipse Struts 2 tooling. We
will be creating a Struts 2 based Blogging
application in this tutorial.
It is also important to be aware of the scope of this
tutorial. This tutorial explains in detail the usage of the
MyEclipse Struts 2 tooling support and should not be
considered as a Struts 2 tutorial.
The project created in this tutorial is available in the
Resources section for folks that
would like to jump ahead.
|
|
|
|
|
This tutorial is intended for developers who are somewhat
familiar with MyEclipse or Eclipse so you recognize
navigation within the IDE, and understand some of the more
common views like the debugger. Familiarity with Struts
Framework in general is necessary. The concepts covered are
fairly straight forward to Java developers.
To learn more information about the topics presented in
this tutorial please have a look at the links in our
Resources
section. To get a better feel for MyEclipse and learning
more about it, please check out our product
Documentation for more
material.
|
|
|
|
|
This tutorial was created with MyEclipse 9.0. However, if you
notice portions of this tutorial looking different than the
screens you are seeing, please
let us know and we will make sure
to resolve any inconsistencies.
|
|
|
|
|
- Creating a Web project -
In this section we are going to create a new Web project
for our sample
Blog application. To create a web project, open
the File menu->New->Web Project. This will show up the
“New Web Project” wizard. Please enter the values as
shown in the figure and press finish to create a new web
project named “MyBlogStruts2Example” in your workspace.
- Adding Struts 2 capabilities to the Web project -
In this section we are going to add Struts 2 capabilities
to the “MyBlogStruts2Example” project. To add Struts 2
capabilities, use the “Add Struts capabilities” MyEclipse
context menu action as shown in the figure below.
On performing “Add Struts capabilities” from the context
menu, the “Add Struts Capabilities” wizard will show up
as shown in the figure below. This wizard allows you to
select the Struts version for the application. Since we
are creating a Struts 2 based application, we will be
selecting “Struts 2.1” option in the wizard. Please enter
the values as shown in the figure. Pressing finish should
add Struts 2 capabilities to the Web project.
The Structure of created Struts 2 Project is shown in the
figure below. By default
Struts 2 Core Libraries Classpath container will
be added to the project. You can select multiple Struts 2
classpath containers from the add Struts 2 capabilities
wizard explained above.
|
|
|
|
|
In this section we are going to create Struts 2 components
that will be required in our sample blog application. The
flow of the application will start with the index.jsp page
that will contain a link to the login page. The login page
will contain user login and password fields. On entering
valid username and password, the request will be passed to
the Admin page that will contain options to create, review
and delete blogs. An error message will show in the login
page for invalid username or password value.
We will be using MyEclipse Struts 2 Flow Editor to create
all the required Struts 2 components and the JSP/HTML pages
required for rendering the result.
Each section below will contain tips that you can make use
of while creating the application.
- Opening the struts.xml file in MyEclipse Struts 2 editor -
-
The struts.xml configuration file is present in the
“src” folder of our “MyBlogStruts2Example” project.
Double clicking the file will open it in the MyEclipse
Struts 2 Editor.
-
By default, the Graphical editor (Flow tab) will be
active the first time and for all the subsequent
invocations, the last open editor will be active. If the
Flow editor is not active, you can activate it by
selecting the Flow tab as shown in the image below.
-
The Flow editor contains a palette containing a set of
tools that can be used to create the different Struts 2
components.
- Creating Struts 2 Packages -
Struts 2 Package component can be used to group a set of
actions together. In this section we are going to create
a package with name login to group all the actions that
will be used during the login operation. The Package tool
in the palette can be used to create a Struts 2 package
component in the Flow editor. The package creation is a
simple two step process. The following steps demonstrate
the process.
-
Select the Package tool from the palette and drop it in
the empty area inside the Flow editor.
-
On completion of the drop operation, a “New Package”
creation wizard will show up as shown in the figure
below. The wizard can be used to configure the different
attributes (name, namespace...) of a Struts 2 Package
component. Please enter the values as shown in the
figure.

Name: Field to set the name of the Struts 2 Package component.
Namespace: Field to set the namespace of the Struts 2 Package component.
Extends: Field to set the Extends relationship between packages. The Add button can be used to select from a list of packages to extend.
-
On pressing the Finish button, a graphical
representation for the Struts 2 package component will
get created in the Flow editor. A new Package entry will
automatically get added to the struts.xml file. You can
view the same by switching to the Source tab in the
editor. The created artifacts are shown in the figures
below.
-
Tips:
-
Right click->MyEclipse->New Package context menu
action can also be used to add packages to the Flow
editor.
-
The Struts 2 configuration file can directly be edited
in the Source Editor. For instance you can add a new
package entry in the configuration file directly and
then switch to the Flow editor and the graphical
representation for the package component will get
generated automatically in the Flow editor. The two
editors (Source and Flow) can be used interchangeably
as needed.
- Creating Struts 2 Actions -
Struts 2 Actions are the fundamental units of the
framework. An action element represents the mapping
between the identifier and the handler class. The
framework uses this mapping to determine how to process
the request. In this section we are going create an
Action that will process the request to open the login
page.The Action tool in the palette can be used to create
the Struts 2 Action components in the Flow editor. The
following steps demonstrate the process.
Select the Action tool from the palette and drop it
inside the login package in the Flow editor.
On completion of the drop operation, a “New Action”
creation wizard will show up as shown in the figure
below. The wizard can be used to configure the different
attributes (name, Class...) of a Struts 2 Action
component. Please enter the values as shown in the
figure.

Package name: The drop down to select the package that
will contain the new action. A new package can be
created using the New button.
Name: The name of the Action.
Action class: Can be used for mapping a
action handler with the action identifier. You can
create a new handler or map to an existing handler
class.
Class: The Action handler class. Selecting
Existing Class radio button will enable the Browse
button.
Parameters: Shows the list of action
parameters.
Results: Shows the list of Results in the
action.
On pressing the Finish button, a graphical
representation for the Struts 2 package component will
get created in the Flow editor. A new Package entry will
automatically get added to the struts.xml file. A
handler class will be created in the Java package
“com.myeclipseide.example.myblog.login”. The created
artifacts are shown in the figures below.
Tips:
-
To Edit an action parameter, just click on the
parameter and the cell will become editable. You can
edit the parameter name and value.
-
You can create new Results (explained in the later
section) directly from the New Action wizard.
-
Selecting the result in the Action wizard will enable
the Edit button. You can edit the results directly
from the action wizard.
-
You can also add new Actions by directly editing the
Struts 2 configuration file in the Source editor.
Switching to the Flow editor will automatically create
the graphical representation for the actions.
- Creating JSP/HTML Web resources -
The JSP/HTML nodes represent the web resources in the
application. The JSP and the HTML tool can be used to
create JSP and HTML pages. In our application, we will be
creating a set of JSP pages. The following steps
demonstrate the creation of “loginPage.jsp”.
-
Select the JSP tool from the palette and drop it inside
the Flow editor.
-
On completion of the drop operation, a “New JSP”
creation wizard will show up as shown in the figure
below. The wizard can be used to configure the
different attributes (path, name ...) of a JSP page .
Please enter the values as shown in the figure.

File Path: Field to specify the path of the
folder containing the web resource.
File Name: Name of the web resource.
Template to use: You can choose between the
different templates provided.
-
On pressing the Finish button, a graphical
representation for the “loginPage.jsp” node will get
created in the Flow editor.
- Creating Struts 2 Results -
An action result represents a possible outcome of an
operation. An action mapping will often contain a set of
results. The action handler method will return a String
(as shown in the figure above), that is used by the
framework to select the result element. In our
application, the “ShowLoginPage” action will contain a
single result with name “success” and type “dispatcher”
which will simply forward the request to another web
resource, in our case the “loginPage.jsp”. The Result
tool in the palette can be used to create new Struts 2
Result component in the Flow editor. The following steps
demonstrate the process.
-
Select the Result tool from the palette and drop it
inside the “ShowLoginPage” action in the Flow editor.
-
On completion of the drop operation, a “New Result”
creation wizard will show up as shown in the figure
below. The wizard can be used to configure the
different attributes (name, type...) of a Struts 2
Result component. Please enter the values as shown in
the figure.

Name: The name of the action result. You can
choose between the standard set of result tokens or set
a custom name.
Result type: Drop down menu to choose the
result type of the result.
Location: Field to specify the web resource to
render the outcome of the operation.
Parameters: Shows the list of result
parameters. You can add /remove parameters using the
Add/Remove buttons.
-
On pressing the Finish button, a graphical
representation for the Struts 2 Result component will
get created in the Action node in the Flow editor. A
new Result entry will automatically get added to the
struts.xml file. The created artifacts are shown in the
figure below.
Tips:
-
In the Result wizard, the Location field can be kept
empty while creation and then you can make use of the
Extends/Location tool to connect to a Web resource node
in the graphical editor.
-
You can also create results directly using connections.
Select the Extends/Location tool from the palette, in
the flow editor, select an action and create a
connection with the required target(JSP/HTML or Action)
node. This will show up the New Result wizard with some
pre-populated filed values based on the target of the
connection. Finishing the wizard will add the new
result in the action. NOTE: This feature is available
from MyEclipse 9.0M2 release.
-
You can also change the target of the existing
connection by selecting the target end of the
connection and dragging it onto another target node.
For instance, you have a result connected to the
loginPage.jsp node and at some point you want to change
it to anotherLoginpage.jsp. You can edit the result
directly or you can simply select the connection
endpoint (the one on the target side) and drag it onto
the anotherLoginpage.jsp node. The changes are
automatically applied to the Source/XML file.
- Creating remaining components for MyBlogStruts2Example -
In this section we will be creating the remaining
components of our sample Blog application. The techniques
used here will be exactly same as explained in the above
sections. We will be looking at two aspects of our
application - a) Validation of the login credentials, b)
create, review and delete blogs.
Validation of the login credentials -
Create a package named “secure”, namespace “/securens”
and extend the “struts-default” package. This package
will group all the actions that deal with security. To
keep things simple we will be creating a single action
that will perform the task of validating the username
and password values. For invalid credentials, the
request will be forwarded back to the login page and
an error message will show up in the login page.
Create an action with name “AuthenticateUser” in the
secure package. Specify the value for the class field
as
“com.myeclipseide.example.myblog.secure.AuthenticateUser”.
This should add an action to the package and create a
new handler Java class named AuthenticateUser in
package Java “com.myeclipseide.example.myblog.secure”.
Add two results to the action. The first result with
the name “error” and type “dispatcher” that will
forward the result back to the login page and second
result with the name “success” with the type
“redirectAction”. This result will redirect the
request to another page that contain options to
create, review and delete blogs. The created artifacts
are shown in the figure below.

NOTE: In the figure above, the success result (name =
success, type = redirect action) is not connected to
an action or a JSP page. We will be doing it in the
later sections after creating the remaining components
of our application.
The logic to validate the username/password is shown
in the snippet below. You can copy it directly in the
“AuthenticateUser” handler class.

NOTE: To keep things simple all the username/password
values are accepted except empty values.
-
Create, Review and Delete blogs -
-
Create a package named “blogging” and namespace
“/bloggingns” that will group all the actions that
deal with the creation, review and deletion of blogs.
Creation an action named “ShowBlogAdminPage” that will
do a simple job of forwarding the request to the
“blogAdminPage”. In the new action wizard, set the
Class field to “ShowBlogAdminPage”. The “success”
result in the above section (inside the
AuthenticateUser action) is mapped to this action. The
framework will forward the request to this action if
the validation succeeds.
-
Select the Extends/location tool in the palette and
create a connection from the Success result (present
inside the “AuthenticateUser” action) to the
“ShowBlogAdminPage” action. The result of the
operation is shown in the figure below.

NOTE: The above figure shows how you can use the redirect action result type.
-
Similar to the “ShowBlogAdminPage” action above,
create actions “CreateBlogPost, ReviewAllPosts,
ShowDeleteBlogPage and DeleteBlogPost” in the package
blogging. In the new Action wizard, set the handler
names same as the action names mentioned above.
Create these actions under package
“com.myeclipseide.example.myblog.blogging”.
-
Create a JSP page “createBlogPage” in the Flow
editor. Add a new Result with name “success” and type
“dispatcher” in the “CreateBlogPost” action. In the
New Result wizard, set the Location value to
“createBlogPage.jsp”.
-
Add similar results to the “ReviewAllPosts” and
“DeleteBlogPost” actions. Forward the result in the
“ReviewPostsPage.jsp” to the “reviewPostsPage.jsp”
and the result in the “DeleteBlogPost” action to the
“deleteBlogPage.jsp”. The figure below shows the
final result of all the above operations.

-
The following snippet shows the contents of the
loginPage, blogAdminPage.jsp, PublishBlogPage,
createBlogPage.jsp, reviewPostsPage.jsp,
deleteBlogPage.jsp pages.
loginPage.jsp:

blogAdminPage.jsp:

publishBlogPage.jsp:

createBlogPage.jsp:

reviewPostsPage.jsp:

deleteBlogPage.jsp:

index.jsp:

|
|
|
|
|
In this section we are going to look at the Source editor
and all its features. As previously mentioned, you can
directly use the Source editor to edit your Struts 2
configuration files. The editor is very well equipped to
handle complex Struts 2 configuration files.
- Content Assist support -
The Source editor provides content assist support
for all the Struts 2 elements. The content assist can
be invoked using the “CNTR + Space” key combination.
The screenshots below shows the usage of content
assist in the Source editor.
The figure shows the content assist used to add the
package attributes.
The figure shows content assist support that can be
used to extend packages.
- Hyperlink detection support -
The editor also provides support for detecting
hyperlinks. The hyperlink detection can o be activated
by placing the mouse over the hyperlink and pressing
CNTR key. Clicking the hyperlink will open the
associated document in the target editor. The images
below show the usage of Hyperlink detection in the
Source editor.


- Formatting support -
The Source editor also supports formatting the Struts 2
configuration file. This can be done using the “CNTR +
F” key combination.
- Validation support -
The Struts 2 editor provides excellent support for
custom Struts 2 validations. The editor starts
validating as you start typing in the Source editor and
keeps providing feedback in the form of error messages that
helps the user in correcting the errors immediately.
The custom validaton mechanism is shown in the figures
below.
Figure shows the error feedback if you provide an empty
package name.
Figure shows the error message if you extend a package
that is not defined in the configuration file.
Figure shows the error message if you include a
configuration file that doesn’t exists.
Figure shows the error feedback when the result
references an action that is not present in the given
namespace.
|
|
|
|
|
In this section we are going to look at the outline view
provided my the MyEclipse Struts 2 editor. The Figure below
shows the outline view. You can add/delete/edit elements
directly from the outline view.
The figure shows the Outline view with the MyEclipse
context menu that can be used to add Struts 2 elements to
the editor.
|
|
|
|
|
-
Auto Layout -
The MyEclipse Struts 2 editor provides excellent auto
layout support. To autolayout the diagram, invoke the
“Auto Layout” action from the MyEclipse context menu in
the Flow editor. The auto-layout operation takes care
of layouting all the elements in the diagram. It also
performs smart routing of connections. The Figure below
shows the Auto layout context menu action.
-
View Menu -
The View menu in the editor provides zoom in, zoom out
action, grid and snap to geometry options. The Figure
below shows the View menu.
-
Exporting to image action -
The MyEclipse Struts 2 editor provides support for
exporting the active diagram to .jpg and .png
formats. To export to an image, invoke the “Export
Image” action from the MyEclipse context menu in the
Flow editor. The Figure below shows the Export image
context menu action.
|
|
|
|
|
In this section we are going to test our application by
running it. Please follow the steps below to deploy and
then test the application.
-
Select the “MyBlogStruts2Example” project and from the
right click context menu, perform Run As->MyEclipse
Server application. This will deploy our application to
the MyEclipse Tomcat Server. The project is deployed, the
server is started and the the index.jsp page of the
project is automatically opened in a browser.
-
The index.jsp page contain a link - Start Blogging. Press
the link and the “loginPage.jsp” will show up.
-
The index.jsp page contain a link - Start Blogging. Press
the link and the “loginPage.jsp” will show up.
-
In the admin page, press “Create” to create a new blog,
“Review” to get a list of all the posts, “Delete” to
delete a post and “Log out” to log out of the
application.
|
|
|
|
|
In this tutorial we developed a simple Struts 2 application using the Struts 2 tooling support available in MyEclipse Enterprise Workbench.
The sample application can be found here - Sample Struts 2 Project
|
|
|
|
|
If you have comments or suggestions regarding
this document please submit them to the
MyEclipse
Documentation Forum. For more
information visit the MyEclipse
Tutorial library.
|
|
|
|