04/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");
04/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.
- 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" )
- Set the date variable:
@cacheExpiresAt = 10.minutes.ago
- Format the date:
<%= @cacheExpiresAt.to_s(:date_time12) %>
: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.