"Hi, my name is Rhama. I love to write, which implies my love to reading. I'm kinda obsessed with Movies, works as an IT guy at day, and developing Android app and iOS app otherwise. Oh, yeah, I also kinda wished to escape the cubicle farm."

Blog Archives

Struts 2 Select UI Tag Problem

Today, I’ve got a problem that manages to puzzled me for a good part of two hours with Struts 2 select UI tag behavior.

This is the code responsible for the said puzzlement.

<s:select name="employee.division" list="allDivisions" listKey="id" listValue="name"
headerKey="-1" headerValue="-- Select Division --"/>

Any seasoned Struts 2 programmers could tell by a glance what the codes depicted above did. But in case you’re wondering, the above code will look into my application’s ValueStack for a Collection or iterable property named allDivisions, and then populate the resulted select HTML tag with values taken from this collection, with attribute “id” as its key and attribute “name” as its value. The select component also has a header key-value pair of “-1″ and “– Select Division –” which appears at the top of the drop down select component.

Now, even if I hadn’t touched Struts 2 or Java code for that matter, for months, I’ve had an experience in developing a couple of Struts 2 applications in the past, and I know for a fact that the above snippet is correct and will be executed properly given that there’s indeed an iterable value in the ValueStack that bears the name “allDivisions.”

But something is clearly wrong or there won’t be any necessity that sparked this article in the first place. Anyway, the select list isn’t properly populated and I had a terrible morning trying to get that to work right. The solution?

This is (part of) my not-working action class, by the way.

private List<Division> allDivisions;

public String execute() {
	this.setAllDivisions(genericEntity.getAllDivision());
	return SUCCESS;
}

public void setAllDivisions(List<Division> divisions) {
	this.allDivisions = divisions;
}

public List<Division> getAllDivisions() {
	return this.allDivisions;
}

Again, any seasoned Struts 2 programmers should notice that the method execute() and String “SUCCESS” indicated above are no strangers as they are the default method when an action that has been mapped with this class was invoked.

In addition to this default method, the snippet above also includes a property, defined as List, therefore iterable, and its Java Bean getter and setter methods. What I had meant to do with the snippet above is, upon action invocation, I would retrieve all Division data from the database (through getAllDivision() method), and populate allDivisions property with the resulted List through its setter method. Presumably, this property would be then available when retrieved in the JSP file.

The resulted select however, although giving the correct number of items, each of them displays the content as empty strings.

First of all, I had assumed that something was amiss during the query. The nature of Struts 2 actions, is a POJO (Plain Old Java Object), giving it ability of easy offline testing and that’s what I do. I created an offline, command-line based test unit, and see if the action class is giving the expected result. It does. Therefore, the problem, I presumed, lies in the JSP file. But after fiddling with the JSP file for a good part of hour, I was clearly wrong. Finally, this is what do to make it work like a charm. I changed my action class a bit to look like this.

public EmployeeAction() {
	this.setAllDivision(genericEntity.getAllDivision());
}

public String execute() {
	return SUCCESS;
}

As apparent above, I merely took the query line from execute method elsewhere (in my case, I put it in the constructor). My reasoning is that by doing so, I made sure that the allDivisions property is available, up and running, before the execute method invoked while in the previous example that behaves erratically, the availability of allDivisions property wasn’t guaranteed for it was populated relatively at the same time with the execute method invocation.