Announcement Announcement Module
No announcement yet.
Using SimpleFormController Page Title Module
Move Remove Collapse
Conversation Detail Module
  • 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;

    <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()));

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


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

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

    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.


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


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