Thursday Apr 19, 2012

Basic Bean Building: How to Quickly Create a JSF Managed "Look-up" Bean

If you're called upon to build a JSF application (large or small), NetBeans is a great tool to help you gain traction, and fast. I've mentioned previously how much I like the ability to do a quick first cut by creating your data structures and using NetBeans to generate a basic web app as a starting point. You go from nothing to working web application in short order, albeit a bit bare-bones. Still, what a great time-saver!

Let me toss out a few reminders and disclaimers. First, I'm a firm believer in rapid, iterative, and thoughtful development. No "paralysis of analysis" allowed, but OTOH, each step should be considered carefully as you go. Plan, execute, evaluate. Lather, rinse, repeat.  :-)

Second, build from small to large. Plan ahead, but make things work on a small scale and build out. This goes hand-in-hand with point #1, but it bears mentioning separately.

With those points in mind, I created a small sample project for use in this and future discussions: The Mighty Bean Coffee Company. Java is a wonderful thing in almost any form, so why not double our pleasure?

For the first iteration of our new project, I created an initial set of tables in a new MySQL database. These are the ones we'll be considering on our quest to create a JSF managed "look-up" bean:

Next, I created a new Java Web project, Web Application in NetBeans called MightyBeansWeb. Defaults were fine for our purposes here with the exception of selecting JavaServer Faces under Frameworks.

With the project created, I right-moused on MightyBeansWeb in the tree browser, chose "New Entity Classes from Database", and made the connections to the data source. Selecting the two tables modeled above, NetBeans provided us a couple of nicely-formed entity classes: Customer.java and CoffeeOrder.java.

Creating the JSF pages was also a snap. Right-moused again on the app, chose "New JSF Pages from Entity Classes", selected the classes and specified destination packages, and clicked finish. Et voila! We now have a working JSF web app. Go ahead, try it out. I'll be right here when you get back.  :-)

(Side note: Please feel free to a) update the toString() methods for more meaningful contents - this will come in very handy later! - and b) to refer to our recent article NetBeans, JSF, and MySQL Primary Keys using AUTO_INCREMENT to enable primary key auto-generation within MySQL.)

"That's great!" you say. But wouldn't it be nice to see which customer we've selected for our order? That brings us to the topic of this article. Let's whip up a look-up bean and make that happen now.

Right-mouse once more on the web app and choose "New JSF Managed Bean". Pick a name (I called this one "CustomerLookupBean") and a package and click finish. No need to do anything else on that panel since we're using Java EE 6.

Opening the class, we see it has annotations for @ManagedBean and @RequestScoped which serve our needs nicely. We just need to add a couple of very small things to wrap up our bean-work.

Since EJBs are non-reentrant, we can simply inject an instance of EntityManager into our lookup bean by adding the following lines to the class definition:
    @PersistenceContext(unitName = "MightyBeansWebPU")
    EntityManager em;
Clicking on the warnings in the left column prompts you to let NetBeans fix the imports. Yes, please!

Next and finally, we need to create a method to return the desired customer, something along the order of this:


This method issues a named Customer query using the ID provided and returns a single result. Since createNamedQuery returns a generic object, we cast it to what we know is coming back to us (a Customer object), then return the customer's full name. Not pretty, but good enough for a first cut.

Editing the appropriate list.xhtml file to display the customer's name instead of number is as simple as replacing <h:outputText value="#{item.customer}"/> with <h:outputText value="#{customerLookupBean.getName(item.customer.id)}"/> to query and receive the full customer name, taking us from this:

To this:

"Pretty" comes later, but this is a start. Any thoughts, comments, or questions? Please drop me an email or comment below!

All the best,
Mark

Wednesday Apr 04, 2012

Extreme Portability: OpenJDK 7 and GlassFish 3.1.1 on Power Mac G5!

Occasionally you hear someone grumble about platform support for some portion or combination of the Java product "stack". As you're about to see, this really is not as much of a problem as you might think.

Our friend John Yeary was able to pull off a pretty slick feat with his vintage Power Mac G5. In his words:

Using a build script sent to me by Kurt Miller, build recommendations from Kelly O'Hair, and the great work of the BSD Port team... I created a new build of OpenJDK 7 for my PPC based system using the Zero VM.

The results are fantastic. I can run GlassFish 3.1.1 along with all my enterprise applications.

I recently had the opportunity to pick up an old G5 for little money and passed on it. What would I do with it? At the time, I didn't think it would be more than a space-consuming novelty. Turns out...I could have had some fun and a useful piece of hardware at the same time. Maybe it's time to go bargain-hunting again.

For more information about repurposing classic Apple hardware and learning a few JDK-related tricks in the process, visit John's site for the full article, available here.

All the best,
Mark

Thursday Mar 01, 2012

Welcome to the Jungle

Greetings, and welcome to the Java Jungle! We'll cover a lot of ground here, traveling some well-worn paths and blazing a few new trails as well. My plan for this site is to explore anything that might be of interest or use to the greater Java community. So...what might you see on these pages?

Discussions about Java Enterprise Edition (Java EE) application servers...rich-client development...mobile platforms...web apps...performance tuning...JVM languages...IDEs...the list is nearly limitless. Java as a language, an environment, and a concept is huge. We won't cover it all, but if it's Java-related, it's probably fair game.

Please feel free to drop me a line with any suggestions or requests you may have by clicking this link. Thanks for stopping by!

All the best,
Mark 

About

The Java Jungle addresses topics from mobile to enterprise Java, tech news to techniques, and anything even remotely related. The goal is to help us all do our work better with Java, however we use it.

Your Java Jungle guide is Mark Heckler, an Oracle Java/Middleware/Core Engineer with development experience in numerous environments. Mark's current work pursuits and passions all revolve around Java and leave little time to blog or tweet - but somehow, he finds time to do both anyway.

Mark lives with his very understanding wife, three kids, and dog in the St. Louis, MO area.



Stay Connected

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today