Announcement Announcement Module
Collapse
No announcement yet.
form controller to form controller Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • form controller to form controller

    Hi,

    I'm new to using the springframework mvc for web development. I'm working with Spring 2.5 and have to put together what I thought was a simple use case.

    From one webpage the end-user will enter an account number. After querying the database the results will be shown on another web page. I extended the SimpleFormController and overrode the onSubmit method to retrieve the account number from the command object and query the database. After getting the data I'd like to then show the results on a second page. I returned a modelandview object attaching the queried data in the model return new ModelAndView(new RedirectView(getSuccessView()),model);

    Part of the problem is the redirect doesn't seem to work. The second controller is never executed.

    From the second webpage the end-user can either re-query using the same account number or enter another account number again showing the results on the second page. Being new to the springframework I'm not sure if I'm looking at doing two simpleformcontroller or a multi-action controller. Any help would be appreciated. I tried using two simpleform controllers where the successview specified in the first controller would be a redirect to the second simpleform controller. the second controller never is executed.

    here's my dispatcher-servlet.xml file
    <--simpleformcontroller one-->
    <bean id="employeeSearchController" class="mypackage.controllers.EmployeeSearchControl ler">
    <property name="sessionForm" value="true"/>
    <property name="commandName" value="employeeSearch"/>
    <property name="commandClass" value="com.dataanalytics.finance.domain.EmployeeSe arch"/>
    <property name="validator">
    <bean class="mypackage.controllers.validators.EmployeeSe archValidator"/>
    </property>
    <property name="formView" value="employeesearch"/>
    <property name="successView" value="reassignEmployee.htm"/>
    <property name="employeeService" ref="employeeService"/>
    </bean>
    </property>
    <property name="formView" value="employeeSearch"/>
    <property name="successView" value="redirect:reassigned.htm"/>
    <property name="employeeService" ref="employeeService"/>
    </bean>

    <--second simpleform controller-->
    <bean id="reassignedController" class="mypackage.controllers.ReassignedController" >
    <property name="sessionForm" value="true"/>
    <property name="commandName" value="employeeSearch"/>
    <property name="commandClass" value="mypackage.domain.EmployeeSearch"/>
    <property name="validator">
    <bean class="mypackage.controllers.validators.EmployeeSe archValidator"/>
    </property>
    <property name="formView" value="employeesearch"/>
    <property name="successView" value="reassignEmployee.htm"/>
    <property name="employeeService" ref="employeeService"/>
    </bean>

    <bean id="simpleUrlMapping" class="org.springframework.web.servlet.handler.Sim pleUrlHandlerMapping">
    <property name="order" value="0"/>
    <property name="mappings">
    <props>
    <prop key="/employeeSearch.htm">employeeSearchController</prop>
    <prop key="/reassignEmployee.htm">reassignedController</prop>
    </props>
    </property>
    </bean>

  • #2
    Going from controller to controller breaks the MVC principle. When an URL is invoked, that URL is mapped to a controller. The controller should:
    1) Pick which view is to be rendered (by optionally querying the business layer);
    2) Populating the model for that view (by querying the business layer for the required data);
    3) Returning the logical name of the view that was chosen to be rendered.

    In your case, you just need a single controller with a get method that reads from the request the account number, calls service layer to get account detail, adds the account detail to the model and then returns the view. The view can be a single jsp page, there is no need to create two. Simply, when there is no account detail in the model the detail displayal section is hidden and only the input to insert the account number and the button are shown. So, the view returned by the single controller should be always the same view.

    One last thing: you shouldn't use SimpleFormController since in your case there is no complex form but just a simple input. Just extend AbstractController or implement Controller. BTW, if you can upgrade to Spring 3 and use annotated controllers, since old style controllers are deprecated.

    Comment


    • #3
      redirecting to self

      hey Enrico,

      Thanks for your reply. I will attempt to do this using spring mvc 3. But my shop doesn't want to upgrade to 3.0 yet. One other thing I forgot to mention was i want to post the results to the same web page. so the web page used to enter the employee number to query and show the results will be the same page. Is this possible

      Comment


      • #4
        the web page used to enter the employee number to query and show the results will be the same page. Is this possible
        Yes it is not only possible but it's the best choice for your case. And I already said in my previous post that you should do that:

        The view can be a single jsp page, there is no need to create two
        The way to realize that is simple: in your controller you put the list on the model and then return the same view. The view shows the table with your list only if it finds it in the model (you can achieve this with a simple c: if jstl tag) so that the first time you enter the page there is only the search form, while when you enter an account and press the search button the table will appear on the same page. If you are into ajax and know the jQuery Data Tables framework, you can even make the table appear without having to reload the page!

        Comment

        Working...
        X