Friday June 22, 2007
Dynamic Data in jMaki Widgets Using JPA
![]() |
jMaki provides a rich set of data widgets that can be embedded in a web application. For most of the widgets to be useful, they need to be tied a database backend. For example consider a Table widget displaying data about your favorite stock tickers. This blog explains the steps to create such a Web application, deployed on GlassFish V2, that contains a jMaki-wrapped Yahoo Data Table widget pulling data from JavaDB. |
If you are using a jMaki build higher than 0.96 (or dated after jul 30) then some steps in this entry need to be updated and described here. These steps are marked with "SEE THE UPDATED ENTRY".
Web Application' project and
name it as 'jmaki-jpa'.
jMaki Ajax Framework' by clicking on 'Next' button while
creating the project.Standard' layout as shown below:
Finish'.Runtime' tab, expand Databases, connect to the
default database (with the URL 'jdbc:derby://localhost:1527/sample [app
on APP]'). Specify the username 'app' and password 'app'.Execute
Command...' and enter the following query to create the table definition:create table COMPANY (id int,
companyName varchar(255),
price float,
change float,
percentChange float,
lastUpdated varchar(50),
PRIMARY KEY (id))Refresh' to see the
newly created table in the Tables tree. Select the 'COMPANY' table,
right-click and select 'Execute Command...' and enter:insert into COMPANY values (1, 'A Co', 71.72, 0.02, 0.03, 'Jan 1, 2007,
10:00am' );
insert into COMPANY values (2, 'B Inc', 29.01, 0.42, 1.47, 'Feb 1, 2007,
10:00am' );
insert into COMPANY values (3, 'C Group Inc', 83.81, 0.28, 0.34, 'Mar 1,
2007, 10:00am' );
insert into COMPANY values (4, 'D Company', 52.55, 0.01, 0.02, 'Apr 1,
2007, 10:00am' );
jmaki-jpa', right-click
and select 'New' and choose 'Entity Classes From Database...'.jdbc/sample' as 'Data Source'.COMPANY' in 'Available Tables' and click on 'Add' and enter
the values as shown below:
Next'.server' as shown below:
Create Persistence Unit...' to create the persistence unit
and enter the values as shown below:
Create'.and click on 'Finish'.
Configuration Files' and open 'persistence.xml'.Add Class' button and click on 'Cancel'
button. For some reason the entity classes are not loaded during the first
time.Add Class' button and choose 'server.Company'
class and click 'OK'. This will ensure that the generated
entity class is explicitly recognized by the EntityManagerFactory.Web Pages', select 'New' and then 'JSP...'.
Give the name as 'data' as shown:
Finish'.data.jsp' with the following:<%@ page import="java.util.*" %>
<%@ page import="server.Company" %>
<%@ page import="javax.persistence.*" %>
<%
EntityManagerFactory emf = Persistence.createEntityManagerFactory("jmaki-jpaPU");
EntityManager em = emf.createEntityManager();
List<Company> list = em.createQuery("select c from Company
c").getResultList();
out.println("[");
for (int i=0; i<list.size(); i++) {
Company c = list.get(i);
out.println("['" + c.getCompanyname()
+ "'," +
c.getPrice()
+ "," + c.getChange() + "," +
c.getPercentchange() + ",'" + c.getLastupdated() +
"']");
if (i < list.size()-1)
out.println(",");
}
out.println("]");
%>index.jsp', drag-and-drop a 'Yahoo Data Table'
widget from the jMaki Palette in the 'Main Content Area'.<a:widget name="yahoo.dataTable" args="{
columns :[
{title : 'Company', width : 200, locked:false},
{title : 'Price', width : 75, renderer: 'usMoney'},
{title : 'Change', width : 75, renderer: 'change'},
{title : '% Change', width : 75, renderer: 'pctChange'},
{title : 'Last Updated', width : 85, renderer: 'italic'}
]}"
value="[
['A Co',71.72,0.02,0.03,'9/1 12:00am'],
['B Inc',29.01,0.42,1.47,'9/1 12:00am'],
['C Group Inc',83.81,0.28,0.34,'9/1 12:00am'],
['D Company',52.55,0.01,0.02,'9/1 12:00am']
]" /><a:widget name="yahoo.dataTable" args="{
columns :[
{title : 'Company', width : 200, locked:false},
{title : 'Price', width : 75, renderer: 'usMoney'},
{title : 'Change', width : 75, renderer: 'change'},
{title : '% Change', width : 75, renderer: 'pctChange'},
{title : 'Last Updated', width : 85, renderer: 'italic'}
]}"
service="data.jsp" />
data.jsp' instead
of the static data.
UPDATED: SEE THE UPDATED ENTRY - Based upon a user request, a NetBeans project for this sample can be opened via Java WebStart here. Alternatively, you can download the project and view at your own ease. Thanks to Geertjan for the tip!
Technorati: jmaki glassfish jpa netbeans
Posted by Arun Gupta in web2.0 | Comments[33]
|
|
|
|
| Sun Keynote @ TheServerSide Java Symposium
![]() |
Eduardo will be giving a keynote (From Tomcat to GlassFish - An Update on OpenSource Server-side Java at Sun) at TheServerSide Java Symposium - Europe next week. |
He is the Editor-in-Chief for TheAquarium so go talk to him and tell him how you like GlassFish or would like us to improve.
Technorati: theserverside glassfish opensource conf
Posted by Arun Gupta in webservices | Comments[0]
|
|
|
|
|
Today's Page Hits: 3248
Total # blog entries: 994