Hi at all,
i'm still spring newbie, i'm re-implementing a classical web application with spring, and i have a doubt on how to do a thing now.
Well, I have a classical task, in which a user can insert, modify and delete some data. Then i have a form in which user inserts data, follow some code:
uploadManager is a manager related to jdbcTemplate use to interact with database. From above, i have my controller, validator, form view, success view and Bean.Code:<bean id="fileUploadController" class="FileUploadController"> <!-- Bean member indicates operation type --> <property name="operationType"> <value>1</value> </property> <property name="sessionForm"><value>true</value></property> <property name="commandName"> <value>uploadBean</value> </property> <property name="commandClass"> <value>UploadBean</value </property> <property name="validator"> <ref bean="uploadValidator"/> </property> <property name="formView"><value>fileup</value></property> <property name="successView"><value>success</value></property> <property name="uploadManager"> <ref bean="uploadManager"/> </property> </bean>
Well, now i want to realize update task, but I would like to re-use som of my code, or better add to it less code possible. I thought to do this: add another bean mapping in conf file for update task:
then re-using form controller, in which in onSubmit method check operation type and do right job based on its type, as follow:Code:<bean id="modFileUploadController" class="FileUploadController"> <!-- Bean member indicates operation type --> <property name="operationType"> <value>2</value> </property> <property name="sessionForm"><value>true</value></property> <property name="commandName"> <value>uploadBean</value> </property> <property name="commandClass"> <value>UploadBean</value </property> <property name="validator"> <ref bean="uploadValidator"/> </property> <property name="formView"> <value>mod-fileup</value> </property> <property name="successView"><value>success</value></property> <property name="uploadManager"> <ref bean="uploadManager"/> </property> </bean>
Using this solution, for any necessary task, must add a bean association in my conf file, and intercepts operation type into form controller.Code:public class FileUploadController extends SimpleFormController { /** IOC Pattern. * Resolved reference by framework. */ private UploadManagerImpl uploadManager; public void setUploadManager(UploadManagerImpl uploadManager) { this.uploadManager = uploadManager; } private String uploadDir; private static final int INSERT_OP = 1; private static final int MODIFY_OP = 2; protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception { UploadBean upload = (UploadBean) command; switch (upload.getOperationType) { case INSERT_OP: /* INSERT JOB */ case MODIFY_OP: /* MODIFY JOB */ default ; return new ModelAndView(getSuccessView(), "upload", upload); } } ... ...... ....... }
Is this correct? From my point of view this is a classical implementation on a front controller, but maybe with spring, ther'e another pretty solution.
Any help is granted.
Regards,
edcruise.


Reply With Quote
...that i don't know if it is praticable.