Results 1 to 10 of 11

Thread: Spring doubt: how would you do it ?

Hybrid View

  1. #1
    Join Date
    Oct 2007
    Posts
    14

    Default Spring doubt: how would you do it ?

    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:
    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>
    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.
    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:
    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>
    then re-using form controller, in which in onSubmit method check operation type and do right job based on its type, as follow:
    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);
        }
      }
      ...
      ......
      .......
    }
    Using this solution, for any necessary task, must add a bean association in my conf file, and intercepts operation type into form controller.
    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.

  2. #2
    Join Date
    May 2005
    Posts
    288

    Red face The command object can have behaviour, too

    If you have the same workflow for different operations, extracting the respective operation to the command object is a viable option. We did that quite often for complex update and delete operations, because the work flow to identify the object to be updated or deleted is identical.

    Just define an abstract class UploadBean for your command objects with an abstract method (eg. doJob(...)). Implement this method in two (or more) command beans (InsertUploadBean, ModifyUploadBean, ...).

    The onSubmit-method in the Controller then calls the method of the command bean to execute the operation at hand, like this:

    Code:
    protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception {
    
          UploadBean upload = (UploadBean) command;
          upload.doJob(.........);
          return new ModelAndView(getSuccessView(), "upload", upload);
        }
      }
    Make sure, that all the objects needed for the operations do appear in the argument list of the method doJob.
    Arguably, this is more object-oriented than the usual way of calling the service layer explicitly in the controller.

  3. #3
    Join Date
    Oct 2007
    Posts
    14

    Default

    Hi Degenaar,
    thanks for reply,
    i understood your proposal, i believe...i define an abstract class as my command object, with abstract method.....after implement it as needed.
    In this way I delegate specific behavior to implemented command bean (InsertUploadBean, ModifyUploadBean, ...) associating each to corresponding form controller.
    But in this way, do i need a form controller for each task and a bean definition too in config file ??
    My purpose is to resolve this task with less code possible and using one form controller, and possibly adopting better implementation ...that i don't know if it is praticable.

    Regards,
    edcruise.

  4. #4
    Join Date
    May 2005
    Posts
    288

    Red face A parent definition will help

    You will need only one controller class for all the use cases, but one bean definition per use case.

    A parent bean definition (read the reference for further information) will help to make things shorter.

    Taking your example, it can be as short as this:
    Code:
    <bean id="abstractUploadController" class="FileUploadController" abstract="true">
            <property name="sessionForm" value="true"/>
            <property name="commandName" value="uploadBean"/>
            <property name="validator" ref="uploadValidator"/>
            <property name="successView" value="success"/>
            <property name="uploadManager" ref="uploadManager"/>
    </bean>
    
    <bean id="fileUploadController" parent="abstractUploadController">
            <property name="commandClass" value="InsertUploadBean"/>
            <property name="formView" value="fileup"/>
    </bean>
    
    <bean id="modFileUploadController" parent="abstractUploadController">
            <property name="commandClass" value="ModUploadBean"/>
            <property name="formView" value="mod-fileup"/>
    </bean>
    HTH
    Fokko

  5. #5
    Join Date
    Oct 2007
    Posts
    14

    Default

    I like your solution,
    is it a typical spring-approch ? And then, put business logic into bean is a correct approach ?? I know that Command class is a Java Bean that holds state of a form view (tipically in a form application).

    regards,
    edcruise

  6. #6
    Join Date
    Mar 2006
    Posts
    312

    Default

    I typically use one Controller and one bean mapping and differentiate operations via request parameters.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •