Results 1 to 4 of 4

Thread: Using SimpleFormController

  1. #1
    Join Date
    Mar 2008
    Location
    Taylors, SC USA
    Posts
    8

    Default Using SimpleFormController

    Hi everyone, I want to make sure I'm using SimpleFormController correctly.

    I've got a view which submits to a controller. The onSubmit calls a service method, which makes a web service call. There can be several different responses to the service method; the response dictates what view to forward to. I don't have a successView set, because there is no single successView per se; a successful web service call can return a number of responses.

    My controller extends SimpleFormController. I have several private String variables, for each view. The configuration xml injects these values. I.e.,
    private String response1View;
    private String response2View;
    etc.

    <bean...
    <property name="response1View" value="resp1.htm" />
    <property name="response2View" value="resp2.htm" />

    and so on.

    In my controller's onSubmit,
    if (resp.equals(RES1))
    return new ModelAndView(new RedirectView(getResponse1View()));

    if (resp.equals(RES2))
    return new ModelAndView(new RedirectView(getResponse2View()));

    if (resp.equals(RES3))
    return new ModelAndView(new RedirectView(getResponse3View()));

    etc.
    My question is, is this a proper use of SimpleFormController? It "works" I suppose, but I want it to work well.

    Regards,
    Brian

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    You normally don't want to burden your controller with the fact that it should redirect. So prefix your urls with 'redirect:' and remove the new RedirectView from your code.

    Next to that you have quite a collection of viewnames to resolve. It might be easier to put them in a map and use the response value as the key. So something like.

    Code:
    <bean id="yourController" class="YourController">
      <property name="viewMappings">
        <map>
          <entry key="RES1" value="redirect:resp1.htm"/>
          <entry key="RES2" value="redirect:resp1.htm"/>
          <entry key="RES3" value="redirect:resp1.htm"/>
        </map>
      </property>
    </bean>
    Then instead of all your ifs.

    Code:
    String viewname = viewMappings.get(resp);
    if (viewname == null) {
      //throw exception or set a default
    }
    return new ModelAndView(viewname);
    Now extending the resultcodes is as easy as adding a new entry to your mapping file, no changes in your code needed.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Mar 2008
    Location
    Taylors, SC USA
    Posts
    8

    Default

    When I looked at your code I said "Wow!" I (obviously) never thought of that, and was happy to see how much my code shrunk, being able to get rid of all the getters/setters, the ifs, etc. Thanks so much for your help.

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    Your welcome. Glad your code is more maintainable now.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

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