Netbeans + Visual Paradigm = EJB Tutorial (Part 2)

In part 1 of this tutorial we walk through the modeling and code creation of an EJB using Netbeans and Visual Paradigm. In part 2 we will create a simple sample ZK web application that reads data using the EJB.

Prerequisites:

  • The project from completed tutorial part 1

Tutorial:

  • Create web application ‘demoejbweb’

    Create web application

    Create web application

  • Add the EJB from dem demoejb.jar
    Note: We have 2 independent projects now, if you rebuild the ejb, you also need to rebuild the web application.

    Add library

  • Add a listbox to the zul file
    <?xml version="1.0" encoding="UTF-8"?>
    
    <zk xmlns="http://www.zkoss.org/2005/zul">
        <window id="list" apply="controller.indexController" title="List">
    
            <listbox id="lstCustomer" width="100%" >
                <listhead sizable="true">
                    <listheader id="lblLast" label=""/>
                    <listheader id="lblFirst" label="" />
                    <listheader id="lblDate" label="" />
                </listhead>
            </listbox>
    
        </window>
    </zk>
    
  • Add the controller class

    Controller Class

    package controller;
    
    import demo.Customer_name;
    import facade.Customer_nameFacade;
    import java.util.List;
    import javax.naming.Context;
    import javax.naming.InitialContext;
    import org.zkoss.zk.ui.Component;
    import org.zkoss.zk.ui.util.GenericForwardComposer;
    import org.zkoss.zul.ListModelList;
    import org.zkoss.zul.Listbox;
    import org.zkoss.zul.Listcell;
    import org.zkoss.zul.Listitem;
    import org.zkoss.zul.ListitemRenderer;
    
    public class indexController extends GenericForwardComposer {
    
        private Listbox lstCustomer;
        Context context;
        Customer_nameFacade customer_nameFacade;
        List<Customer_name> customers = null;
    
        @Override
        public void doAfterCompose(Component comp) throws Exception {
            super.doAfterCompose(comp);
    
            setupRenderer();
    
            context = new InitialContext();
    
            try {
                customer_nameFacade = (Customer_nameFacade) context.lookup("java:global/demoejbweb/Customer_nameFacade");
                customers = customer_nameFacade.findAll();
                lstCustomer.setModel(new ListModelList(customers));
    
            } catch (Exception ex) {
                System.out.println(ex.getMessage());
            }
    
        }
    
        private void setupRenderer() {
            ListitemRenderer listitem = new ListitemRenderer() {
    
                @Override
                public void render(Listitem item, Object data) throws Exception {
                    item.setValue(data);
    
                    item.appendChild(new Listcell(((Customer_name) data).getLast_name()));
                    item.appendChild(new Listcell(((Customer_name) data).getFirst_name()));
                    item.appendChild(new Listcell(((Customer_name) data).getEntrydate().toString()));
    
                    Listcell listcell = new Listcell();
                    item.appendChild(listcell);
                }
            };
            lstCustomer.setItemRenderer(listitem);
        }
    }
    
    
  • Add some data using the “data explorer” in Netbeans
    Note: If you chosed the strategy ‘drop and create‘ for your persistence unit, the data will be lost on the next deployment. After the tables have been created change to ‘Nothing‘.

    Adding data

  • Run the web application

    Running web application

Remarks:

  • No, you don’t need to use the context to access the EJB, you could use the annotation approach, provided both Netbeans projects are part of one enterprise application.
  • In part 3 we will tinker with real databases, PostgreSQL and Oracle.
About these ads

2 thoughts on “Netbeans + Visual Paradigm = EJB Tutorial (Part 2)

  1. Pingback: Netbeans + Visual Paradigm = EJB Tutorial (Part 3) « The JavaDude Weblog

  2. Pingback: Netbeans + Visual Paradigm = EJB Tutorial (Part 4) « The JavaDude Weblog

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s