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");

Tuesday, April 22, 2008

How to Format Dates in Ruby & Rails

I have seen several techniques used to format dates in blogs and forums threads. This is the technique that worked for me.
  1. Create a file called config/initializers/date_formats.rb with the following contents:
    ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!(
     :date => '%m/%d/%Y',
     :date_time12  => "%m/%d/%Y %I:%M%p",
     :date_time24  => "%m/%d/%Y %H:%M"
    )
  2. Set the date variable:
    @cacheExpiresAt = 10.minutes.ago
  3. Format the date:
    <%= @cacheExpiresAt.to_s(:date_time12) %>
The :date_time12 parameter to the to_s method simply chooses the formatting from the DATE_FORMATS hash. This technique lets you centralize all date formatting in your application.

Friday, April 18, 2008

Workaround For Minor Hibernate Bug - Blank hibernate.default_schema Property Adds Period

We are using the hibernate.default_schema property with Oracle in production. Today I tried to use HSQL for our integration tests. When I blanked the hibernate.default_schema property, Hibernate tried to create tables named like '.FOO' - notice that pesky leading period! You can workaround this issue for HSQL by setting hibernate.default_schema to 'sa'.

Wednesday, April 09, 2008

ActiveRecord Without Rails, Refined

About a year ago Aizat Faiz, a software developer in Malaysia wrote a blog entry about using ActiveRecord outside of the Rails framework. This is definitely something that I want to do for a variety of reasons, the simplest being that I'd like to write utility programs that access the database without needing to also write a user interface for them.

In any case, Aizat wrote a nice understandable entry. However, he seemed to make the assumption that developers using ActiveRecord without Rails would not have a Rails application at all. This is not the case for me.

If your utility script resides in the top level directory, then you can use your already existing database.yml file instead of creating a new one. Just use the following as a guide.

dbconfig = YAML::load(File.open('config/database.yml'))
ActiveRecord::Base.establish_connection(dbconfig['development'])

Also, there is no reason to have your models in your script when they are already in your Rails application. You can load existing modesl like this:

require 'app/models/event.rb'
require 'app/models/senator.rb'

That's it. Very simple and no duplication of code.

Friday, April 04, 2008

How To Add a Datasource To Tomcat Server.XML File.

... ... factory oracle.jdbc.pool.OracleDataSourceFactory validationQuery select 1 from dual url jdbc:oracle:thin:USERNAME/PASSWORD@localhost:1521:XE maxActive 4 maxWait 5000 driverClassName oracle.jdbc.OracleDriver maxIdle 2 ... ..