File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSF and the fly likes Basic JSF design question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Basic JSF design question" Watch "Basic JSF design question" New topic
Author

Basic JSF design question

Pinal N Patel
Ranch Hand

Joined: Sep 05, 2003
Posts: 57
Hello Everyone,

I am very new to JSF and I am having a little hard time designing JSF components.

Basically, my application contains three jsp pages. First is called Entry.jsp which contains a input text field (to enter a name of a person) and two submit buttons ("Get Addr Info" button and "Get Recently Viewed Product" button). Based on the submit button, I have a POJO class that goes and retrieves relevant information for a person typed in the input text field and displays in the appropriate jsp pages (addr.jsp or viewedProduct.jsp).

Now, my question is, Whose responsibility is to retrieve the database information? Is it the backing bean (EntryBean) associated with Entry.jsp? So, this means, I have to call the POJO class from EntryBean to retrieve the Person info from database and populate the associated backing bean for the next view page based on the navigation rule.

OR, Should AddrBean/ViewProductBean retrieve the Person info from db? If yes, Could you please describe/seq diagram on how the flow works?

I really appreciate everyone's input on this.

Thanks in advance!!

OR,
Sol Mayer-Orn
Ranch Hand

Joined: Nov 13, 2002
Posts: 311
Hi,

I've only done little JSF so far, but based on my experience with other MVC web frameworks, I'd strongly consider a third option: another class (some people may call it 'controller class', 'action class', or even 'facade').

So this would end up as follows:

1. class EntryForm:
Stores request parameters that were submitted from entry.jsp
So in entry.jsp you have some input fields such as:
< h:inputText value="#{entryBean.text}" / > ...

2. class AddrView:
Stores data to be shown by the 'address' jsp (e.g. city, street...)
So in addrView.jsp you have some output fields such as:
< h utputText value="#{addView.stree}" / > ...

3. class ProductView:
Stores data to be shown by the 'product view' jsp (e.g. list of products)
So in productView.jsp you have some output fields such as:
< h utputText value="#{productView.price}" / > ...

4. class EntryController :
the only class that holds logic that talks with the database (directly or, preferrable, though another utility class).
This class would hold references to the other beans, and read/update them as required:



Use the following code to make sure entryController methods are called when buttons are clicked:


Now, the only remaining problem is how to make sure that your EntryController actually knows the beans entryForm, addrView, productView..
For this, JSF uses 'injection' technique, which means you need the following lines in your 'faces-config.xml', for 'EntryController' (this tells JSF to call EntryController.setEntryForm() with your 'Entry Form', and similarly for the other bean):



Good luck.
Sol Mayer-Orn
Ranch Hand

Joined: Nov 13, 2002
Posts: 311
Sorry, slight correction:
on step 1 ("EntryForm")
I wrote < h:inputText value="#{entryBean.text}" >
It should actually be < h:inputText value="#{entryForm.text}" >
Alexander Jesse
Ranch Hand

Joined: Mar 17, 2004
Posts: 35
Isolate your db-accesses into a service-layer. Each service concentrating on one business-object.

For Cross-object-boundary functionality, write facade-objects.

Objects that travel the layer-boundaries are DTO's (DataTransferObjects).

From your managed beans access either
pure: only facade-objects (needs a few more)
mixed: facade-objects and services

How to find the services? Either use a service-lookup facility or use Spring.

hth
Alexander
Pinal N Patel
Ranch Hand

Joined: Sep 05, 2003
Posts: 57
thanks a lot guys!!. The information all of you have provided is greatly appreciated.
Pinal N Patel
Ranch Hand

Joined: Sep 05, 2003
Posts: 57
ok Guys, I have one more question based on the above postings.

The controller methods are called from action tags in JSP, then the controller gets the data from the backend and populates the form. So, it seems like after retriving the data, controller needs to set the navigation-case values( such as "success", "failure" etc) to determine the next page. Is it a good idea to let the controller decide the navigation case?

Regards,
Pinal.
Alexander Jesse
Ranch Hand

Joined: Mar 17, 2004
Posts: 35
The controller (as known eg within Struts) is not existant in JSF. You call a managedbean-method which does the usecase-work and then returns the navigation-outcome.

The navigation-outcome can point to different result-pages in dependance from the source-page...
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Basic JSF design question