aspose file tools*
The moose likes JSF and the fly likes How do you organize your code ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "How do you organize your code ?" Watch "How do you organize your code ?" New topic
Author

How do you organize your code ?

Juan M.
Greenhorn

Joined: Jan 04, 2005
Posts: 6
Hi,

I�m starting to learn JSF and I�ve two basic questions that I didn�t find answered in the tutorials available on the net and in the Sun docs. If someone could provide some help it�d be really welcome.

1)
Each component in JSF is backed with a model. Each JSP page with a JavaBean. In that java bean you must set the properties to feed each web component that requires it (lists, combos, etc) with its model. That way, a list for example must retrieve a Map as its model, and the Map can be retrieved from a database.

That Java Bean also has the logic to handle the event. And the same bean also has the code that invokes your business delegate methods (usually in the event handler methods).

Isn�t this dirty ? In struts you have Actions and Beans, here you are mixing everything in the same class.

2)
The JavaBeans that invoke your business delegate code you don�t have a reference to your HttpSession or a ServletContext object. So, when you need to store temporal data across several pages, where do you store it ? I did read that there is a way to restore the HttpSession from it. Is this correct ? If JSF wants to hide the JSP/Servlet objects from you, is it correct to use this ? If it isn�t, what is the best method to do this?

Any help or comment would be really welcome. I�m trying to see how clear JSF is, but it seems to be a bit dirty to me right now

Thanks in advance,

- Juan
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

1. No, it's not dirty. You have to realize that you are dealing with a significantly different type of framework. It's a component oriented design more similar to Tapestry and even Swing programming. You can't really think of it in terms of actions. It's actually more MVC than Struts.

Basically you have a page and that page has a backing bean. This backing bean is, as you said, more or less a javabean. But it also contains methods that provide functionallity for the page. You will typically also have another javabean that you use to pass the data around. For example, you wouldn't do any database work in your backingbean. You'd take all your values, verify that they are what you want in the backingbean, then pass those values via a regular javabean to whatever delegate you desire.

I think it's actually a bit cleaner than Struts.

2. You can get access to any of the regular j2ee stuff via FacesContext.getExternalContext(). with externalContext you can get the request, session, etc.

However, I typically don't like to grab the session from a bunch of different places. As several books suggest, I have a Visit object that stores anything about the users visit to my website. Anything I need to store in the session for whatever reason, I give Visit a property for. That way, I can grab the Visit object from the session (which doesn't even require me to use externalContext()) and then I have access to all my session objects I need.

If you don't have it, get a copy of JavaServer Faces In Action by Kito D. Mann. The online version is only $22. It answers these questions and more. It's a very good book for JSF.


GenRocket - Experts at Building Test Data
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16246
    
  21

I dunno. The older I get, the more I prefer to keep logic in logic beans and data in data beans and not make composite beans.

OTOH, it's one less class to keep track of this way.


Customer surveys are for companies who didn't pay proper attention to begin with.
Fintan Conway
Ranch Hand

Joined: Apr 03, 2002
Posts: 141
Originally posted by Juan M.:
1)
Each component in JSF is backed with a model. Each JSP page with a JavaBean. In that java bean you must set the properties to feed each web component that requires it, and the Map can be retrieved from a database.

That Java Bean also has the logic to handle the event. And the same bean also has the code that invokes your business delegate methods (usually in the event handler methods).

Isn’t this dirty ?


Hi Juan,

Have a look at
Rick HighTowers JSF tutorial. This uses a Controller bean to delegate methods to the Model bean.

HTH,

Fintan
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do you organize your code ?