Database Reference
In-Depth Information
Apache Struts 2
Apache Struts 2 is a Java implementation of an MVC framework. The aim of the framework is to help you quickly
build out powerful web applications and APIs using only what is absolutely necessary to get the job done. The Struts 2
library and its dependencies are included with the sample application and should not require any additional
configuration to run the application.
As with other MVC frameworks, one of the most important aspects of Struts 2 is the handling of request routing.
Listing 12-1 demonstrates the basics of Struts 2 with the XML configuration option.
Listing 12-1. Example of struts.xml Configuration for a Package and Its Actions
<struts>
<package name="default" extends="struts-default">
<action name="Logon" class="mailreader2.Logon">
<result name="input">/pages/Logon.jsp</result>
<result name="cancel" type="redirectAction">Welcome</result>
<result type="redirectAction">MainMenu</result>
<result name="expired" type="chain">ChangePassword</result>
</action>
<action name="Logoff" class="mailreader2.Logoff">
<result type="redirectAction">Welcome</result>
</action>
</package>
</struts>
However, the addition of annotations to Struts 2 has allowed request routing to be placed directly within the
controller class. Most of the sample code uses the annotation method to keep the configuration variables closer to the
action to which it is connected. As with the XML configuration, you can set up parent packages that set up top-level
configuration that can then be reused within child packages. In Listing 12-2, you will see that the “HomeAction” uses
the root namespace and extends the parent package that is named “practicalneo4j-struts-default”. In addition to being
able to see the package and namespace settings at a glance, it allows you to configure and view the specific actions
and results within the class file.
Listing 12-2. Example of Annotation Configuration
@ParentPackage("practicalneo4j-struts-default")
public class HomeAction extends GraphStoryAction {
private static final long serialVersionUID = 1L;
@Actions({
@Action(value = "home", results = {
@Result(name = "success", type = "mustache", location = "/filepath")})
})
public String home() {
setTitle("Home");
return SUCCESS;
}
}
 
Search WWH ::




Custom Search