File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB and Other Java EE Technologies and the fly likes Struts & EJB - how to mix? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » EJB and Other Java EE Technologies
Reply Bookmark "Struts & EJB - how to mix?" Watch "Struts & EJB - how to mix?" New topic
Author

Struts & EJB - how to mix?

Buzz Andersen
Ranch Hand

Joined: Aug 28, 2001
Posts: 54
Hi,
I have an architectural sort of question about how best to mix Struts & EJB. I am building an application in which I am using a Struts-like framework to handle the view and controller (i.e. I use beans to handle form processing and actions to control the flow), and I am using CMP EJBs to handle database access.
As I said, I'm using beans to populate HTML forms with information from the database, but now I'm wondering about how best to populate the beans themselves. Is there a better way of doing it than simply using an action or something to get the fields from my EJB session bean and set their complimentary fields in the form bean? What about using the form bean as a "facade" for my Session EJB (which is in turn a facade for my Entity EJB)? The idea here would be that each get method in my form bean would simply call its complimentary get method in my session bean. Conversely, after validation, each set method would call its accompanying set method in the session bean.
Does this make sense, or would it have some kind of performance cost or other gotcha?
Thanks,
Buzz
Junilu Lacar
Ranch Hand

Joined: Feb 26, 2001
Posts: 3008
Refer to Kyle Brown's article on Session Facade.
My 2 cents (take this with a grain of salt because I'm no expert ):
1. Using a Session facade in your Servlet ties it directly to the J2EE layer. I would put another layer such as a BrokerInterface layer between my servlet and the actual persistence mechanism. This will make it easier to switch between persistence mechanisms, e.g. if you want to use XML to test, MS-SQL Server to integrate, Oracle or DB2 for production. You won't have to have different versions/methods of the Servlet to handle different persistence layers, you'll just program to an interface in the Servlet and switch persistence layers.
2. Using the Session facade the way you propose (delegate gets) gains you nothing. The session facade should simplify the interface to the entity, not just serve as a pass-through. Use a Value Object (aka Data Transfer Object) as a parameter to the Session Facade. I.e.:
ActionForm <--> Data Transfer Object <--> BrokerInterface <--> Session Facade <--> Entity Bean
It may seem like a roundabout way but it is a more flexible design, IMHO.
Junilu


Junilu
[How to Ask Questions] [How to Answer Questions] [MiH]
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3878
I'm with Junilu (gee, he sure sounds like an expert ). Using a large-grained session facade and data transfer objects is the way to go.
You might want to look at my article on Struts where I talk about similar things. In particular, look at the perform() method in my Action class in the article.
Kyle
------------------
Kyle Brown,
Author of Enterprise Java (tm) Programming with IBM Websphere
See my homepage at http://members.aol.com/kgb1001001 for other WebSphere information.
[This message has been edited by Kyle Brown (edited November 08, 2001).]
[This message has been edited by Kyle Brown (edited November 08, 2001).]


Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at http://www.kyle-brown.com/ for other WebSphere information.
Junilu Lacar
Ranch Hand

Joined: Feb 26, 2001
Posts: 3008
Originally posted by Kyle Brown:
I'm with Junilu (gee, he sure sounds like an expert ).

Aww shucks... Thanks, Kyle. I'm really just regurgitating my understanding of the stuff you and others wrote in articles on VADD
Junilu

[This message has been edited by JUNILU LACAR (edited November 09, 2001).]
Buzz Andersen
Ranch Hand

Joined: Aug 28, 2001
Posts: 54
Thanks to everyone!. Actually, it just so happened that I stumbled onto Kyle's article when I was perusing his website yesterday, and I immediately latched onto his ideas as relevant to my own question. Per the article's recommendations, I have implemented a session facade which provides "large grain" functionality (this actually makes a lot of sense for me since I have to pull data from a lot of different tables/entity beans!) and used JavaBeans as "value objects" to communicate between application layers.
I have to admit I really dig this approach--it results in that nice, clean MVC separation I was looking for. Everything I need in my JSP can be accomplished with useBean and get and setProperty tags, which is very cool!
Junilu, thanks for your advice as well--that seems like sound practice. I will now implement a BrokerInterface layer as well...
Thanks again!
Buzz
[This message has been edited by Buzz Andersen (edited November 09, 2001).]
 
 
subject: Struts & EJB - how to mix?
 
Threads others viewed
What's the best way to handle multiple Java bean instance in Jsp?
Best design for a shopping cart
form bean/jsp design
Can session beans interact??
J2EE Design Question -- Am I On The Right Track?
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com