More Automatic JSF Generation

In addition to generating a whole CRUD application using JSF and the Java Persistence API, NetBeans IDE 5.5 dev builds also have some cool JSF generation features. I'm trying to tie everything up before going on vacation (two weeks in Spain - BRING IT!!!) and I was filling in some online help on this feature, so I thought I'd share it.

So let's say you don't want to generate a whole CRUD app, but you do want to generate a form for getting user information and storing it in a database. You'd want to create an entity class for the user information, (let's say a Book class with bean properties for author, title, and genre) and a JSF managed bean that acts a controller (methods for returning all books, saving a new book in the database, etc.). Maybe these things already exist, the developer who handles the DB and back-end has already set them up, and your job is just doing the front-end.

So you open up your web app, create a new JSP page called addbook.jsp. Then you drag the JSF Form palette item into the JSP and release. You get this dialog box, where you fill in the class name and specify you want an editable form (you want to create a new book, not just show the data from an existing book):

Press OK and you get this:

<f:view>
    <h2>Create</h2>
    <h:form>
        <h:panelGrid columns="2">
            <h:outputText value="Title:"/>
            <h:inputText id="title" value="#{anInstanceOfdb.book.Book.title}" title="Title" />
            <h:outputText value="Author:"/>
            <h:inputText id="author" value="#{anInstanceOfdb.book.Book.author}" title="Author" />
            <h:outputText value="Genre:"/>
            <h:inputText id="genre" value="#{anInstanceOfdb.book.Book.genre}" title="Genre" />
        </h:panelGrid>
    </h:form>
</f:view>

Now you need to change anInstanceOfdb.book.Book to a property in the controller class that represents an instance of a single book. Supposing you've got your controller class registered as a JSF managed bean with the name book, it might look something like this:

<f:view>
    <h2>Create</h2>
    <h:form>
        <h:panelGrid columns="2">
            <h:outputText value="Title:"/>
            <h:inputText id="title" value="#{book.book.title}" title="Title" />
            <h:outputText value="Author:"/>
            <h:inputText id="author" value="#{book.book.author}" title="Author" />
            <h:outputText value="Genre:"/>
            <h:inputText id="genre" value="#{book.book.genre}" title="Genre" />
        </h:panelGrid>
    </h:form>
</f:view>

You'd then add a commandButton to submit the form. Run it and here's what you get:

Pretty handy, especially when you've got a lot of user information you have to fill in. Note that you can also use the Read-only function to just present all of the data for an existing book as well.

Comments:

Compared to the Google Webtoolkit (GWT) JSF looks like stoneage

Posted by Momo on June 08, 2006 at 03:00 AM CEST #

Post a Comment:
Comments are closed for this entry.
About

johnc

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