Announcement Announcement Module
Collapse
No announcement yet.
2 forms, 2 Models, 2 Controllers, 1 .jsp Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 2 forms, 2 Models, 2 Controllers, 1 .jsp

    Hello,

    I am pretty new to Spring and would appreciate any help on the following problem:

    I have pageA.jsp with html formA, which submits to ControllerA and uses ModelA. The success view for pageA.jsp is pageB.jsp. pageB.jsp has 2 forms, each of which have their own Model and Controller. The first form in pageB.jsp is identical to formA and it submits to ControllerA and uses ModelA. The second form, formB, submits to ControllerB and uses ModelB.

    In formA several html fields are populated by values in ModelA. For example, in pageA.jsp:


    Code:
    <c:when test="${command.findWithin}">


    Where “command” refers to ModelA and findWithin is a Boolean.

    The problem is that when pageA.jsp loads it tries to retrieve values from ModelB so I get an error like:

    “…Unable to find a value for "findWithin" in object of class ModelB”

    In pageB.jsp if I comment out
    Code:
    “<c:when test="${command.findWithin}">
    ” in the first form (formA) I get the same error, but it refers to the next line that tries to get a value from ModelA (i.e.
    Code:
    “<c:when test="${command.findOutside}">”.
    Here is a section of my servlet.xml (the views are mapped to their corresponding .jsps):

    Code:
    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
              <property name="mappings">
                <props>
                  <prop key="/pageA.jsp">pageAForm</prop>
                  <prop key="/pageB.jsp">pageBForm</prop>
                </props>
              </property>
            </bean>
    
            <bean id=" pageAForm " class=" web. pageAForm ">
              <property name="formView" value="pageAView"/>
              <property name="successView" value="pageBView"/>
            </bean>
    
            <bean id="pageBForm" class=" web.pageBForm">
              <property name="formView" value=" pageBView "/>
              <property name="successView" value="pageCView"/>
            </bean>


    My questions are:

    When I load pageA.jsp, why is it looking in ModelB for values on pageB.jsp, which hasn’t even loaded yet?

    Do I have to approach this differently in order to have 2 forms on one page that each use their own model and controller?

  • #2
    Hi mate,

    I know it's late so this might be a stupide questionne, but don't you just need to bind your form objects to different names? It looks as if you're binding both to "command", which would be inherently problematic.

    HTH

    Mike

    Comment


    • #3
      Hi Mike,

      Thanks for the reply. The form in pageA populates a Model or View State, data are added to the View State Server-Side, and the same View State is returned to pageB to populate the first form. So "command" still refers to that View State. The second form on pageB refers to its View State as "navcommand". The app is trying to find "command.findWithin" by looking in the "navcommand" View State.

      Shouldn't I be able to refer to the same View State on a different page by the same name ("command")?

      Thanks,
      J

      Comment


      • #4
        Hi J [very MiB],

        I'm having some trouble following your use of the terms View State, form and Model.

        Spring uses the term "Command" for what I get the impression most people refer to as a form object, indeed in Spring Webflow the FormObjectAccessor uses this terminology. When you say:

        The form in pageA populates a Model or View State, data are added to the View State Server-Side
        I presume that you mean that a controller creates a form-object and binds it to the Model before rendering the view. The users fills in the HTML form and posts it back to the server where the values are bound to the form-object.

        Moving on:

        ... and the same View State is returned to pageB to populate the first form.
        Sorry, not really sure what you mean here. But anyway, surely the crux of your question comes from:

        So "command" still refers to that View State. The second form on pageB refers to its View State as "navcommand". The app is trying to find "command.findWithin" by looking in the "navcommand" View State.
        How you bind the form object to the ModelAndView is up to you, but there's no magic involved here, if you bind it as "navcommand" you will not find it under "command". Objects previously bound in earlier requests will not be there.

        Hope this helps,

        Mike

        Comment


        • #5
          Hello Mike [Agent M],

          Thanks for your reply and clarification.

          I changed some things in the servlet.xml so i don't get the error when I load pageA, but I still have the same problem with submitting from pageB.

          There are several html elements on pageB that are populated, when the page loads, with values from the form-object from pageA.

          Like:
          Code:
           ...
          <c:when test="${command.findWithin}">
          ...
          Where the form-object from pageA is bound like:
          Code:
          ...
          return new ModelAndView(view, "command", viewState);
          ...
          Also on pageB is an html form (frmNav) that has its own form-object and ModelAndView. The form-object is bound like:
          Code:
          ...
          return new ModelAndView(view, "navcommand", mapViewState);
          ...
          When I try to submit the frmNav html form (document.frmNav.submit(); ), it trys to find values in the form-object for "navcommand" that are are only in the form-object for "command" (i.e. command.findWithin) and the form is never submitted to the server. I get the following error:

          org.apache.jasper.JasperException: javax.servlet.jsp.JspException: An error occurred while evaluating custom action attribute "test" with value "${command.findWithin}": Unable to find a value for "findWithin" in object of class "com.web.MapViewState" using operator "." (null)

          I am confused as to why it even trys to evaluate those attributes. I hope my explaination of the problem is clear.

          Thanks again.

          Comment


          • #6
            Hi J,

            Sorry if this misses the point, but if you want to include a statement in pageB.jsp like this:

            Code:
            "${command.findWithin}"
            Then you need to include the following line:

            Code:
            return new ModelAndView(view, "command", mapViewState);
            rather than one binding the form to "navcommand", assuming, that is, that mapViewState has a property called "findWithin". The only thing stopping you binding more than one form object to the model for display in the JSP is that the controller would only be able to bind the parameters submitted from the JSP to the form-object it is expecting.

            Does this help?

            Mike

            Comment


            • #7
              Hi Mike,

              I got it working. There was an element in my navform that was not populated by one of my javascript functions. Instead of giving me an error about not being able to populate the viewstate, it would go to the default viewstate (command) and throw the error i posted earlier.

              Thanks for all your time and help.
              -J

              Comment

              Working...
              X