|
|
Another question for more experienced users:
I'm trying to do my application with Spring and AJAX and that's why I have to use multiple forms on one page. It looks sth like that:
Code:
lt;form method=quot;postquot; action=quot;ftpConnection.htmquot;gt;
lt;spring:bind path=quot;ftpConnection.actionquot;gt;
lt;input type=quot;textquot; name=quot;actionquot; value=quot;lt;c ut value=quot;${status.value}quot; /gt;quot; /gt;
lt;/spring:bindgt;
lt;input type=quot;submitquot; value=quot;Connectquot; /gt;
lt;/formgt;
lt;form method=quot;postquot; action=quot;ftpDirectory.htmquot;gt;
lt;spring:bind path=quot;ftpConnector.dirquot;gt;
lt;input type=quot;textquot; name=quot;dirquot; value=quot;lt;c ut value=quot;${status.value}quot; /gt;quot; /gt;
lt;/spring:bindgt;
lt;input type=quot;submitquot; value=quot;Change directoryquot; /gt;
lt;/formgt;
As you can see - first form is responsible for connecting with ftp server (of course I'm showing here very simplified versions of these forms - just as an example) and the second one should ask for directory with given name ('dir').
So, configuration for these two forms looks like this:Code:
lt;bean id=quot;ftpConnectionControllerquot; class=quot;xxx.web.spring.controllers.FTPConnectionControllerquot;gt;
lt;property name=quot;sessionFormquot;gt;lt;valuegt;truelt;/valuegt;lt;/propertygt;
lt;property name=quot;commandNamequot;gt;lt;valuegt;ftpConnectionlt;/valuegt;lt;/propertygt;
lt;property name=quot;commandClassquot;gt;lt;valuegt;xxx.web.ftp.FTPConnectionlt;/valuegt;lt;/propertygt;
lt;property name=quot;formViewquot;gt;lt;valuegt;imagemanagerlt;/valuegt;lt;/propertygt;
lt;property name=quot;successViewquot;gt;lt;valuegt;ftpConnection.htmlt;/valuegt;lt;/propertygt;
lt;/beangt;
lt;bean id=quot;ftpDirectoryControllerquot; class=quot;xxx.web.spring.controllers.FTPDirectoryControllerquot;gt;
lt;property name=quot;sessionFormquot;gt;lt;valuegt;truelt;/valuegt;lt;/propertygt;
lt;property name=quot;commandNamequot;gt;lt;valuegt;ftpConnectorlt;/valuegt;lt;/propertygt;
lt;property name=quot;commandClassquot;gt;lt;valuegt;xxx.web.ftp.FTPConnectorlt;/valuegt;lt;/propertygt;
lt;property name=quot;formViewquot;gt;lt;valuegt;imagemanagerlt;/valuegt;lt;/propertygt;
lt;property name=quot;successViewquot;gt;lt;valuegt;ftpConnector.htmlt;/valuegt;lt;/propertygt;
lt;/beangt;
They both extends SimpleFormController.
And for imagemanager.htm there is such controller:
Code:
lt;bean id=quot;imControllerquot; class=quot;xxx.web.spring.controllers.ImageManagerControllerquot; /gt;
And it extends AbstractController.
Ah, and mappings:
Code:
lt;bean id=quot;uclMappingquot; class=quot;org..web.servlet.handler.SimpleuclHandlerMappingquot;gt;
lt;property name=quot;uclMapquot;gt;
lt;mapgt;
lt;entry key=quot;/imagemanager.htmquot;gt;lt;ref bean=quot;imControllerquot; /gt;lt;/entrygt;
lt;entry key=quot;/ftpConnector.htmquot;gt;lt;ref bean=quot;ftpDirectoryControllerquot; /gt;lt;/entrygt;
lt;entry key=quot;/ftpConnection.htmquot;gt;lt;ref bean=quot;ftpConnectionControllerquot; /gt;lt;/entrygt;
lt;/mapgt;
lt;/propertygt;
lt;/beangt;
So, as more experienced users have already noticed - I have a problem with delivering (injecting?) command objects (defined using commandName and commandClass attributes) to these forms. When I try to bind them (on .jsp page) with objects written down in configuration I'm getting errors about non existing objects. They are correct beacause I really don't have these objects .
When I enter /ftpConnector.htm or /ftpConnection.htm everything works fine (of course when I disable for a while the second form - in the other way I would have the same error as on imagemanager.htm page). So, the form is shown correctly and everything is great. I realized then that it all because imController does not contain these two command objects...
So, my question is: how can I make these command object available for these multiple forms within one .jsp page? Is it possible? If not, maybe there is other solution for such problem? I will be very thankful for any help...
Ok, I found an idea and I did it but I need your opinion if this method is correct:
We can provide access to these objects in imController by injecting them in xxx-servlet.xml. I did then sth like this:
Code:
lt;bean id=quot;ftpConnectionquot; class=quot;xxx.web.ftp.FTPConnectionquot;gt;
lt;property name=quot;actionquot;gt;lt;valuegt;connectlt;/valuegt;lt;/propertygt;
lt;/beangt;
lt;bean id=quot;imControllerquot; class=quot;xxx.web.spring.controllers.ImageManagerControllerquot;gt;
lt;property name=quot;ftpConnectionquot;gt;lt;ref bean=quot;ftpConnectionquot; /gt;lt;/propertygt;
lt;/beangt;
and I have changed ImageManagerController class by adding ftpConnection field with getter and setter. Then, I had to somehow make these objects visible to lt;spring:bindgt; tags so I tried to add them to ModelAndView object under the name used in 'spring:bind path' attribute. And... It's alive
Is this method correct? Will I have any problems with this in future? I.e. command objects prepared in form controllers will override (or not) these prepared by me, objects? I'm asking because I don't know all mechanisms of Spring and I'm doing all this just quot;as I feel it should bequot;.
Thanks for all opinions (if any appears ). |
|