Results 1 to 9 of 9

Thread: use the JSTL's forEach with spring

Hybrid View

  1. #1

    Default 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. #2
    Join Date
    Jul 2005
    Posts
    246

    Default

    Can you post the code to "createAssetForm" and "detailForm"?

    We need to see the Java for this one.

    Bob

  3. #3

    Default

    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.

  4. #4
    Join Date
    Jul 2005
    Posts
    246

    Default

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

    Bob

  5. #5

    Default

    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 at 02:10 PM.

  6. #6
    Join Date
    Sep 2005
    Location
    Toronto, Canada
    Posts
    42

    Default

    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" %>

  7. #7
    Join Date
    Jul 2005
    Posts
    246

    Default

    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 (matt@mpcontracting.co.uk)
     */
    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

  8. #8
    Join Date
    May 2009
    Posts
    2

    Default

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

    Thanks


    Rubber bracelets

  9. #9
    Join Date
    Jan 2006
    Location
    Seattle, Washington
    Posts
    467

    Default

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

Posting Permissions

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