aspose file tools*
The moose likes Struts and the fly likes Why ModelDriven not working? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Why ModelDriven not working?" Watch "Why ModelDriven not working?" New topic
Author

Why ModelDriven not working?

Daniele De Rossi
Greenhorn

Joined: Apr 05, 2010
Posts: 8
My action class has 2 different methods which return each different Bean class.
This action class is mapped in strusts.xml like this;


==struts.xml==

<action name="Beans" class="Myaction" method="doSomething">
<result>result_mybean.jsp</result>
</action>

<action name="Beans" class="Myaction" method="doSomething2">
<result>result_mybean2.jsp</result>
</action>


==Myaction.java==

...implements ModelDriven, Prepare {

private MyBean bean;
private MyBean2 bean2;


public String doSomething() {
bean= new MyBean ();
...
setMyBean(bean); // <--this setter will push the bean to the valuestack.. right???

return SUCCESS;
}

public String doSomething2() {
bean2 = new MyBean2();
...
setMyBean2(bean2);

return SUCCESS;
}


public Object getModel() {
return null; //<-- It returns null because setter() will push the beans to the valuestakc..
}
public void prepare() {
//does nothing..
}

//...getters & setters of MyBean, MyBean2
...
}//end of MyAction




And I try to access to the propery of MyBean class in JSP

==result_mybean.jsp==
...
<s:property value="mybeanProperty" />

But it doens't work!

And this does work!
<s:property value="bean.mybeanProperty" />

I think I could access to the property without a class name due to the modeldriven interface and interceptor(in default interceptor Stack).

Teach me what I'm thinking wrong..



+)...I wonder that one action class should play only one role.

Or related tasks(like view, list, delete, update a Bean) can be together in one action class.

How you guys doing about action class??
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9304
    
  17

Daniele you are not using the ModelDriven correctly. Calling setMyBean or setMyBean2 will not push the objects on the value stack. Actually after the action is finished processing, the whole action class object is added to the value stack. The bean properties of the action can then be used directly (like accessing bean2 calls getBean2 in the action class). ModelDriven is generally used with forms so that form values are automatically set with the property name and the bean name is not required. If you just want to access the properties of the bean without using the bean name, then you can manually push it on the value stack. In the JSP you can use the s:push tag to do this...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Also, please UseCodeTags when posting code or configuration. Unformatted code and configuration is unnecessarily difficult to read. You can edit your post by using the button.
Daniele De Rossi
Greenhorn

Joined: Apr 05, 2010
Posts: 8
I figured out how to use ModelDriven exactly by now, sorry.

I had to do like this;


This works well with MyBean class.(Can get access to the property without a class name)

But as I said, two different methods is mapped in struts.xml


==struts.xml==

And each result page tries to do with different beans class.

So, getModel() and prepare() have to work different depends on which method was called.

I mean when doSomething() is called, getModel() and prepare() must do with MyBean class.

And when doSomething2() is called, getModel() and prepare() must do with MyBean2 class.

Is it possible??

This is why I asked you before post, one action must do one task or can do many related tasks.

I hope you all understand what I mean..

(Thanks for you tips, David Newton)
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Use a parameter to name the class, use a factory that's injected into the action, etc.
Daniele De Rossi
Greenhorn

Joined: Apr 05, 2010
Posts: 8
I tried to make object instance by class.forName(). I didn't do the Factory Pattern because I want to simplify the problem.

And faced a problem.It seems that the methods call sequence like this;

prepare()(by Prepare Interceptor) -> ... -> getModel() -> execute() or other methods mapped in struts.xml(doSomething() in here)

Because prepare() is called before doSomething(), there's no way to know the class.forName() in prepare();




getClass() throws NullPointerException because object is null.

I don't know when and where to create object instance..

And get back to my last question, Do I have to create each actions on each tasks?(action1 does the MyBean1, action2 does the MyBean2...)?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You can re-order or re-call interceptors as necessary (for example, see the prepareParamsPrepare stack).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why ModelDriven not working?