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

  • 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
    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.

    Comment


    • #3
      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.

      Comment


      • #4
        Your welcome. Glad your code is more maintainable now.

        Comment

        Working...
        X