Announcement Announcement Module
Collapse
No announcement yet.
ModelAndView losing the model, not displayed by the view Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • ModelAndView losing the model, not displayed by the view

    I am trying to get the Step-By-Step application working and I am having trouble with the model which is set into the ModelAndView returned by the handleRequest() method of the SpringappController class.

    Within the SpringappController.handleRequest() method I can list the products in the model for debugging (seen on the JBoss console), but the JSP (view) which should display these values shows no date or product values, as if the model is not present. The model is set in the ModelAndView in the SpringappController.handleRequest() method just as listed in the Step-By-Step tutorial:

    Code:
            // return a ModelAndView
            return new ModelAndView("hello", "model", myModel);
    Any ideas as to why this is happening, or what I might try in order to get past this problem ?

    Thanks in advance for any suggestions.


    --James

  • #2
    If the myModel Map contains the right values then you either do a redirect instead of an include of the view, or the JSP is wrong. Show some code from the JSP.

    S.

    Comment


    • #3
      Thanks for the help. Here is the hello.jsp I am using:

      Code:
      <%@ include file="/WEB-INF/jsp/include.jsp" %>
      
      <html>
          <head>
              <title><fmt&#58;message key="title"/></title>
          </head>
          <body>
              <h1>
                  <fmt&#58;message key="heading"/>
              </h1>
              <p>
              <fmt&#58;message key="greeting"/>
              <c&#58;out value="$&#123;model.now&#125;"/>
              </p>
              <h3>Products</h3>
              <c&#58;forEach items="$&#123;model.products&#125;"
                               var="product">
                  <c&#58;out value="$&#123;product.description&#125;"/> 
                  $<c&#58;out value="$&#123;product.price&#125;"/>
                  <br><br>
              </c&#58;forEach>
              <br>
              <a href="<c&#58;url value="priceincrease.htm"/>">Increase Prices</a>
              <br>
          </body>
      </html>

      Here is my SpringappController.handleRequest()

      Code:
          public ModelAndView handleRequest &#40;HttpServletRequest request, 
                                             HttpServletResponse response&#41;
              throws ServletException,
                     IOException
          &#123;
              // get a date string which will be part of the model
              String now = &#40;new java.util.Date&#40;&#41;&#41;.toString&#40;&#41;;
              
              // DEBUGGING
              System.out.println&#40;"\n\n\tRUNNING THE SPRINGAPP CONTROLLER'S HANDLEREQUEST\n\n"&#41;;
              Iterator iterator = m_productManager.getProducts&#40;&#41;.iterator&#40;&#41;;
              System.out.println&#40;"\n\n\tCurrent Products\n"&#41;;
              int count = 1;
              while &#40;iterator.hasNext&#40;&#41;&#41;
              &#123;
                  Product product = &#40;Product&#41; iterator.next&#40;&#41;;
                  System.out.println&#40;"Product " + count + "&#58; " + product.getDescription&#40;&#41;&#41;;
                  System.out.println&#40;"    Price&#58; " + product.getPrice&#40;&#41;&#41;;
                  count++;
              &#125;
              System.out.println&#40;"\n\n"&#41;;
              
              
              // create a Map to use as the model, add the date and products
              Map myModel = new HashMap&#40;&#41;;
              myModel.put&#40;"now", now&#41;;
              myModel.put&#40;"products", m_productManager.getProducts&#40;&#41;&#41;;
      
      
              // DEBUGGING
              iterator = &#40;&#40;List&#41; myModel.get&#40;"products"&#41;&#41;.iterator&#40;&#41;;
              System.out.println&#40;"\n\n\tModel Products\n"&#41;;
              count = 1;
              while &#40;iterator.hasNext&#40;&#41;&#41;
              &#123;
                  Product product = &#40;Product&#41; iterator.next&#40;&#41;;
                  System.out.println&#40;"Product " + count + "&#58; " + product.getDescription&#40;&#41;&#41;;
                  System.out.println&#40;"    Price&#58; " + product.getPrice&#40;&#41;&#41;;
                  count++;
              &#125;
              System.out.println&#40;"\n\n"&#41;;
              
      
              // return a ModelAndView
              return new ModelAndView&#40;"hello", "model", myModel&#41;;
          &#125;

      Comment


      • #4
        Change ${model.products} to ${products} and you will be fine :-)

        S.

        Comment


        • #5
          Thanks for this suggestion. Unfortunately I made the change in my hello.jsp but it didn't help. It doesn't make sense anyway since the JSP tag (c:forEach) probably needs to first access the model by name to get its product list.

          Any other suggestions ?


          --James

          Comment


          • #6
            If you want to use ${products} in the JSP, you'll have to use a different ModelAndView constructor:
            Code:
            Map myModel = new HashMap&#40;&#41;;
            myModel.put&#40;"now", now&#41;;
            myModel.put&#40;"products", m_productManager.getProducts&#40;&#41;&#41;;
            return new ModelAndView&#40;"hello", myModel&#41;;
            If that still doesn't work, include a JSP scriptlet (or just use JSTL) to dump all request attributes to the page or stdout, to see where the model data has gone.

            Comment


            • #7
              Or, if you want to use your original code, access the model this way:
              Code:
                  <c&#58;forEach items="$&#123;model&#91;'products'&#93;&#125;" >
                   ....

              Comment


              • #8
                I think the problem comes from the forEach which iterate on a list rather than a collection. try to do a <c:out value = "${model.products}/> to see if the model you sent contains an item products which is not empty.

                Olivier

                Comment


                • #9
                  I have tried using the ModelAndView constructor with no model name and it still doesn't work. Next I put in some scriplet code in hello.jsp to show the request attributes, and sure enough the products are there. I am obviously getting the model as part of the request, so this isn't the problem. Perhaps I have an error somewhere in the JSP tags, perhaps the fmt:message or c:forEach ? I have pasted these into hello.jsp straight from the tutorial, but Eclipse shows warnings (yellow underlining) at these tags with no explanation, so I'm afraid that there is some problem with these tags, or that maybe the JSTL tag library definition files I'm using aren't the correct version for the JBoss/Tomcat I'm using (JBoss 3.2.5) ?

                  --James

                  Comment


                  • #10
                    do y have
                    Code:
                    <%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
                    <%@ taglib uri="http://www.springframework.org/tags" prefix="Spring" %>
                    on the top?

                    Comment


                    • #11
                      Originally posted by monocongo View Post
                      I am trying to get the Step-By-Step application working and I am having trouble with the model which is set into the ModelAndView returned by the handleRequest() method of the SpringappController class.

                      Within the SpringappController.handleRequest() method I can list the products in the model for debugging (seen on the JBoss console), but the JSP (view) which should display these values shows no date or product values, as if the model is not present. The model is set in the ModelAndView in the SpringappController.handleRequest() method just as listed in the Step-By-Step tutorial:

                      Code:
                              // return a ModelAndView
                              return new ModelAndView("hello", "model", myModel);
                      Any ideas as to why this is happening, or what I might try in order to get past this problem ?

                      Thanks in advance for any suggestions.


                      --James
                      Although I see James' quoted code looks correct, he still may have been tripped up by the same problem that snagged me in the Spring MVC tutorial. The third code section of Step 16 on http://www.springframework.org/docs/...ep-Part-2.html has a lot of interlaced new code so it becomes easier to type rather than copy and paste, and I missed changing the line that was


                      Code:
                      return new ModelAndView("hello", "now", now);
                      to


                      Code:
                      return new ModelAndView("hello", "model", myModel);
                      in my actual code, which is needed to pass the new model object on to the view. The tutorial is right, but it's easy to overlook that detail.

                      Either that or he may have been using the

                      Code:
                      <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
                      include line the tutorial recommends with the wrong servlet version. I got an exception screen with that URI, but

                      Code:
                      <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
                      recommended above worked for me in 2.4 running Tomcat as an Eclipse server with JRE 1.5.0, and so did

                      Code:
                      <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
                      --wial

                      Comment


                      • #12
                        Hi James,

                        Have you resolved your problem? I am running to exactly the same problem as yours. Thanks.

                        Wen

                        Comment


                        • #13
                          i think u should write view resolvers. to give a proper view for your model.

                          Comment


                          • #14
                            hello

                            i used to worrk with this taglib,
                            Code:
                            <%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
                            <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
                            <%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt" %>
                            i had your same problem, exists different versions of taglib or configurations, i work with tomcat 5.5.27/29

                            regards

                            Comment


                            • #15
                              Originally posted by manish_iitg View Post
                              i think u should write view resolvers. to give a proper view for your model.

                              My view resolver is as below, but I still have the same problem. Any Ideas? Thanks.


                              <!-- View Resolver -->
                              <bean id="viewResolver"
                              class="org.springframework.web.servlet.view.Intern alResourceViewResolver">
                              <property name="viewClass">
                              <value>org.springframework.web.servlet.view.JstlVi ew</value>
                              </property>
                              <property name="prefix">
                              <value>/WEB-INF/jsp/</value>
                              </property>
                              <property name="suffix">
                              <value>.jsp</value>
                              </property>
                              </bean>

                              Comment

                              Working...
                              X