Announcement Announcement Module
Collapse
No announcement yet.
use the JSTL's forEach with spring Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • use the JSTL's forEach with spring

    In order to display something like:

    <input type="text" name="detailForm.colleagues[0].name" value="Jon"/>

    <input type="text" name="detailForm.colleagues[1].name" value="Mike"/>

    <input type="text" name="detailForm.colleagues[2].name" value="Charlie"/>


    I used the JSTL's forEach, like this:


    // bind to “detailForm” path

    <spring:nestedPath path="detailForm">

    // for each colleague in the detail form

    <c:forEach var="colleague" items="${detailForm.colleagues}" varStatus="loopStatus">

    // set a path prefix, e.g colleagues[0]

    <c:set var="prefix" value="colleagues[${loopStatus.index}]"/>

    // bind to a specific property path for the current collegue, e.g. “name”

    <spring:bind path="${prefix}.name">

    <input type="text" name="${status.expression}" value="${status.value}"/>

    </spring:bind>


    </c:forEach>


    </spring:nestedPath>



    When run, complaining about the items="${detailForm.colleagues}" , saying: items cannot be runtime expression. value: "[${createAssetForm.lstAstMainContributors}]"


    What is wrong in the codes?


    Thanks to help.

  • #2
    Can you post the code to "createAssetForm" and "detailForm"?

    We need to see the Java for this one.

    Bob

    Comment


    • #3
      Thanks for the response. DetailForm.java is:

      --------------------------------------------------------------
      public class DetailForm implements Serializable {

      private List colleagues;


      /**
      * @return Returns the colleagues.
      */
      public List getColleagues() {
      return colleagues;
      }
      /**
      * @param colleagues The colleagues to set.
      */
      public void setColleagues(List lstColleagues) {
      this.colleagues= lstColleagues;
      }

      ...
      ...
      ...
      -------------------------------------------------------------

      Correction, the error message is: items cannot be runtime expression. value: "[${detailForm.colleagues}]



      Thanks to help.

      Comment


      • #4
        Your colleagues list cannot be null when the JSTL asks for it. Has it been instantiated at that point?

        Bob

        Comment


        • #5
          Then I tried the initialization


          //This is the DetailAction.java

          public class DetailAction extends FormAction {

          public DetailForm() {
          setFormObjectName("detailForm");
          setFormObjectClass(DetailForm.class);
          setFormObjectScope(ScopeType.FLOW);
          ...



          //This is the DetailForm.java

          public class DetailForm implements Serializable {

          List colleagues;
          //I tried the initialization as this,
          public DetailForm() {

          TestListBean contrib = new TestListBean ();

          this.colleagues.add(contrib);



          But it did not works. Same error message persists.


          What is the right way to initiate a List colleagues ?



          Thanks
          Last edited by scott; Jan 4th, 2006, 02:10 PM.

          Comment


          • #6
            at the top of your page, are you using
            <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
            or
            <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>

            Comment


            • #7
              This is how I do it:-

              Code:
              /*
               * EditGlobalOptionsCommand.java
               *
               * Created on 04 December 2005, 12:21
               */
              package org.xxxx.xxxxadmin.command;
              
              import java.util.ArrayList;
              import java.util.List;
              
              import org.xxxx.xxxxadmin.bean.PostageBandBean;
              
              /**
               * A POJO that acts as a Spring command for editing the global options
               *
               * @author Matt Parker ([email protected])
               */
              public class EditGlobalOptionsCommand
              {
                  private String generalCaption;
                  private String generalTextTop;
                  private String generalTextBottom;
                  private String ringingCaption;
                  private String ringingTextTop;
                  private String ringingTextBottom;
                  private List postageBands;
                  
                  <...SNIP...>
                  
                  /**
                   * Sets the list of postage bands
                   *
                   * @param postageBands The list of postage bands
                   */
                  public void setPostageBands(List postageBands)
                  {
                      this.postageBands = postageBands;
                  }
                  
                  /**
                   * Retrieves the list of postage bands
                   *
                   * @return The list of postage bands
                   */
                  public List getPostageBands()
                  {
                      if (postageBands == null)
                      {
                          postageBands = new ArrayList();
                      }
                      
                      return (postageBands);
                  }
                  
                  /**
                   * Adds a postage band bean to the list
                   *
                   * @param postageBandBean The postage band bean
                   */
                  public void addPostageBand(PostageBandBean postageBandBean)
                  {
                      getPostageBands().add(postageBandBean);
                  }
              }
              I have a framework that I've written that allows me to intercept the creation of the form backing object and add the appropriate number of beans in the view controller and to adjust the number in the form controller if the submitted number is different (in other words I can call getFormBackingObject() from within my controllers and get my command object).

              You can get a version of this from here (it's Apache licensed so do what you like with it) - http://www.zen29969.zen.co.uk/mpsc-modules-0.2.zip - I'm still waiting on Sourceforge to approve the project to get hosting there.

              In particular, look at uk.co.mpcontracting.modules.spring.controller.Abst ractFormController and uk.co.mpcontracting.modules.spring.controller.Abst ractViewController to get some ideas.

              There is JavaDoc, but no other documentation. Of course, I don't accept any responsibility for bugs you may find etc etc

              Bob

              Comment


              • #8
                Hi All of code are used but they are not work. Please give me a solution why they are not working.

                Thanks


                Rubber bracelets

                Comment


                • #9
                  Can we see your web.xml file, at least the top of it?

                  Comment

                  Working...
                  X