aspose file tools*
The moose likes Struts and the fly likes where to put this: action or model ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "where to put this: action or model ?" Watch "where to put this: action or model ?" New topic
Author

where to put this: action or model ?

Raj Bhandari
Ranch Hand

Joined: Oct 19, 2005
Posts: 97
Hi,
Please help me with this design issue.
I've made a model class which I'll use for some of my actions. This class queries the database and returns the result so that my action can forward it to jsp (to populate drop downs).
Here is what I'm doing currently..

My Action class:


My Model Class (FormsPopulate.java):



So, now my question is that my action class still has to know what column names is being returned by the Model class, so that it can do new LabelValueBean(rsACMod.getString("Name"), rsACMod.getString("ID")).

Also, I'll have to repeat this while loop code again in all action classes (which doesn't sound right)

Otherwise if I let the model do this part:

then I'm making the model depend on struts' LabelValueBean.

Am I missing something here ? How should I handle this... tks.
[ April 19, 2006: Message edited by: Raj Bhandari ]
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
I've faced the same dilemma. The Struts LabelValue bean is a handy bean to use, but in order to really use it you have to create a dependency on Struts in your model layer, which is a bad thing.

By the same token, needing to know column names in your action form is also a bad thing.

I solved this problem by creating my own version of a LabelValue bean and putting it with my model classes. After all, it's just a simple java Bean with a label and value property. That way I don't have any imports to Struts classes in my model packages, and I still have an object that I can use to shuttle information back and forth between the model and the view.

I'd suggest you create classes and methods in your model layer that perform more specific tasks, such as getCustomerList() which in turn call getModelList. Then call the specific methods from your Action class.


Merrill
Consultant, Sima Solutions
Raj Bhandari
Ranch Hand

Joined: Oct 19, 2005
Posts: 97
Thanks Merrill for the suggestion. Your reply has reaffirmed me that I'm on the right path.
Following your reply, I created my own LVBean and moved the while loop code to the model and all the model does is return the action an arraylist. This works well now.

I have one more question which is somewhat related:

As you can figure that in my action, I have the following lines now, which works perfect:


I'm thinking its not good to be setting attribute for each and every method I have in my model here in the action.
So I thought I should just pass the model object itself to the jsp, like:

request.setAttribute("service", service);

I did make FormsPopulate model class a bean and when a new instance is created, the properties are set.
But then on the jsp I don't know how to get the method values using struts-el tags. What I tried was this:


But this doesn't work. So, basically how can I get service class's acmodelList property.
Thanks a ton!
[ April 19, 2006: Message edited by: Raj Bhandari ]
Raj Bhandari
Ranch Hand

Joined: Oct 19, 2005
Posts: 97
please answer this
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Try this:

<htmlptionsCollection name="service" property="acmodelList" label="label" value="value" />

This link explains how to use the <htmlptions> and <htmlptionsCollection> tags.
Raj Bhandari
Ranch Hand

Joined: Oct 19, 2005
Posts: 97
Hi Merrill,
Thanks a lot for your suggestion and the link. However I still get the following errror:


I know if I were using scripting, this is what I'd do:


but can't get it using the taglib. Any ideas what else should I try ?
Your help is much appreciated. Tks

[ April 20, 2006: Message edited by: Raj Bhandari ]
[ April 20, 2006: Message edited by: Raj Bhandari ]
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Did you use exactly the code that I gave you?

Frome the error message, it looks like you may have done this:

<htmlptionsCollection name="${service.acmodelList}" label="label" value="value" />

Which is incorrect.

If that isn't it, please post the code that you used to get this error message.
[ April 20, 2006: Message edited by: Merrill Higginson ]
Raj Bhandari
Ranch Hand

Joined: Oct 19, 2005
Posts: 97
Hi Merrill,
Sorry it was my mistake..
What I did was this:



But then I realized, I should remove the commented line and on trying that, it worked.

Tks a lot for your help, much appreciated
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
[edit: looks like you figured it out before I posted]

Looking at your last post, it looks like the code it doing a "toString" on your collection and then trying to use that as the name. I cannot help much without seeing the exact jsp that you are using. If you are using "${service.acmodelList}", the trying switching it to just "service.acmodelList".

If FormsPopulate is your data service component, it does not seem like good design to stick that object onto the request and have your jsp make calls directly against your service. I also do not like sticking a bunch of individual items on the request...how is the jsp developer supposed to know the names and types of all the items? I make it a practice to create a single form object, that will contain the properties needed to build the page. So my code might look like this:

FormsPopulate service = new FormsPopulate(dbconn);
ArrayList acmodelList = service.getAcmodelList();
ArrayList custList = service.getCustList();

MyDataForm myForm = new MyDataForm();
myForm.setAcmodelList(acmodelList);
myForm.setCustList(custList);

request.setAttribute("MyDataForm", myForm);

BTW, I would have my methods return a Collection or List instead of an ArrayList.

- Brent
[ April 20, 2006: Message edited by: Brent Sterling ]
Raj Bhandari
Ranch Hand

Joined: Oct 19, 2005
Posts: 97
Thanks Brent! That was a very-2 helpful design tip. I've changed my code to do that and it works gr8.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: where to put this: action or model ?