Monday, December 22, 2008

Creating custom result type - struts 2

Creating a custom result type comes to the field when your are trying
to develop a Ajax base development with struts 2. By default, struts 2 uses RequestDispatcher originated from servlets API, as the default result type. That is, after returning the control string from your action, this default result type will show the way for final page rendering.

Suppose you want to have some data response to the client, in stead of despatching to a JSP page after returning from your action class. Ajax request to the server often needs this kind of data.
Here, I am going to explain you, how to make a Ajax request to a struts 2 action class and how to use a custom result type to send some data back to the client, instead of rendering a another JSP page as usual. I am not going to give you a detailed description on Ajax related things. My major concern is how to create a custom result type and use it with Ajax development.

I have a list of User's names in a struts select box component and onchange event will make a request to a action class to get full details about the user to the client and update some segment of current rendered page of the browser as usual Ajax application does. I am passing userId as query string parameter to the action class.
Here is the javascript code the responsible to make the request to the server. I have encpsulated all related functions needed to make a request to the server, into one javascript object.

var PDA = {};
PDA.common = {};
PDA.common.ContentLoader = function(httpMethod,url,params,callBack){
PDA.common.ContentLoader.prototype = {
httpMethod : null,
url : null,
params : null,
callBack : null,
error : null,
request : null,

      alert("error fetching data!"
      +"\nstatus: "+this.request.status
      +"\nheaders: "+this.request.getAllResponseHeaders());

getXMLHttpRequest: function(){
     this.request = new XMLHttpRequest();
   }else if(typeof ActiveXObject != "undefined"){
       try {
           this.request = new ActiveXObject("Msxml2.XMLHTTP");
       }catch(e) {
                  this.request = new ActiveXObject("Microsoft.XMLHTTP");
              } catch (e){
                    this.request = null;

onReadyState: function(){
 var req = this.request;
 var ready = req.readyState;
 if (ready == this.READY_STATE_COMPLETE){
     var httpStatus = req.status;
    if (httpStatus == 200 || httpStatus == 0){;

sendRequest: function(){
             var loader = this;
             this.request.onreadystatechange = function(){

init: function(httpMethod,url,params,callBack){
      this.httpMethod = httpMethod;
      this.url = url;
      this.params = params;
      this.callBack = callBack;
      this.error = this.defaultError;
The above object almost contains all the function to make a Ajax request to the server. I will simply explain the constructor parameters need to initiate object from this class.
  • httpMethod : eidther "POST" or "GET"
  • url : request url (in my case, it is action name)
  • params : url parameter needed to pass to the server
  • callBack : javascrip function name which handles the response data and update the client browser content
If you want to make a request to the server, you can simply initiate an object from this class. Easy isn't it? Now the time to call the action.
My user list select box' onchange event will fire the fallowing javascrip function which get the user id form the select box and make a request to the "ViewUsers" class.
function fetchUser(){
    var selectBox = document.getElementById('userId');
    var selectedIndex = selectBox.selectedIndex;
    var selectedValue = selectBox.options[selectedIndex].value;
    new PDA.common.ContentLoader("POST",
                                 "ViewUsers.action?userId=" + selectedValue,
"showUsers" function is the callback function in my case which is responsible for client side updates. Let's see the source code for ViewUsers action class next.
package com.axiohelix.pda.actions;
import org.hibernate.Transaction;
import com.opensymphony.xwork2.ActionSupport;

public class ViewUsers extends ActionSupport{
 private static final long serialVersionUID = 1L;
 private int userId;
 private User user;
 private Object jsonModel;
 public String execute(){
  String result = null;
   //do some thing to get the user object from userId
   result = SUCCESS;
  }catch(Exception e){
   result = ERROR;
  return result;
 public int getUserId() {
  return userId;

 public void setUserId(int userId) {
  this.userId = userId;

 public User getUser() {
  return user;

 public void setUser(User user) {
  this.user = user;

 public Object getJsonModel() {
  return jsonModel;

 public void setJsonModel(Object jsonModel) {
  this.jsonModel = jsonModel;
As you can see, I have created User object from the userId and exposed it into the ValueStack under the property of "jsonModel". After returning from this action class, it should hit our custom result type and should send the user informations back to the client. Here, I am going to use JSON which provides a succinct text-based serialization of data objects. JSON can be a powerfully succinct and lightweight means of communication between a web application server and an Ajax client.
Next, we will see the core of the post.We are going to create the custom result which sends JSON data string back to the client. The only requirement needs to create a struts2 custom result is, implementing Result interface of struts 2. Here is the source code to create a custom result type and serialize the object in to JSON.

package com.axiohelix.pda.jsonresult;


import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.Result;
import com.opensymphony.xwork2.util.ValueStack;
import com.thoughtworks.xstream.XStream;

public class JSONResult implements Result{
 private static final long serialVersionUID = 1L;
 public static final String DEFAULT_PARAM = "classAlias";
 String classAlias;

 public String getClassAlias() {
  return classAlias;
 public void setClassAlias(String classAlias) {
  this.classAlias = classAlias;
 public void execute(ActionInvocation invocation) throws Exception {
  ServletActionContext.getResponse().addHeader("Cache-Control", "no-cache");
  PrintWriter responseStream = ServletActionContext.getResponse().getWriter();
  ValueStack valueStack = invocation.getStack();
  Object jsonModel = valueStack.findValue("jsonModel");
  XStream xstream = new XStream(new JettisonMappedXmlDriver());
  if ( classAlias == null ){
   classAlias = "object";
  xstream.alias(classAlias, jsonModel.getClass() ); 
  responseStream.println(xstream.toXML( jsonModel ));

Here, I have used two open source libraries, XStream and Jettison. XStream helps to serialize java objects into XML format while Jettison helps to create JSON from the XML.As you can see,I am getting "jsonModel" property exposed into the ValueStack from our ViewUsers action calss,progrmmetically and serialize it into JSON.
Now the time to tell ViewUsers action class about our custom result type. We have to add fallowing entries into our declarative architecture, ie struts.xml file. To declare our custom result type, you have to add fallowing into struts.xml file.
<result-type name="customJSON" class="com.axiohelix.pda.jsonresult.JSONResult">
To use our custom result type, we need to add the fallowing to struts.xml file.
<action name="ViewUsers" class="com.axiohelix.pda.actions.ViewUsers">
<result type="customJSON">user</result>
Here, you can see, I am passing in a parameter to the custom result type. It will be set to the "classAlias" field and finally, it will be the name of the root JSON object return to the client. Fallowing is the response JSON string representation of JSON


Saturday, December 6, 2008

Handling custom error pages - struts 2

When you work with struts 2, you may need to handle exception and
forward it to a custom error pages as you wish. The exception interceptor of struts 2 will catch exception and map them to user-defined error pages by type. For example, suppose you want to map all java.lang.Exception to a custom error page defined by yourself, you have to involve the exception interceptor.
You can add the fallowing entry to your struts.xml file just after the package element.

<result name="error">/WEB-INF/jsp/errorPage.jsp
<exception-mapping exception="java.lang.Exception" result="error" />

With this declaration, each java.lang.Exception exception will end up with your error page. Also you can display more details on error in your custom page.Before yielding control to the result, the exception interceptor will create an ExceptionHolder object and place it on top of the ValueStack. The Exception-Holder is a wrapper around an exception that exposes the stack trace and the exception as JavaBeans properties that you can access from a tag in your error page. The following snippet shows our error JSP page:

Exception Name: <s:property value="exception" />

What you did wrong:<s:property value="exceptionStack" />

We can use the
exception and exceptionStack property which are already exposed to ValueStack by ExceptionHolder object to get the details about the exception into the error page.

Tuesday, December 2, 2008

Object backed-components – Struts 2

The automatic data transfer of Struts 2 needs only to provide
JavaBeans properties on actions, using the same names as the form
fields being submitted. Object backed – components of Struts 2 allows us to transfer data as objects. Suppose you want
to create a new user.
We have User class as fallows,
public class User{
private String name;
private String email;
// getter and setter methods
If you use Struts 2 default data transferring process, you must give the same field names as input JSP’s field’s name, must write JavaBeans properties with the same name in you action class and you have to create User object by yourself to create a new user. If you use, object – backed components of Struts 2 for data transferring, We don’t even have to create the User object that backs the property because the framework’s data transfer will handle this for us when it starts trying to move the data over. You can have more cleaner action class as fallows.
public String execute(){
return SUCCESS;
private User user;
public void setUser(User user){
this.user = user;
public User getUser(){
return this.user;
You also need to do slight changes in your input JSP’s and the resulting JSP’s when addressing appropriate fields. For example, in the input JSP for creating a new user,
Similarly, when rendering the result,
Welcome <s:property value="" />
The minor consequences are that we have to go a dot deeper when we reference our data from the JSP pages.

Saturday, November 22, 2008

Vertically formatted CHECKBOXLIST - struts 2

This is one tricky point encountered by myself when creating
checkboxlist with struts 2. The default 'xhtml' them will always rendered
horizontally formatted list into the page. To have vertically formatted checkboxlist, we have to overwrite the default FreeMarker template for checkboxlist component.
If you check carefully, you can find the set of templates files
written in FreeMarker for each theme of struts 2 from struts-core.jar file inside the template folder. Here, I am going to overwrite the template file for checkboxlist component to have vertically formated checkboxlist.
Consider fallowing HTML mark up rendered into the view page from
checkboxlist component. It tries to list the users of the system preceding with a checkbox. User id has been set as the value of each checkbox.

<input type="checkbox" id="users-1" value="1000" name="users"/>
<label class="checkboxLabel" for="users-1">administrator</label>
<input type="checkbox" id="users-2" value="1001" name="users"/>
<label class="checkboxLabel" for="users-2">siriwardana</label>

You will get the fallowing out put.

This is a mark up of horizontally formated checkboxlist. If we can add additional '<br>' tag after each of this <label> tag, we are done !. We can have a vertically formatted checkboxlist. We have to overwrite checkboxlist.ftl file located at template/xhtml in struts-core.jar file. I do not suggest you to change this file. Instead, we will create a new theme. To create a new our own theme, create a folder called 'template' under /WEB-INF/classes. Then create a folder with new theme name. In my case, I created a folder called 'pda' and copied all the template files from default xhtml folder to pda folder. Now, I am going to change the checkboxlist.ftl file located at pda folder.

We will look at the contents of this file for a moment.

<#include "/${parameters.templateDir}/${parameters.theme}/controlheader.ftl" />
<#include "/${parameters.templateDir}/simple/checkboxlist.ftl" />
<#include "/${parameters.templateDir}/xhtml/controlfooter.ftl" /><#nt/>

This is written in FreeMarker. I guess, now you know what we have to do is. You can see, this has included checkboxlist.ftl file from default simple theme. We have to edit that file. Again, I am going to overwrite the default simple theme comes with struts 2 for my new theme. I am creating a folder called "pda_simple" under the /WEB-INF/classes/template and copy the checkboxlist.ftl file from default simple theme folder to pda_simple folder. Before edit this checkboxlist.ftl file, we have to change the file path of checkboxlist.ftl file located at pda folder as fallows.

<#include "/${parameters.templateDir}/${parameters.theme}/controlheader.ftl" />
<#include "/${parameters.templateDir}/pda_simple/checkboxlist.ftl" />
<#include "/${parameters.templateDir}/xhtml/controlfooter.ftl" /><#nt/>

Now this refers checkboxlist.ftl file from our new simple theme which is pda_simple. To control the alignment of our checkboxlist component, I am going to use the cssStyle attribute of the checkboxlist component. Here is the my view code.

<s:form action="deleteUser" method="POST" theme="pda">
<s:checkboxlist list="users" name="users" listKey="userId"
listValue="userName" cssStyle="vertical">
<s:submit value="update"></s:submit>

From above code, I want your attention on theme attribute of form component and the cssStyle attribute of checkboxlist component. I have specified our new theme for current form and cssStyle is set to vertical to have a vertically formatted checkboxlist.
Now, we have to edit checkboxlist.ftl file located at /WEB-INF/classes/template/pda_simple as fallows.

<#assign itemCount = 0/>
<#if parameters.list?exists>
<@s.iterator value="parameters.list">
<#assign itemCount = itemCount + 1/>
<#if parameters.listKey?exists>
<#assign itemKey = stack.findValue(parameters.listKey)/>
<#assign itemKey = stack.findValue('top')/>
<#if parameters.listValue?exists>
<#assign itemValue = stack.findString(parameters.listValue)/>
<#assign itemValue = stack.findString('top')/>
<#assign itemKeyStr=itemKey.toString() />
<input type="checkbox" name="${}" value="${itemKeyStr?html}" id="${}-${itemCount}"<#rt/>
<#if tag.contains(parameters.nameValue, itemKey)>
<#if parameters.disabled?default(false)>
<#if parameters.title?exists>
<#include "/${parameters.templateDir}/simple/scripting-events.ftl" />
<#include "/${parameters.templateDir}/simple/common-attributes.ftl" />
<label for="${}-${itemCount}" class="checkboxLabel">${itemValue?html}</label>
<#if parameters.cssStyle?exists>
<#if "${parameters.cssStyle?html}" == "vertical">

Actually you don't need to worry about the whole code above, but what I have highlighted. At the very beginning, I mentioned my target is adding additional <br> tag at the end of each <label> tag. I have done. There may be better ways of achieving this. This is how, I got through this. Finally, I got the expected output as fallows.

The CHECKBOXLIST component - struts 2

This a vary usefull UI component comes with struts 2 which allows user
for multiple selection. If we use normal HTML checkbox component, we have to write a custom function to get the selected values from a list of check boxes. Struts 2 provides a convenience way of fetching selected from a list of check boxes. Struts 2 ValueStack will resolve an array of string for the selected check boxes.

I will further demonstrate this with a simple example. Suppose I want to create a check box list which allows user to select several users from the list. This is my User class.

public class User{
private int userId,
privte Stirng userName;

// -------

Then I should create the HTML markup for the component.

<s:form action="deleteUser" method="POST" >
<s:checkboxlist list="users" name="users" listKey="userId"
<s:submit value="update"></s:submit>

Here, the value of list attribute "users" should be prepopulated with an action class, and then that action class should result into this page. Don't be confuse, I have use same for the value of the name attribute. You can use any meaningful one for this. Then you have to write the action class to fetch the checked list as fallows.

package com.axiohelix.pda.actions;

import com.opensymphony.xwork2.ActionSupport;

public class DeleteUser extends ActionSupport{
private String[] users;

public String[] getUsers() {
return users;
public void setUsers(String[] users) {
this.users = users;
public String execute(){
String[] userId = this.getUsers();
// --------------------------
return SUCCESS;

Sunday, November 9, 2008

The SELECT component - struts 2

The select component is perhaps the most common collection-based
UI component comes with struts 2. In a java web application, it's common to build these list of options from a Collection, Map or Arrays of data. The fallowing snippet shows the simplest use case of the select UI component.

<s:select name="userType" list="{'Administrator','Power User','Guest'}" />

Here, we have supplied an OGNL list literal as the list value of the select box. This render the fallowing HTML markup into the page.

<select name="userType" id="registerUserForm_userType">
<option value="Administrator">Administrator</option>
<option value="Power User">Power User</option>
<option value="Guest">Guest</option>

Here, "registerUserForm" is the my action name submitted with the form. But, most of the time, we may need to create select UI component with more advance Collection data. In this case, it may be list of UserType objects. Suppose, we have a class as fallows.

public class UserType{
private int userTypeId,
private String userTypeName,
// .....

Now, we want to create the select UI component from a List of UserType objects. In this case, we need to have userTypeId as the value and userTypeName as option contents (what user can see from select box). Here, the userTypeId should be a key attribute in the matching database table.

<s:select list="userType" key="userType" listKey="userTypeId"

userType is the list of UserType objects which is already exposed into the ValueStack through an action class. listKey and listValue are important tow attributes, when it comes to this kind advance data types.

  • listKey - The property of the List’s elements to be used for the value submitted when those elements are complex types; key by default.
  • listValue - The property of the List’s elements to be used for the content of the option when those elements are complex types in other words, the string seen by the user; value by default.

Here is the rendered HTML from the above UI components.

<select id="registerUserForm_userType" name="userType">
<option value="1">administrator</option>
<option value="2">power</option>
<option value="3">guest</option>

Saturday, November 8, 2008

Using global messages - struts 2

Putting text resources in properties files is a common web
application development practice enhancing proper management of texts,easy modifications and for internationalization. When it comes to struts 2, it further helps to keep our code clean and reduce the development time. To use global messages with struts 2, we need to create "" and place it on the class path at WEB-INF/classes. All the messages can be put in this file as key- value pairs.

userName=User Name
In order to use this file, we have to tell struts 2 where to find this file. As usual , we need to add one entry into the "" file residing in the class path.


Here, how we use these messages in our view pages. You have to use the "key" attribute of UI components to specify the key from the global-messages.

<s:textfield key="userName"/>
<s:password key="password"/>

Key attribute does infer the name and value for the text filed with message key. Here, proper naming convention is important to have this kind of service from the struts 2 framework.

Sunday, November 2, 2008

Changing the "theme" of UI components - struts 2

The struts 2 UI components come with four themes for rendering HTML markup into your view page.

  • simple
  • xhtml
  • css_xhtml
  • ajax
The default theme coming with struts 2 is xhtml which is involving html table layout to render the form component in the view page. For example consider the fallowing UI components.

<s:form action="doLogin.action" method="POST">
<s:textfield name="userName" label="User name" />

This UI components will render the fallowing HTML markup into your view page base on the default theme of the struts 2. You may confuse, if you haven't heard about struts 2 themes.

<form method="post" action="doLogin.action" onsubmit="return true;" id="doLogin">
<table class="wwFormTable">
<td class="tdLabel">
<label class="label" for="doLogin_userName">User name:</label>
<input type="text" id="doLogin_userName" value="" name="userName"/>
This HTML markup was rendered into view page on xhtml theme of struts 2. You may probably want to change the default theme. You can change this by overriding the default property which is defined in, found in struts 2 core jar file at org.apache.struts2.

To override any of the framework’s default properties, you only need to create your own properties file, named, and place it in the classpath. To change the default theme, just create a file with the following property definition:

In addition to these four themes that comes with struts 2, you can create your own them too.

Saturday, November 1, 2008

Filtering and Projecting with OGNL - struts 2

This is important OGNL feature when we are working with collections
to generate the UI components with Struts 2 tag libraries. To explain this, I will consider a class called "
Employee". Suppose we have a list of employee objects from the "ValueStack".

public class Employee{

private int age;

private String firstName;

private String lastName;



Now, you want to filter out the employees where the age is grater than 30. The OGNL filtering will resolve the problem. You can get this as fallows .

<s:set name="empList" value="employeeList.{?#this.age > 30}"></s:set>
<s:iterator value="#empList">
<s:property value="firstName"/>

Here, #this refers to the "employeeList" which is resolved in "ValueStack". That is it, about filtering. Now I will explain, how we use the projection. Think, you want to have a separate list with employee's full name. The OGNL projection feature will lead us to achieve it .

<s:set name="empNameList"
value="employeeList.{firstName + ' ' + lastName}"></s:set>
<s:iterator value="#empNameList" status="stat">
<s:property value="#empNameList[#stat.getIndex()]"/>

Tuesday, October 28, 2008

OGNL - Calling Static Methods and Fields - struts 2

Most of the time, we have used to declare system constants as static fields
of a class, and some time static methods are being involved. If we want to access this kind of field from a JSP page with struts 2 taglibs, we have tow methods. I have tow classes 'SystemConstants' and 'Utils' in my own application.

public class
// ............
public static final
SESSION_DATA = "sessionData";
// .........

Also I have a separate class called "SessionData" to keep the data needed for the entire session. Useful information needed to keep through out the session are stored in the "

public class

private User user ;

public setUser(User user){
this.user = user;

public User getUser(){
return this.user;

I have stored the SessionData object in the session object from an "Action" class of struts 2.
SessionData sd = new SessionData();

Now, I want to access the session object and the "
SESSION_DATA" static field from the jsp page. This is where the point comes. For this, we have tow methods.

Method one:
To access the static field ,
<s:set name="sdKey"

Then to access the session object
from the "ActionContext",
<s:set name="objSd" value="#session[#sdKey]"/>

Then we can access the fields and methods declared in the SessionData object.
<s:property value="#objSd.user.userName"/>

This is how, we access static methods

If our field or method will resole in the 'ValueStack' in advance, we can use the fallowing syntax to access relevants
Method tow:

The SESSION_DATA static field is declared in the class 'SystemConstants' packaged at
'com.axiohelix.utils' and getSessionId() function is implemented within the Utils class packaged at 'com.axiohelix.utils'

Monday, October 27, 2008

Forcing OGNL - struts 2

I wanted to develop a dynamic menu with some set of links by retrieving appropriate .action names from a database table. It was a tricky point for me to set the 'action' attribute of the <s:url> tag. The property name that I wanted to set to 'action' attribute was 'actionName' which is already in the 'ValueStack'. Because, 'actionName' is already in the 'ValueStack', first I tried in this manner

<s:url action="actionName" id="myUrl">
    <s:param name="userId" value="1001"></s:param>

<a href="'<s:property  value="#myUrl">'">
    Click me

This was completely wrong syntax. Because here, 'actionName' is evaluated as a String itself, it does not retrieve the 'actionName' from the 'ValueStack'. Simply It generates the fallowing HTML mark up, which was not expecting.

<a href="/pda/acionName.action?userId=1001">
   Click me

To get the value for 'actionName' from the 'ValueStack', we need to force OGNL as fallows. We need to use %{expresion} notation to force OGNL to get the property of 'actionName' from the 'ValueStack' as a String literal.

<s:url action="'%{actionName}''" id="myUrl">
     <s:param name="userId" value="1001"></s:param>

<a href="'<s:property" value="#myUrl"> Click me </a>

This created the expected HTML mark up as fallows.

<a href="/pda/updateUser.action?userId=1001">
   Click me

* pda - is my context path

* updateUser - is one of my action name