Wednesday, April 30, 2008

Struts Error/Exception: No getter method available for property name for bean under name

Recently I was using Struts v1.x and I needed to display a select list. The information about how to use the html:optionsCollection tag was sketchy. I was able to get the tag working this way:

On the JSP page, I added this inside the html:form tag:

<html:select property="selectedEquipment">
  <html:optionsCollection property="equipment" label="name" value="id"/>
</html:select>

Then I create a Java class called SelectOption like this:

package com.codebits.struts;

public class SelectOption {

    String id;
    String name;

    SelectOption() {
    }

    SelectOption(final String _id, final String _name) {
        setId(_id);
        setName(_name);
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Next I created an ActionForm bean:

package com.codebits.struts;

public class AddForm extends ActionForm {

    private Integer           selectedEquipment = null;

    private List              equipment         = new ArrayList();
    
    public Integer getSelectedEquipment() {
        return selectedEquipment;
    }
    
    public void setSelectedEquipment(Integer selectedEquipment) {
        this.selectedEquipment = selectedEquipment;
    }
    
    public List getEquipment() {
        return equipment;
    }
    
    public void addEquipment(final String equipmentId, final String emtEquipmentId) {
        this.equipment.add(new SelectOption(equipmentId, emtEquipmentId));
    }
    
    public void setEquipment(List equipment) {
        this.equipment = equipment;
    }

}

And finally, in my action class, I prepopulated the ActionForm bean:

  AddForm addForm = (AddForm)form;
  addForm.addEquipment("1", "AAA");
  addForm.addEquipment("2", "BBBB");
Post a Comment