lovespring
lovespring

Reputation: 19569

Is there a better way to transfer data to view in Struts 2

I add properties to Action object, and in my JSTL/JSP view, I read those properties of action in valueStack.

I just feel not clean, if I have to add some data to view, I have to add a property to my action.

And this leads to my action, data, and view were coupled.

Is there a better way to do this?

class MyAction{
    int theDataSentToView1;
    String theDataSentToView2;
}

in JSP:

echo myAction.theDataSentToView1;
....

Upvotes: 0

Views: 67

Answers (1)

Roman C
Roman C

Reputation: 1

In Struts2 Action is a model in the sense of MVC. So the model properties should be available to view to use them with JSTL, EL, OGNL, etc.

All this expression languages used in the view operate on valueStack in Struts2, the action bean placed on top of it by the framework, so you easy can couple your data to the valueStack.

If you don't want to use it in the action, you can push/put it somewhere in the valueStack, so it will be accessible from there.

But you shouldn't do that because framework has ModelDriven feature, that could be used for model in the sense of MVC instead of action.

Just move your properties from the action to the model object. modelDriven interceptor pushes this object on top of the valueStack.

Upvotes: 1

Related Questions