July 31, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Who Says EJB 2.1 is Too Hard?

  • October 11, 2005
  • By Dick Wall
  • Send Email »
  • More Articles »

Create a Demo Client in Java Studio Creator

Almost there now. Throw a quick project together and a couple of pages to demonstrate the new easy binding features for EJBs in Creator. You will demonstrate it using Data Tables, but the same EJB methods can be bound to other controls using the same tricks. For more information on general binding in Java Studio Creator, see the tutorials at http://developers.sun.com/prodtech/javatools/jscreator/ea/jsc2/learning/tutorials/index.html.

First, get a simple project and page to list all the people:

  1. At the Creator welcome page, click Create New Project.
  2. Select Web and JSF Web Application from the Choose Project dialog. Click Next.
  3. Choose a name (like PeopleDemo) and a location for the project. Click Finish.
  4. In the resulting Page1 file that comes up, drag a Table control from the Palette on the left onto the page.
  5. Rename the "Table" label to read People.
  6. Now, for the cool part. Go to the Servers tab in the top left pane and find the listAll method from the CreatorDemoEJB (if you have not expanded it already, expand the CreatorDemoEJB and the ejb/PeopleEJBBean under that until you see the methods).
  7. Drag the listAll method onto the data table you just created. This binds the results of that method call to the table; you will see the fields change in response to the PersonDTO class fields.
  8. Note: If you get a dialog asking you to select a target, make sure you select the whole table, probably called table 1).
  9. Pretty impressive, but the columns are probably in the wrong order, plus the peopleList static property shows up (which you don't want), and you also can give it some clues about the representation of the data you want:
    1. Right-click on the table and select Table Layout.
    2. Select peopleList and click the < button to remove it.
    3. Select firstName and click the Up button until it is at the top.
    4. Do the same for lastName.
    5. You might want to arrange the homeTel and workTel numbers together as well.
    6. Feel free to select each column definition in turn and give it a better Header Text entry (which will be displayed in the column header).
    7. Also, change the onVacation field to be represented as a checkbox: select onVacation in the columns and then change the Component Type to Checkbox.
    8. Click OK. You should see the changes reflected in the data table.

Now, run the project (Run->Run Main Project from the menu). You should see a page like this:



Click here for a larger image.

If you have not yet played with the new Java Studio Creator 2 controls, go ahead and click on the column headings; this allows you to sort the data displayed in the table by the different columns (and, by using the + symbols that appear after you first select a sort order, you can create a sort hierarchy as well).

A More Involved Example

So, you just bound the results from a simple EJB list method directly into a data table. Very neat, but what if you want to be a little more selective about what you show?

If you remember, you created a method called listIncomeAbove that took a value to filter the details returned by. Create a page that uses that.

  1. In the Projects tab in the bottom right pane, right-click on the Web Pages node and select New->Page....
  2. For the File Name, type ListIncomeAbove. Click Finish.
  3. In the Projects tab again, find the new page, right-click on it and select Set As Start Page (this will make testing it easier).
  4. This time, drag a Static Text control, Text Field control, and Data Table onto the page so it looks like this:
  5. You should rename the ID of at least the text field to be something like amountInput. Renaming your controls to a meaningful name at least for all of the input ones is a good idea because it makes your page code easier to read. Select the control, click properties, and change the top field in the properties pane called ID to read amountInput.
  6. Find the listIncomeAbove EJB method and drag it over the data table.
  7. Follow Step 8 from the List All example above to change the layout of the table and make it look like the List All page.
  8. Now, right-click on the amountInput text field and select Auto Submit on Change, so that when you enter a value here, it will fire a change event in the page bean.
  9. Double-click on the inputAmount text field and Creator will drop you into editing the event for that text field.
  10. Where it says //TODO: Replace with your code, replace the line with the following:
  11. double amount =
       Double.parseDouble((String)this.amountInput.getValue());
    this.peopleEJBRemoteListIncomeAbove1.setAmount(amount);
    this.peopleEJBRemoteListIncomeAbove1.refresh();
    
  12. When you dragged the listIncomeAbove method onto the data table, Java Studio Creator made this peopleEJBRemoteListIncomeAbove1 reference for you, which you can now use to tell it the amount you want to filter by, and then refresh the object (that will update the data in the table). It's that easy.
  13. Run it to see the results. At first, you will see a list of all of the people in the system (the income amount filter is 0 by default), but if you type in 100000 to the income above field and click return, it will filter down to two names with income above 100000.

Conclusion

Well, once again you barely scratched the surface of what is possible, but if you think about the ground covered in this article, it is quite impressive. You have put together an EJB API, deployed it to the App Server Instance in Java Studio Creator, consumed it in Creator, and then bound it to a control in a JSF page.

More to the point, you have done this without seeing a deployment descriptor, Home and Remote interface classes, JNDI lookup code, or anything else that might be considered tricky or confusing. The new EJB features in Java Studio Creator 2 truly do extend the component model and ease of use data bindings into the world of full EJB 2.1 architecture.

Many people are eagerly awaiting the release of EJB 3.0 to help simplify EJB development, but with suitable use of the right IDE tools, simple development using EJBs is available right now, I also look forward to EJB 3.0, but it's hard to imagine how much easier it will make things for the demo we have seen.

Download the source code for this article here and here.

About the Author

Dick Wall is a Lead Systems Engineer for NewEnergy Associates, A Siemens Company based in Atlanta, GA that provides energy IT and consulting solutions for decision support and energy operations. He can be reached for comment on this and other matters at dick.wall@newenergyassoc.com. He also co-hosts the Java Posse with Tor Norbye and Carl Quinn, a podcast devoted to Java News and Interviews, which can be found at http://javaposse.com.





Page 3 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel