Back Forum Reply New

In the view, how do I get the command name?

So here's a simple question.

The command's name is set when the controller bean is configured, through the commandName tag.  The form backing object is put in the model, keyed by that commandName.  Let's say that we used the string quot;flarnyquot; as the name of the command object.  We then use the name quot;flarnyquot; to reference the command object in spring:bind tags.

Let's say I'm developing JSP pages, and I have NO IDEA what name was used in the controller definition for the command object.  In fact' I don't want to know, because this particular JSP page could be invoked by one of several controllers, each of which may have a different commandName defined.

How can I extract the name used for the command object?  Where is it stored?  Is it stored by Spring somewhere?

Is there some attribute in some context somewhere accessible by the view that contains the string used to name the command object?  In other words is there an attribute somewhere ${Foo} that contains quot;flarnyquot; such that I can do this:

lt;spring:bind path=quot;${Foo}.bar.bazquot;gt;

instead of this:

lt;spring:bind path=quot;flarny.bar.bazquot;gt;

Thank you in advance,

John Vance

I forgot to mention I'm using Spring 1.2.8, and it's unlikely we'll migrate to 2.0 in the near future.


Originally Posted by John VanceLet's say I'm developing JSP pages, and I have NO IDEA what name was used in the controller definition for the command object.  In fact' I don't want to know, because this particular JSP page could be invoked by one of several controllers, each of which may have a different commandName defined.

Whatever compelling reason there is for these controllers not to be able to standardize on quot;flarnyquot;, wouldn't that same reason be as compelling for quot;Fooquot;, or any other quot;well knownquot; places?Originally Posted by John VanceHow can I extract the name used for the command object?  Where is it stored?  Is it stored by Spring somewhere?

The command name is injected into a controller instance, and also kept in the corresponding bean definition. I suspect that accessing either place to get the command name would make the JSP more tightly coupled to the controller(s) instead of the other way around.Originally Posted by John VanceIs there some attribute in some context somewhere accessible by the view that contains the string used to name the command object?  In other words is there an attribute somewhere ${Foo} that contains quot;flarnyquot; such that I can do this:

lt;spring:bind path=quot;${Foo}.bar.bazquot;gt;

instead of this:

lt;spring:bind path=quot;flarny.bar.bazquot;gt;

Originally Posted by manifoldroninWhatever compelling reason there is for these controllers not to be able to standardize on quot;flarnyquot;, wouldn't that same reason be as compelling for quot;Fooquot;, or any other quot;well knownquot; places?

Because the developers were learning Java, Spring, and in fact the entire web stack all at the same time on a quot;get it done yesterdayquot; project.  Because different developers worked on different portions of the application and chose command names that made sense for their particular corner of the application, without considering the benefits of standardizing.  The application is now several hundred thousand lines of code and I really really don't have the time to refactor all the JSPs to use a standard name for the command.

If this were Struts, I'd just use form.bean and be done.
Originally Posted by manifoldroninThe command name is injected into a controller instance, and also kept in the corresponding bean definition. I suspect that accessing either place to get the command name would make the JSP more tightly coupled to the controller(s) instead of the other way around.

I don't see how dropping the command name into the context - or better yet, dropping a standardized reference to the command object into the context* - would couple the view any more tightly to the controller.  As it is in my situation, the fact that different names were chosen for different controllers binds the JSP to the controller very tightly.  I'm asking for something to LOOSEN the coupling.

Anyway, if somebody has an answer to this, I'd very much appreciate it.

*which doesn't solve my problem because the errors are referenced by the command name, so I really do need the command name and not another reference to the command.

i have a horrible way.

Rewrite the simpleFormController to reject any commandName it is given and always use the default.

Also change the form tag to do the same.

but that would be messing with the internals of spring

Thank you Chanyaka.  I thought along those lines too, but instead of rejecting the given command name, providing an additional mapping in the model to expose the injected command name.

Maybe instead of hacking at the framework I could use AOP to do that, possibly on the return from formBackingObject().
¥
Back Forum Reply New