aspose file tools*
The moose likes Struts and the fly likes Why do we need ModelDriven approach? i find problems with it ?  Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Why do we need ModelDriven approach? i find problems with it ?  " Watch "Why do we need ModelDriven approach? i find problems with it ?  " New topic
Author

Why do we need ModelDriven approach? i find problems with it ?

shukla raghav
Ranch Hand

Joined: Aug 03, 2008
Posts: 200
In the modelDriven approach in struts2 corresponding to an action there is a model object that is returned from within the action class.

Is it neccessary that there is only one type of model object required to be returned. why not more than one.

say a user enters the information in a form. this information can be divided into 2 different chunks one portion related to his personal details that goes to UserTable another related to "question" asked by him that goes to QuestionsTable. Correspondingly for personal details of every Visitor we have a VisitorBean and Question details are stored in the QuestionBean. now in a single action how can we invoke both the beans using a ModelDriven approach when we can have only single Bean object to returned using getModel() method.

Such situations are very common where a single form is used to populate several different Beans? What is the best way to do it ?


Daniele De Rossi
Greenhorn

Joined: Apr 05, 2010
Posts: 8
I faced the similar problem and I made it work in very low level.
I placed a if-else check in prepare() and handle the beans by the parameter values..
Maybe I could have done it with some patterns for the higher level. But it seems meaningless to me.
In your example, I think each different cases should be connected to each different actions..or separate each cases to two forms.
I think two irrelevant cases are in one form and that's where the problem begins..

Well, I'm not sure my thoughts are right and wanna know how the ohters think about this..
shukla raghav
Ranch Hand

Joined: Aug 03, 2008
Posts: 200
Yes there are ways to handle it but then the basic purpose behind the modelDriven approach is defeated. here is the problem that i am really facing. i am thinking of somthing like action chaining.

what if a particular action requires more than one model objects to be pushed to the value stack. say i am creating an application for online forums. Any visitor can post a question without registering, all they need to do is provide few personal details.

now they need to provide following information through the form. PostThread.jsp

1. Name of User
2. Email Id of the User
3. City they belong to
4. Proffession

5. Subject of the Question
6. Topic it belongs to
7. Question in detail

Now the data no. 1,2,3,4 are his personal information that must populate the Model called Visitor.java and the information 5,6,7 must populate the model object called Question.java. the action that must populate the two models is PostThreadAction.java. The result jsp i.e. will display all the details entered by the user from 1 to 7. now for this my Action class PostThreadAction.java requires to return more than one Model Object - Visitor object and Question object to the value stack using getModel().

PostThreadAction extends ActionSupport implements ModelDriven{





Still waiting for the authors to provide a solution for this problem
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Shukla> Is it neccessary that there is only one type of model object required to be returned. why not more than one.

Because Java methods can only return one object--that has nothing to do with Struts or ModelDriven. This is why the composition pattern exists.

Shukla> i am thinking of somthing like action chaining.

Don't; action chaining is almost *always* a Bad Idea.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why do we need ModelDriven approach? i find problems with it ?