Create List Of Dates With DynaBean

27 09 2007

Create list of dates (last 6 months) to populate listbox in .jsp.

// get search date listbox
DynaBean dateBean = new LazyDynaBean();
List<DynaBean> dateList = new ArrayList<DynaBean>();
SimpleDateFormat formatter = new SimpleDateFormat("MMM yyyy");
// display 6 months starting from current month
Calendar cal = Calendar.getInstance();
// get current date
cal.setTime(new Date());
for(int i=0; i<5; i++) {
dateBean = new LazyDynaBean();
dateBean.set("property", formatter.format(cal.getTime()));
dateBean.set("value", cal.getTime());
// add to list
dateList.add (dateBean);
// subtract 1 month
cal.add(Calendar.MONTH, -1);
// send to form bean (type="java.util.List")
PropertyUtils.setUtils(form, "datelist", dateList);

in .jsp

<bean:define id="dates" name="MyFormName" property="datelist"/>
<html:select property="s_valueDate" value='<%= request.getParameter("s_valueDate") %>'> <html:options collection="dates" property="value" labelProperty="property"/>




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: