views:

224

answers:

1

If i put an object named "foo" into model in Spring controller, and want to limit its scope, how can I do this.

Let's say I have a page which uses a jsp tag that takes as a parameter "foo". If I call the tag inside jsp, like <tag foo="${bar}" />, it seems to me that the model "foo" is interfering with the "bar".

Or even if not, if I just want to limit the scope of model "foo" to be accessible only in jsp page, and not in others (either in 'ed page or in a tag that first jsp page calls.)

+1  A: 

Basically you can't restrict the scope. The only way to pass objects from a Controller to the view is via the request scope attributes.

If you have a lot of different views/controllers for a single page you may want to have a naming convention for your request attributes, something like the class name of the controller. Only trouble is that accessing them is not so clean

${requestScope['com.your.app.Controller.RESULT']}

One possible is to use to create page scope versions of the variables:

<c:set var='result' value="${requestScope['com.your.app.Controller.RESULT']}"/>
...
${result}

In your example I'm not sure I'd say that 'foo' is interfering with 'bar' it looks like you are passing bar to the tag as a parameter?

Gareth Davis
Thanks. In the example, yes, I'm passing bar as a parameter. But in the tag I get "model foo", not bar, as the value binded to "parameter foo". At least, I've had troubles with name clashing in the past. I'm not sure if I've had just an another problem, but I feel that request parameter "foo" overrides tag parameter "foo" in tags.
egaga
I think there may have been another issue, request parameters will not be passed to anything unless you specifically request them to be via an EL expression.
Gareth Davis