Sunday Dec 17, 2006
Sunday Dec 17, 2006
A forum poster recently asked for the following information:
I have a table with 5 columns, (week_no, objective, date1, date2 and add report). How do I get the date for date1( example, today's date)? How do I set the week number as auto increment by 1( example, row 1, the week_no is 1, row 2, the week_no is 2 and so on)? Lastly, how to I set the add report button to link to another JavaServer Pages file?
This scenario gives me an excellent opportunity to show how to use an Object Array Data Provider (OADP). I usually use the Object List Data Provider (OLDP), but OLDPs take a bit more work. (See the Using Hibernate tutorial for an OLDP example.) If you have a simple array of data, the OADP might be the better choice, as in this case. The following steps create a table similar to the one requested in the forum.
/*
* WeekBean.java
*
*/
package arraytableexample;
public class WeekBean
extends Object implements Serializable {
private int weekNumber;
private Calendar startDate;
private Calendar endDate;
private int dayOfWeek;
public WeekBean(int weekNumber) {
this.weekNumber = weekNumber;
Calendar workingDate = Calendar.getInstance();
workingDate.set(java.util.Calendar.HOUR_OF_DAY, 0);
int offset = weekNumber - 1;
workingDate.add(
Calendar.DAY_OF_YEAR,
+ (offset * 7));
dayOfWeek = workingDate.get(Calendar.DAY_OF_WEEK);
startDate = (Calendar) workingDate.clone();
startDate.add(
Calendar.DAY_OF_YEAR, - (dayOfWeek - Calendar.SUNDAY));
endDate = (Calendar) workingDate.clone();
endDate.add(
Calendar.DAY_OF_YEAR,
+ (Calendar.SATURDAY - dayOfWeek));
}
public WeekBean() {
this(1);
}
public int getWeekNumber() {
return this.weekNumber;
}
public void setWeekNumber(int weekNumber) {
this.weekNumber = weekNumber;
}
public Calendar getStartDate() {
return this.startDate;
}
public void setStartDate(Calendar startDate) {
this.startDate = startDate;
}
public Calendar getEndDate() {
return endDate;
}
public void setEndDate(Calendar endDate) {
this.endDate = endDate;
}
}
/**
* Holds value of property weeks.
*/
WeekBean[] weeks = {
new WeekBean(1),
new WeekBean(2),
new WeekBean(3),
new WeekBean(4)
};
/**
* Getter for property weeks.
* @return Value of property weeks.
*/
public WeekBean[] getWeeks() {
return this.weeks;
}
/**
* Holds value of property reportStartDate.
*/
private Calendar reportStartDate;
/**
* Getter for property reportStartDate.
* @return Value of property reportStartDate.
*/
public Calendar getReportStartDate() {
return this.reportStartDate;
}
/**
* Setter for property reportStartDate.
* @param reportStartDate New value of property reportStartDate.
*/
public void setReportStartDate(Calendar reportStartDate) {
this.reportStartDate = reportStartDate;
}
/**
* Holds value of property reportEndDate.
*/
private Calendar reportEndDate;
/**
* Getter for property reportEndDate.
* @return Value of property reportEndDate.
*/
public Calendar getReportEndDate() {
return this.reportEndDate;
}
/**
* Setter for property reportEndDate.
* @param reportEndDate New value of property reportEndDate.
*/
public void setReportEndDate(Calendar reportEndDate) {
this.reportEndDate = reportEndDate;
}
weekNumber startDate endDate
public String button1_action() {
getSessionBean1().setReportStartDate(
(Calendar)getValue("#{currentRow.value['startDate']}"));
getSessionBean1().setReportEndDate(
(Calendar)getValue("#{currentRow.value['endDate']}"));
return null;
}
Winston has many blog entries about the dataprovider. A good place to start is his blog titled What is this Data Provider in Sun Java Studio Creator anyway?. Another good resource is Joel Brown's Weblog.
jsf netbeans creator dataprovider
Posted by Ariel on March 06, 2007 at 10:17 AM PST #
Posted by diva#1 on March 06, 2007 at 03:04 PM PST #
I follow the steps above. But the problem is there's nothing appear in array drop-down list. Why's that? and how can i solve the problem?
Posted by wowerng on January 01, 2008 at 09:07 PM PST #
wowerng,
Are you using Sun Java Studio Creator IDE or NetBeans IDE. If you are using the NetBeans IDE, what is the version number.
Try doing a clean and build, closing the project, restarting the IDE and opening the project.
Posted by Diva #2 on January 03, 2008 at 01:12 PM PST #
I'm using NetBeans 5.5.1 . I followed your suggestion and it does work! thank you.
Posted by wowerng on January 03, 2008 at 06:32 PM PST #