Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: Multiple forms on one view?

  1. #1
    Join Date
    Sep 2004
    Posts
    19

    Default Multiple forms on one view?

    How do you go about setting up multiple HTML forms on the same view? For example, I would like to have a template that is used to build every view on my site, and which includes sections for a header, a work area, and a footer. In the header, I would like to have a "Product Lookup" form that is always available. There may or may not be another HTML form in the work area section at any given time. I'm not even sure where to begin trying to figure out how to set this up. Hope that was clear.

    Thanks!
    Brian

  2. #2
    Join Date
    Aug 2004
    Location
    Carlisle, UK
    Posts
    184

    Default

    We do something similar, it's not a problem. You can have different bits - headers, footers, forms, whatever - included in your views with include lines.
    The form action will send the form parameters to the appropriate controller to handle it.
    The only thing to be aware of is that if the form uses <spring:bind> tags to display errors etc., you have to have the formbacking object with the errors instance in the model, otherwise you get an error.
    So you may need two versions of your form jsp: one without the spring bind tags, just for entry, which can be used in any view; and one with the error display handling which is used by the controller that handels the form's input.
    Unless there is a better way of handling this issue?
    Chris Harris
    Carlisle, UK

  3. #3
    Join Date
    Sep 2004
    Posts
    19

    Default

    Sorry, Chris, I'm just getting started with Spring MVC, and I'm still not clear how to configure a single view to support multiple form controllers.

    From my springapp-servlet.xml:

    Code:
    <bean id="form1Controller" class="Form1Controller">
        <property name="sessionForm"><value>true</value></property>
        <property name="commandName"><value>Form1Command</value></property>
        <property name="validator"><ref bean="form1Validator"/></property> 
        <property name="formView"><value>myOnlyView</value></property>
        <property name="successView"><value>form1Confirmation</value></property>
    </bean>
    
    <bean id="form2Controller" class="Form2Controller">
        <property name="sessionForm"><value>true</value></property>
        <property name="commandName"><value>Form2Command</value></property>
        <property name="validator"><ref bean="form2Validator"/></property>
        <property name="formView"><value>myOnlyView</value></property>
        <property name="successView"><value>form2Confirmation</value></property>
    </bean>
    
    <bean id="form1Validator" class="Form1Validator"/>
    
    <bean id="form2Validator" class="Form2Validator"/>
    
    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="/myOnlyView.vm">form1Controller</prop>
            </props>
        </property>
    </bean>
    Obviously a couple of issues with this. The primary issue is that the url "/myOnlyView.vm" can only be mapped to one of the two form controllers. The other form controller is never activated ("activated" probably isn't the correct term here... but it doens't work, anyways, and from a lack of log messages it is never "activated").

    Here is an example of the HTML that I would like to be able to somehow link up to one or more form controllers:

    Code:
    <html>
    <head></head>
    <body>
    <form id="form1" action="get">
    ...
    </form>
    <form id="form2" action="get">
    ...
    </form>
    </body>
    </html>
    Hope that's clear. Thanks in advance.

    Brian

  4. #4
    Join Date
    Sep 2004
    Posts
    5

    Default

    Hello,

    There are many issues concerning multiple form submission. I understand, that you would also like to invoke many controllers during submission. I had similar problem so I had to develop simple chaining mechanism based on the Chain of Responsibility pattern.

    If you problem concerns multiple form submission at single request I could give you more details.

  5. #5
    Join Date
    Sep 2004
    Posts
    19

    Default

    I'm actually trying to support two forms on one view, each with an independent form controller and independent form submission. I'm not trying to submit more than one form at once. Sorry if that was unclear.

    For an example of what I'm talking about, see www.amazon.com. On the left side of their home page are two search forms, one which allows you to search Amazon's "inventory", and another which allows you to do a "web search". Both are written as independent HTML forms. Both have a little, round, orange or red "GO!" button next to them.

    Thanks again.

    Brian

  6. #6
    Join Date
    Aug 2004
    Location
    Auburn, AL, USA.
    Posts
    106

    Default

    If I understand what you want to do is:

    • Have one display page.
    • Have two (or more) forms with different processing logic.
    • Display the processing results in the 'same' view/URL for all forms.


    This is one of the things that, while no worse that others, Spring doesn't really help too much with. What I've often done is set up 3 controllers:

    Code:
    <bean id="form1Controller" class="Form1Controller">...</bean>
    <bean id="form2Controller" class="Form2Controller">...</bean> 
    <bean id="displayController" class="DisplayController">...</bean>
    Then the processing controllers can send redirects to the URL mapped to the displayController. If you don't mind having different URLs, then you can do without the displayController.

    Also note that if a form doesn't have an action="x" then it submits to the current URL, so for multiple forms only 1 can submit to the current page:
    Code:
    <form id="form1" method="get">
    ...
    </form>
    <form id="form2" method="get" action="MyFormController2URL">
    ...
    </form>
    Also, take a look at the ParameterizableViewController class in the mvc package, depending on how much processing per-form, etc.. it may also suit your needs.

  7. #7
    Join Date
    Aug 2004
    Location
    Carlisle, UK
    Posts
    184

    Default

    Brian,

    You've had a number of answers so far, so if you're sorted now, fine.
    But if not, my understanding is you want two (or more) forms on one page, say a product query form, and a login form. If they complete and submit the product query form, they get back a search results page. If they complete and submit the login form, they get a page saying they've successfully logged in (or not), and maybe showing their details. Or whatever.

    So in the jsp of the view with multiple forms, you need, for example:

    Code:
    <form method="post" action="<c&#58;url value="search.xyz"/>">
          your product search form stuff
    </form
    
    <form method="post" action="<c&#58;url value="login.xyz"/>">
           your login form stuff
    </form>
    with the action in each case specifiying the url for the controller that handles the form, as mentioned in the previous post.
    Here xyz is the suffix that gets handled by your servlet (if that's how your servlet mapping works).

    These two controllers handle the form input, and each send their response to their own results view.

    Hope that helps - post again if not.
    Chris Harris
    Carlisle, UK

  8. #8
    Join Date
    Sep 2004
    Posts
    19

    Default

    Chris,

    That's exactly what I want. And I can handle it as you suggested, using a different URL in each action parameter (am I correct in believing that that would then direct control to whichever Controller was mapped to the specified URL?).

    However, does that preclude the use of Command objects, Validators, etc.? Could I press you and ask how the bean configuration might look? Thanks, again, in advance!

    Brian

  9. #9
    Join Date
    Aug 2004
    Location
    Tampa, FL
    Posts
    39

    Default

    Note that if you need to prepopulate all forms of the view (or validate them all at once or something), one way to do this is have a single Controller and a single Command object that contains the fields for all of your forms.

    In your view you can have a submit with different names for each form (but technically you have a single form in your HTML). For example, I have:

    Code:
    <form ....
    
    <input type="submit" name="submit.update" value="Update"/>
    <input type="submit" name="submit.delete" value="Delete"/>
    </form>
    Then in the onSubmit for your Controller you can find out which submit was clicked and do the appropriate action. For my example:

    Code:
    if &#40;request.getParameter&#40;"submit.update"&#41; != null&#41; &#123;
       // do stuff for update
    &#125; else if &#40;request.getParameter&#40;"submit.delete"&#41; != null&#41; &#123;
       // do stuff for delete
    &#125;
    This may be overkill for what you want to do (or even problematic due to validatation), though, and you may want to stick with the multiple controller solution.

  10. #10
    Join Date
    Aug 2004
    Location
    Carlisle, UK
    Posts
    184

    Default

    The suggestion in the previous post is a useful option in some cases. However the problem is that it makes it awkward to mix and match forms in your pages.
    If you want formA in a dozen pages, and formB in a dozen other pages, and both formA and formB in a further half dozen pages, you can't do it without some complex & messy conditional logic in your jsp.
    The jsp is much cleaner if you just include the jsp files for the forms you want in that page.

    However the problem then is that pre-population of form fields is difficult, if not impossible. If you're happy with blank form fields, or fixed defaults every time the form appears, then it's OK.

    The issue here is how you arrive at the view that has the form. If you're coming from the controller that handles the form (e.g. you went to that controller with a get, so it knows it's a request for the form, not a form submission), then you have all the facilities that offers, to create a form backing object, pre-populate it, set up reference data, etc. etc.
    If you arrive at the view that has the form via a diffenent controller, that knows nothing about this form, then you don't have these facilities, and all you can do is present the blank form (with standard defaults if required).

    When you submit the form however, you willl always be going to the controller that handles the form (via the action url), usually with a post, so the controller knows it's a form submission. Then all the standard things happen, i.e. creation, population and validation of the form backing object, followed by execution of onSubmit (provided the object is valid).

    This is documented in the JavaDoc for AbstractFormController, in the section headed Workflow. Steps 1-7 are for a form request (get) and will never be executed in the scenario we are considering; steps 8-14 are for a form submission (post), and will be executed.

    As for the bean configuration, I don't think it would be any different to the 'standard' set up of using the controller to handle the form request and the form submission. The only difference is you're not using the controller to handle the form request. That won't affect the bean configuration.

    Don't forget that with this set up, you can't use spring:bind tags in the form used in multiple pages for data entry. But of course you can use them in the form(s) used by the controller to display errors or success.

    Hope this helps. Ask again if not.
    Chris Harris
    Carlisle, UK

Similar Threads

  1. Replies: 9
    Last Post: Nov 1st, 2005, 10:36 PM
  2. Multiple forms approach?
    By MadOtis in forum Web
    Replies: 1
    Last Post: May 18th, 2005, 05:21 PM
  3. Content Provider vs View Model
    By Martin Kersten in forum Swing
    Replies: 21
    Last Post: Mar 10th, 2005, 02:25 PM
  4. Replies: 1
    Last Post: Feb 25th, 2005, 07:12 AM
  5. Replies: 4
    Last Post: Jan 21st, 2005, 08:43 AM

Posting Permissions

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