Tuesday, July 24, 2007

Difference between Chain and Redirect Action result types

Chain result type is used for Action Chaining which means that the source result invokes an entire other action, complete with it's own interceptor stack and result.

Redirect Action result type is used to redirect to another Action which means making your source Action, after it has successfully executed, result in a redirect.

As a rule, Action Chaining is not recommended. Redirect Result or the Redirect Action Result is preferred over Chain Result.

Friday, June 8, 2007

Common Support class for Action classes

Create a support class for your actions and implement all the common logic for your actions in this class. Make this class extend ActionSupport and then in your action classes, just extend this support class. Following is a snippet of my ApplicationSupport class:

public class ApplicationSupport extends ActionSupport implements SessionAware, ParameterAware {
/**
* Field to store session context.
*/
private Map session;

/**
* Field to store request parameters.
*/
private Map parameters;

/**
* Result code for session expiry action.
*/
public final static String SESSION_EXPIRED = "SessionExpired";

...

You can handle following common concerns in the ApplicationSupport class:
1. Session
2. Parameters
3. Token
4. Validating session (though I would like to create an interceptor for this)
5. Common result codes etc.

ApplicationSupport class would reduce your action class code to a large extent by handling lot of common concerns in one centralized place.

Thursday, June 7, 2007

Accessing Request parameters in your Action class

1. Make your Action implement the ParameterAware interface.

2. Define a class variable for parameters Map:
private Map parameters;

3. Create getter and setter methods for the above defined parameters Map.

4. Calling getParameters() gives you access to the request parameters through the above defined Map.

5. To make things simple, you can create a getParameterValue() method to return value of a request parameter by its name:

public String getParameterValue(String param) {
Object varr = getParameters().get(param);
if (varr == null) return null;
return ((String[]) varr)[0];
}

Tuesday, June 5, 2007

Accessing Session

1. Make your Action implement the SessionAware interface.

2. Define a class variable for session Map
private Map session;

3. Define setSession() and getSession() methods.

4. Calling getSession() gives you access to session attributes through the above defined session Map. Any changes made to the session Map are reflected in the HttpSessionRequest object.

Saturday, June 2, 2007

Struts 2 - Get Started

Following are few links to get you started with Struts 2:

1. A Walking Tour of the Struts 2 MailReader Application : Perfect place to learn walking and getting ready for the run.

http://www.planetstruts.org/struts2-mailreader/Tour.do


2. Struts 2 Book : A free book for download, courtesy of Ian Roughley and InfoQ.com.

http://www.infoq.com/minibooks/starting-struts2


3. Migrating from Struts 1 to Struts 2 : Few links to help with migration.

http://www.infoq.com/articles/converting-struts-2-part1

http://opensource.atlassian.com/confluence/oss/download/attachments/4942/migrating.pdf?version=1

http://www.denverjug.org/meetings/files/200611_MigratingFromStruts1ToStruts2.pdf


4. Mark's Struts 2 Cookbook : Learn some nice recipes.

http://www.vitarara.org/cms/struts_2_cookbook


5. apache.org : Last but not the least, The Mothership.

http://struts.apache.org/2.x/docs/home.html