Saturday 28 July 2007

Working with cookies in Struts 2

I have been working with Struts for a very long time now, in fact, I still clearly remember using perform method in the action classes :^) . And then, a few days ago, I got an opportunity to try my hand at Struts 2, the reincarnation of Struts, and at a first glance, it looks impressive, to say the least.

Coming to the subject, when I googled for how to work with cookies in struts 2, I could only find a couple of references, viz. struts-users mail archive link and CookieInterceptor javadoc . So, I'll take this opportunity to share whatever little I have picked up.

Let's divide this into two parts -
- Receiving cookies in an action
- Adding cookies to a response in an action

First, let's see how to receive cookies in an action. I could figure out two options by which it can be achieved. Well, there could possibly be other options available as well, but I'm sure you'll excuse me, as this post is based on my extremely limited knowledge of struts 2 that I could manage to gather in past couple of days. Please feel free to enlighten me if you know more about it. Both options need the CookieInterceptor to be configured for the action that needs to receive cookies. Here are the two options -

Option 1 - Filter cookies in the CookieInterceptor configuration and add properties with the cookie names to the action class
Option 2 - Make the action implement CookiesAware interface and receive a map that contains cookies received

Let's say we have an action class Greeter that expects a cookie named flash in the incoming request. Here is the action configuration for the CookieInterceptor. The param tag under interceptor-ref tag indicates the cookie name that is expected by the action.

<action name="Greet" class="com.omkarpatil.Greeter">
<result>/greet.jsp</result>
<interceptor-ref name="cookie">
<param name="cookiesName">flash</param>
</interceptor-ref>
</action>

To Use the first option, a field named flash with getters/setters needs to be added to Greeter action. The CookieInterceptor does it's magic at runtime to make the cookie available into the action. Here is the code snippet for Greeter action -


public class Greeter extends ActionSupport {
private String message;
private String flash;

public String getFlash() {
return flash;
}

public void setFlash(String flash) {
this.flash = flash;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public String execute() throws Exception {
setMessage("Hello " + getFlash());
return SUCCESS;
}
}

To use the second option, the Greeter action needs to implement CookiesAware interface. CookiesAware interface defines one method void setCookiesMap(Map cookies), which the CookiesInterceptor uses to carry out interface injection into the action at runtime.


public class Greeter extends ActionSupport implements CookiesAware {
private Map cookiesMap;

public Map getCookiesMap() {
return cookiesMap;
}

public void setCookiesMap(Map cookiesMap) {
this.cookiesMap = cookiesMap;
}

public String execute() throws Exception {
// Do whatever you want to with the cookies map
return SUCCESS;
}
}

Well, that covers the "Receiving cookies into action" part. Onto the "Adding cookies to a response" in the next post. Stay tuned.