Results 1 to 10 of 11

Thread: Spring MVC and JSTL

Hybrid View

  1. #1

    Default Spring MVC and JSTL

    I hope this is the correct forum for this question.


    I have a simple Spring MVC based web app.

    I generate a ArrayList of Hash Maps which look like this

    [{key1=value1, key2=value2, key3=value3, key4=value4}, {key1=value1, key2=value2, key3=value3, key4=value4}]
    I use a multi action controller to return this object with my view like this
    Code:
     ModelAndView view = new ModelAndView("list_view");
            view.addObject("list", ListObject);
            return view ;
    in my view

    HTML Code:
         <TABLE BORDER="1">
          <TH>Test</TH>
          <c:forEach var="current_outer" items="${Request.list}">
                    <c:forEach var="current_inner" items="${current_outer}">
            <TR>
              <TD><c:out value="${current_inner.Key1}"  /></TD>
              <TD><c:out value="${current_inner.Key2}"  /></TD>
              <TD><c:out value="${current_inner.Key3}"  /></TD>
              <TD><c:out value="${current_inner.Key4}"  /></TD>
            </TR>
                  </c:forEach>
          </c:forEach>
        </TABLE>
    Everything compiles and loads fine into tomcat . I have verified that JSTL is work correctly in tomcat. I have verified that the request contains all the right values .

    However I get no data rendering in the table rows .

    I suspect I am not correctly referencing the object to the JSTL forEach tag.

    I thought a had an iterations problem thus the outer and inner forEach loops. but even with only one forEach i get the same result

    HTML Code:
    <tbody><tr><th>Test</th>
          </tr><tr>
              <td><c:out value=""></c:out></td>
              <td><c:out value=""></c:out></td>
              <td><c:out value=""></c:out></td>
              <td><c:out value=""></c:out></td>
            </tr>
               
        </tbody>
    google,books and the petclinic have all failed me at this point

    any help would be greatly appreciated.

  2. #2

    Default

    Try the following:
    • Change "Request.list" to "list"
    • Change "current_inner.Key1" to "current_inner.key" and "current_inner.value"


    Code:
    <TABLE BORDER="1">
      <TH>Test</TH>
      <c:forEach var="current_outer" items="${Request.list}">
        <c:forEach var="current_inner" items="${current_outer}">
          <TR>
            <TD><c:out value="${current_inner.key}"  />: <c:out value="${current_inner.value}"  /></TD>
          </TR>
        </c:forEach>
      </c:forEach>
    </TABLE>

  3. #3

    Default

    Ok i will give that a try .

    Thanks for the quick response . I tried some variations on the values options earlier but not this one .

    I also used the "list" option as well but will try again . When i used the "list" object I expected to see x number of TR blocks , one for each list element even if the values were not there . I never reached that error state , always only 4.

    I will try what you suggest and post back.

    thanks again

  4. #4

    Default Something happened !

    Ok Well we have change and thats good .

    I now get an exception ( non was thrown previously ) when the jsp loads.


    org.apache.jasper.JasperException: An exception occurred processing JSP page /WEB-INF/views/list.jsp at line 51

    48: <c:forEach var="current_outer" items="${List}">
    49: <c:forEach var="current_inner" items="${current_outer}">
    50: <TR>
    51: <TD><c:out value="${current_inner.Key1}"/></TD><TD><c:out value="${current_inner.value}"/></TD>
    52: <TD><c:out value="${current_inner.Key2}"/></TD><TD><c:out value="${current_inner.value}"/></TD>
    53: <TD><c:out value="${current_inner.Key3}"/></TD><TD><c:out value="${current_inner.value}"/></TD>
    54: <TD><c:out value="${current_inner.Key4}"/></TD><TD><c:out value="${current_inner.value}"/></TD>

    javax.el.PropertyNotFoundException: Property 'Key1' not found on type java.util.HashMap$Entry
    I double checked the key names ( including case) and they are correct.

    Perhaps its looking on the wrong block or location?

    hmmm

  5. #5

    Default

    current_inner is of type HashMap$Entry. This means you can reference its "key" and "value" properties to get the key and value of a given entry.

    Change your code to use current_inner.key rather than current_inner.Key1:

    Code:
    <c:out value="${current_inner.key}"/>
    This will output the key itself.

  6. #6

    Default fixed !

    That did it .

    Thanks very much .

    As a side note where can find the JSTL docs ?

    i could only find the 1.1 docs here

    http://download.oracle.com/docs/cd/E.../docs/tlddocs/

  7. #7

    Default which reference

    Ok expanding of the reference question above .

    It appears the in the c:out tag is an EL expression. ( right ? )

    The question is how do i get the value based on key ?

    something like

    Code:
              <TD><c:out value="${current_inner.["literalNameOfKey"].value}"/></TD>
    do i have do something awkward like

    Code:
    <c:choose>
    <c:when test="${current_inner.key == literalNameOfKey}">
    <TD><c:out value="${current_inner.value}"/></TD>
    </c:when> 
    </c:choose>
    I don think the above will produce the results desired ..ie values based on a specific key where needed.

Posting Permissions

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