I was wondering if anyone had any ideas or opinions on how to interact with the business tier from the web tier. We are using EJB's through a Session Facade and passing around Value objects.
On the web tier, we want to use WebWork or Struts. Now, the part we're getting stuck at is about how to pass the data back to the web tier. My collegue wants to extend the framework to some how create a value object instead of an Action object, because we'd have to end up copying the value object to the Action object anyway and would basically be duplicating the Value Object. Now I think that it would be fine having the Action object talk to the Value object. Would it not decouple the Web Tier from the Presentation Tier more effectively, or is the Session Facade already doing this? The Action object would me more like a helper object, no?
Any ideas or suggestions would be greatly appreicated.
If you use Struts all you have to do is add an instance of your value object within your ActionForm. You can get access to this value object as part of the ActionForm when you refer to it in your JSPs while using the custom tags. This will minimize copying of individual attributes.
When you are ready to commit the changes to the database, you would simply use the value object copy from your ActionForm to uypdate your EJB.
I don't think that you have to extend Struts for this to work cleanly. Perhaps if you provide a little more information I can help you further.
Another tip to better use Struts is to think about using DispatchAction instead of Action in order to group actions by context. For example, instead of having two Actions called Login and Logout, you can have one DispatchAction called Session with two methods called Login and Logout, respectively.
We're using Struts on the web tier which is making calls to a Session Facade, which will call on the entity beans which will return us one value object with all the data in it, for that request. The part we're having difficult with is how to pass this back to the web tier. If we're using Struts, would we have to copy all the values from the value object into the Action object?
Just trying to find a way to pass the data back easily I guess. I hope this is enough info. I'm not quite sure what else I could provide you.
Actually, it sounds like Leo might have answered my original question. The last time I used Struts, I had to create an Action class, and create getters/setters for all properties I wanted set from the form fields. Since many of the forms resembled (if not matched exactly) the properties of a value object, I had to then copy all the properties from the Action class to the value object class before passing it off to the Session Facade layer for update. As you can imagine, I had all kinds of classes with duplicated sets of properties.
Now, let me see if I understand you correctly: Struts can populate my value object (instead of the Action object) from form fields, but still invoke an Action and provide access to the value object for validation, updating, whatever else needs to occur...? If this is the case, this will work very well. This is exactly what I was after. I have used both Struts and WebWork for brief periods of time in the past, but was not sure if either provided this functionality.
Actually, you can so something similar in WebWork as well, using ParamaterAware, populate with some helpers in the Bean Util.
The way Struts determines which accessor methods to call is based on the property name.
For example, if your form contains a value object called "valueObject", which in turn contains a property called "property", which in turn has a property called "value", you can use something like
<html:textarea property="valueObject.property.value" cols="40" rows="5">
Struts will make the following calls to the ActionForm instance: getValueObject().getProperty().getValue(). Note that this does not imply that Struts has access to your attributes, since they should be either protected or private. Instead, Struts uses the name you specify and adds the "get"/"set" depending on the direction of the operation. We did not know this was possible, but when we learned about it we found it quite useful.
With this information, all you would have to do to minimize the copying of specific attributes is add a placeholder for each value object(s) in your ActionForm definition. You can literally use complex hierarchies and still use Struts to do the correct mapping.
There are other things to learn about Struts that are similar to this example. For instance (no pun intended), how does one traverse through list correctly, etc.
One thing that I am noticing here is the use of the word “Action.” I just want to make sure that we all understand that the Action class is only used to give us a chance to populate the ActionForm instance so that our data can be used for a given request. This ActionForm instance does not live past a single request. Therefore, if you want to make sure you can use the contents of the data you are gathering from the user over multiple requests (e.g. within the context of a Wizard), you will have to store the ActionForm instance yourself and pass it along as part of the processing of an Action. This applies to DispatchAction, which is what I recommend using over simple Action instances. A DispatchAction will minimize the amount of mapping you will have to do within your struts-config.xml.
Hope all this information helps you some. I know I had a bit of a tough time finding out all this information when we first started using Struts.
I want to know what basically is this value object and how to use it.Is it from ur form beam you are calling the value object?Can anybody give me some example codes for the same?
please take it as very urgent and reply me as soon as possible.
Thanks and regards,
Jyothi, take a look at the Sun Java Center J2EE Patterns
. They have an full explanation of the Value Object
design pattern, as well as other design patterns that you may find useful.
Rick, to answer your question directly. In order for the data in your value object to be updated automatically by Struts you would define it in the manner I already described (instance in ActionForm). Once the user completes specifying the information, the form would be "posted" to the server using the Struts mechanism. The ActionServlet would recognize it, and call the appropriate Action class. At this point you can get the ActionForm that is passed and get the contents of the Value Object instance within your ActionForm. You would then assign this copy of the value object to your EJB, which in turn would trigger the storage to the database. I hope I did not miss a step. I am assuming that you are already familiar with how EJBs work.
Thanks Leo for the excellent example. That's good to now that Struts will understand and populate Value objects (or any object for that fact) in the fashion. It definately makes things a lot easier!! The rest is simple from there. Thanks again for the tips.
i'm playing with xml and xslt to implement de view part of my project.
Do you think that struts is the best framework to do it???
We've successfully used struts in conjunction with XML/XSLT (using Xalan). A good article on this approach (a little bit different from the way we did it) is: