Friday, 8 August 2008

Struts2 Online Book By Ian Roughley

I tried to read online docs downloaded from struts website.

I throughly enjoyed working through the existing examples available at this site

I wanted to share few important points i found from the online articles.

a) Apache Struts 2 is an elegant, extensible framework for creating enterprise-ready Java web applications. The framework is designed to streamline the full development cycle, from building, to deploying, to maintaining applications over time.

Apache Struts 2 was originally known as WebWork 2. After working independently for several years, the WebWork and Struts communities joined forces to create Struts2. This new version of Struts is simpler to use and closer to how Struts was always meant to be.

b) s:i18n tag is used to support internalization and the supported property file should be placed in any location but in classpath.

<s:i18n name="alternate"><a href="http://struts.apache.org/">
<img src="<s:text name="struts.logo.path"/>"
alt="<s:text name="struts.logo.alt"/>" border="0px"/>
</a>
</s:i18n>

alternate.properties should be existing.

There is a free Starting Struts2 By Ian Roughley book download available at this site

c) Please visit this site for more examples of struts2 and they are really cool examples where it divides in 3 steps

i) Step 1: Getting Started example example.
ii) Step 2: Dependency Injection With Spring example
iii) Step 3: Data Access With Hibernate
iv) Step 4: Decorating the Address-Book Using SiteMesh


Below are few points from the book.

1) With so many choices out there, why choose Struts2? Here are some of the features that may lead you to consider Struts2:

a) Action based framework
b) Mature with a vibrant developer and user community
c) Annotation and XML configuration options
d) POJO-based actions that are easy to test
e) Spring, SiteMesh and Tiles integration
f) OGNL expression language integration
g) Themes based tag libraries and Ajax tags
h) Multiple view options (JSP, Freemarker, Velocity and XSLT)
i) Plug-ins to extend and modify framework features

2) In the beginning of 2005, a new fascination was starting in web development. Coined by Jesse James Garrett, Ajax stood for “Asynchronous JavaScript and XML.” Relatively speaking, the technologies were nothing new. In fact, the primary web browser components for making the asynchronous calls – the XMLHttpRequest Object – had already been available for 6 years (since version 5 of Internet Explorer).
But what was new was the application of the technology.Google Maps was one of the first applications to take full advantage of the technology.

3) From a high level, Struts2 is a pull-MVC (or MVC2) framework; this is slightly different from a traditional MVC framework in that the action takes the role of the model rather than the controller, although there is some overlap. The “pull” comes from the views ability to pull data from an action, rather
than having a separate model object available.

4) The Model-View-Controller pattern in Struts2 is realized with five core components – actions, interceptors, value stack / OGNL, result types and results / view technologies.

5) The Include Tag:

The <include … /> tag is used to modularize a Struts2 application by including other configuration files and is always a child of the <struts> tag. It contains only one attribute “file” that provides the name of the file to be included – which is a file that has exactly the same structure as the “struts.xml”
configuration file.

6) In Struts2 an action can be used in a couple of different ways.

a) Single Result

The first, and most basic usage of an action, is to perform work with a single result always being returned. In this case, the action would look like this:

class MyAction {
public void String execute() throws Exception {
return "success";
}
}


A few things are worth noting. First, the action class does not need to extend another class and it does not need to implement any interfaces. As far as anyone is concerned, this class is a simple POJO.

Second, the class has one method named “execute”. This name is the one used by convention. If you wanted to call it something other than “execute”, the only change needed would be in the actions configuration file. Whatever the name of the method is, it will be expected to return a String result code.The actions configuration will match the result code the action returned to a specific result that will be rendered to the user. If needed, the method can also throw an exception.

The simplest configuration for the action looks like this:

<action name="my" class="com.fdar.infoq.MyAction" >
<result>view.jsp</result>
</action>

The attribute “name” provides the URL information to execute the action, in this case a URL of “/my.action”. The extension “.action” is configured in the “struts.properties” configuration file. The attribute “class” provides the full package and class name of the action to be executed.

b) Multiple Results

The next, slightly more complicated use is when the action can return different results depending on the outcome of the logic. The class looks similar to the previous use:

class MyAction {
public void String execute() throws Exception {
if( myLogicWorked() ) {
return "success";
} else {
return "error";
}
}
}


Since there are now two different results that can be returned, we need to configure what is to be rendered back to the user for each case. Hence, the configuration will become:

<action name="my" class="com.fdar.infoq.MyAction" >
<result>view.jsp</result>
<result name="error">error.jsp</result>
</action>

This introduces a new “name” attribute of the result node. In fact, it has always been there. The value (as in the first result configuration) defaults to a value of “success” if not provided by
the developer.

7) Many of the features provided in the Struts2 framework are implemented using interceptors; examples include exception handling, file uploading, lifecycle callbacks and validation.Interceptors are conceptually the same as servlet filters or the JDKs Proxy class.

8) Value Stack / OGNL
The value stack is exactly what it says it is – a stack of objects. OGNL stands for Object Graph Navigational Language, and provides the unified way to access objects within the value stack.

The value stack consists of the following objects in the provided order:

a. Temporary Objects – during execution temporary objects are created and placed onto the value stack; an example of this would be the current iteration value for a collection being looped over in a JSP tag.

b. The Model Object – if model objects are being used, the current model object is placed before the action on the value stack.

c. The Action Object – the action being executed.

d. Named Objects – these objects include #application,#session, #request, #attr and #parameters and refer
to the corresponding servlet scopes.

9) Result Types

So far we have shown action configurations that result in a JSP being rendered to the user. This is one case, but not the only one. In fact, Struts2 supports many types of results. These can be visual, or they can be interactions with the environment.

To configure an action to execute a result of a specific type, the “type” attribute is used. If the attribute is not supplied, the default type “dispatcher” is used – this will render a JSP result.
Here’s what the action configuration looks like:

<action name="my" class="MyAction" >
<result type="dispatcher">view.jsp</result>
</action>

10) Modularization

Being able to split web applications into modules becomes important as applications become large. It allows functionality or new framework features developed on one project to be packaged independently, and then re-used across other projects.Struts2 has adopted modularization as a fundamental part of the
architecture, allowing developers to work independently and build upon each other’s work.

There are a few ways that applications can be modularized:

a) Configuration information can be split into multiple files
– this does not affect the packaging of the application,but makes the development easier as configuration
information is easier to find and logically separated along functional boundaries

b) Self-contained application modules can be created as plug-ins
- everything that is needed to provide a particular feature can be packaged together and
independently distributed as a plug-in; this includes actions, interceptors, interceptor stacks, view templates (except JSPs), etc. An example is the config browser plug-inix, this plug-in provides a complete module that,when added to your application, provides a web interface to view configuration information

c) New framework feature plug-ins can be created – new functionality that is non-application specific can be
bundled as a plug-in and used across many difference applications

11) Tiles

Apache Tiles, just like SiteMesh, is installed by adding the plugin xiv into your web applications “/WEB-INF/lib” directory.

In order to load the tiles configuration, a servlet listener needs to be configured:

<listener>
<listener-class>
org.apache.struts2.tiles.StrutsTilesListener
</listener-class>
</listener>

The listener loads the “tiles.xml” configuration file, which defines each tile for your application, from the “WEB-INF” directory. Unlike SiteMesh, Tiles are implemented as a new result type. Each action result that wishes to use a Tiles layout needs to provide the attribute “type” with value “tiles” (or alternately set the Tiles result to be the default), and provide the name of the tile to use. The Tile name needs to be defined in the configuration file “tiles.xml”.

<action name="my" class="com.fdar.infoq.MyAction" >
<result type="tiles">myaction.layout</result>
</action>

Version 2 of Tiles is used in Struts2. This version has not yet had a stable release and could undergo further change. For this reason Tiles support is currently marked as experimental in Struts2.

12) Spring Framework

Installing Spring support involves downloading and copying the Spring plug-inxvii into your web applications “/WEB-INF/lib” directory.

To the “web.xml” configuration file you will need to add two blocks of code. The first registers a listener that enables Spring integration for application objects:

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

And then the location of the Spring configuration file needs to be specific. In this case, any XML file starting with “applicationContext” will be loaded:

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:applicationContext*.xml
</param-value>
</context-param>

About the Author

Ian Roughley is a speaker, writer and independent consultant based out of Boston, MA. For over 10 years he has been providing architecture, development, process improvement and mentoring services to clients ranging in size from fortune 10 companies to start-ups. His professional background includes work in the financial, insurance, pharmaceutical, retail, elearning,hospitality and supply-chain industries.

Focused on a pragmatic and results-based approach, he is a proponent for open source, as well as process and quality improvements through agile development techniques. Ian is a committer to the WebWork project; member of the Apache Struts PMC; and speaker at No Fluff Just Stuff Symposiums. He is also a Sun Certified Java Programmer and J2EE Enterprise Architect; and an IBM Certified Solutions Architect.

No comments: