Tuesday Feb 27, 2007
Tuesday Feb 27, 2007
We have yet another new tutorial, this one titled Building a Tree From Database Data. This tutorial shows how to build a two-page application, the first page of which includes a Tree component. You populate the first-level nodes in the Tree with names from a database, and the second-level nodes with the trips for that person. The trip nodes are links to a second page, which displays the details for that trip.
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
Wednesday Sep 06, 2006
Note: See Using jMaki 9.3 With the NetBeans Visual Web Pack or the Sun Java Studio Creator IDE for updated instructions on how to add jMaki to a project.
Jennifer Ball just published two new jMaki tutorials, which you can read about in her blog. One of the tutorials shows how to use jMaki in JavaServer Faces projects, and it completes the information that I needed to figure out how to integrate a jMaki widget in a page built using the Java Studio Creator IDE. In particular, how to bind to a bean property, how to save off the widget's values, and how to make managed bean methods available for the AJAX requests.
So, here is a mini-tutorial on how to use jMaki to add a dojo AJAX combo box to a JavaServer Faces web page.
jar xvf jMaki.war
command, or you can rename the file to have a .zip extension and unzip it.jmaki/resources/jmaki.js to creator-project-dir/web/resources.
jmaki/resources/dojo/combobox to creator-project-dir/web/resources/dojo/combobox.
/resources/libs/dojo directory
(not the whole dojo directory) to creator-project-dir/web/resources/libs/dojo/version.3.1.
jmaki/WEB-INF/lib/ajax-wrapper-comp.jar to creator-project-dir/web/WEB-INF/lib.
ajax-wraper-comp.jar
to the IDE's library manager. Then, you can easily add the jMaki library to
a project by right-clicking the Libraries node in the Projects window and
choosing Add Library. xmlns:a="http://java.sun.com/jmaki-jsf" to the top-level
<div> tag. a:ajax tag inside an f:verbatim
tag, as shown below. Note that the service attribute is referring to
ApplicationBean1. Later, you add the completeCountry() method
to the application bean to return the list of choices.value="#{ApplicationBean1.completeCountry}".
Once I get it figured out, I will post a new entry.
<?xml version="1.0" encoding="UTF-8"?>
<jsp:root version="1.2" xmlns:a="http://java.sun.com/jmaki-jsf"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:jsp="http://java.sun.com/JSP/Page"
xmlns:ui="http://www.sun.com/web/ui">
<jsp:directive.page contentType="text/html;charset=UTF-8"
pageEncoding="UTF-8"/>
<f:view>
<ui:page binding="#{Page1.page1}" id="page1">
<ui:html binding="#{Page1.html1}" id="html1">
<ui:head binding="#{Page1.head1}" id="head1">
<ui:link binding="#{Page1.link1}" id="link1"
url="/resources/stylesheet.css"/>
</ui:head>
<ui:body binding="#{Page1.body1}" id="body1"
style="-rave-layout: grid">
<ui:form binding="#{Page1.form1}" id="form1">
<ui:panelGroup binding="#{Page1.groupPanel1}"
id="groupPanel1"
style="position: absolute; left: 24px; top: 48px">
<f:verbatim>
<a:ajax id="cb1" name="dojo.combobox"
service="ApplicationBean1-completeCountry.ajax" />
</f:verbatim>
</ui:panelGroup>
</ui:form>
</ui:body>
</ui:html>
</ui:page>
</f:view>
</jsp:root>
|
AbstractApplicationBean and use
the Managed Beans node in the Projects window to make the class' properties
available in application scope.
private String[] countries =new String[] {
"Canada", "France", "Uganda", "Ukraine", "United States of America",
"United Kingdom", "Japan", "Korea", "Jamacia", "Thailand"
};
private String[] countryCodes =
new String[] {"CA", "FR", "UG", "UR", "USA", "UK", "JP",
"KR", "JA", "TH"
};
public void completeCountry(FacesContext context,
String[] args,AjaxResult result) {
result.setResponseType(AjaxResult.JSON);
result.append("[");
for (int loop=0; loop < countries.length; loop++){
result.append("[\"" + countries[loop] + "\",\""
+ countryCodes[loop] + "\" ]");
if (loop < countries.length -1) result.append(",");}
result.append("]");
}
|
private String country = "";
public String getCountry() {
return country;
}
public void setCountry(String country){
this.country = country;
}
|
value="#{SessionBean1.country}"
attribute to the ajax tag, as shown below.
<a:ajax id="cb1" name="dojo.combobox"
service="ApplicationBean1-completeCountry.ajax"
value="#{SessionBean1.country}"/>
|
onSubmit property
to jmaki.attributes.get('form1:cb1').saveState();.Go.Back.
Click the ... button for the component's url property, choose
Page1, and click OK.value attribute is bound
to the session bean's country property, the previously selected value is displayed.
To understand how the jMaki tags work, I highly recommend that you read both of Jennifer's tutorials.