To run this sample project Eclipse, download and install the WAR file:
StrutsIntro.war.
In NetBeans, download the
StrutsIntro.zip archive.
These "Struts2 Essential" JAR files are needed for NetBeans:
Struts2 is
an enhancement of Struts (version 1), which is
one of the older Java web frameworks. The
1.x series is what is currently used in NetBeans, but the most active
development work is in the newer 2.x series.
Struts 2 is based on the OpenSymphony WebWork Framework and represents
the collaboration of Struts and WebWork developers.
Struts 2 framework is a MVC-style framework where the controller is expressed
in terms of XML configuration files and Java "action" classes, and the
view is JSP files using dedicated tags.
According to the
description on the Struts2 page, this framework provides three key components:
A "request" handler provided by the application developer that is mapped to a
standard URI.
A "response" handler that transfers control to another resource
which completes the response.
A tag library that helps developers create interactive form-based
applications with server pages.
One key idea behind Struts2's MVC approach is that all
URL requests must be associated with an action,
as defined in a configuration file. In particular, all
requests of a certain type
must be filtered through a specialized handler
scheme as specified by the basic web.xml configuration
file:
web.xml
<web-app ...>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
...
</web-app>
The default extension used by Struts2 is ".action", i.e.
any URL:
http://.../______.action
will "go through" the Struts2 processing. The processing
associates the response with a certain Java class
and a template output, often a single JSP file.
This association
mechanism can be made in a number of ways, but, perhaps
the most common is through the file struts.xml located
in the source folder:
struts.xml
<!DOCTYPE struts ...>
<struts>
<package name="default" extends="struts-default">
<action name="foo" class="action.Bar">
<result>/success.jsp</result>
</action>
<action name="sample" class="action.Bar">
<result>/sample.jsp</result>
</action>
<action name="testing" method="test" class="action.Bar">
<result name="failed">/failed.jsp</result>
<result>/success.jsp</result>
</action>
</package>
</struts>
The final ingredient in this example is the action class
supporting the "result" JSP files. In this case it is
action.Bar
package action;
public class Bar {
private String info;
public String getInfo() { return info; }
public void setInfo(String info) { this.info = info; }
private String data = "initial data";
public String getData() { return data; }
public void setData(String data) { this.data = data; }
public String initial() { return "success"; }
public String execute() { return "success"; }
public String test() {
if (info.trim().isEmpty())
return "failed";
else
return "success";
}
}
Sample Actions
We will explore a sample Struts2 form using these tags:
In this case, with no method defined, by default the
String execute()
method from action.Bar class is called, returning
the success result string. The unnamed
result tag within
matches the success output and the
success.jsp "template" is activated:
success.jsp
<body>
<h2>success.jsp</h2>
info: ${info}
<br />
data: ${data}
</body>
The two EL expressions correspond:
${info} = getInfo()
${data} = getData()
Struts2 employs what it calls the valueStack in that
a bean representing an instantiation of the action.Bar class
is "pushed on the stack" during the generation of the
response, making info and data refer to this bean.
The setter methods, setInfo and setData, refer to
the textfields with names info and data within the
form, respectively. While generating the response for
the action, these methods are invoked implicitly using
the data entered into the respective fields.
Invoking sample.action
One sees different behavior when the second hyperlink
is activated even though
the same script, sample.jsp, is used.
In this case the second action element is referenced:
Again, the execute method is called, yielding a "success"
result, but this time calling sample.jsp.
The difference,
easily noted, is that now that data textfield is
initialized by virtue of the value of
getData from in newly instatiated action.Bar object.
The "Press Me Next" button
The "Press Me Next" button specifies its own action:
<s:submit action="testing" value="Press Me Next" />
Pressing this invokes the testing action, referring to
this entry in struts.xml:
The method attribute indicates that the following, not
the default execute method be called:
public String test() {
if (info.trim().isEmpty())
return "failed";
else
return "success";
}
The method can generate two possible result outcomes:
"failed" or "success".
The result tags within indicate
that the failed.jsp script is be used in the former
case and success.jsp in the latter case.