Search code examples
javaspringtapestry

How to get data from tapestry textfield with the same name?


I am so new using tapestry, i got several problem using it, how to get value from tapestry textfield with the same name?

For example:

<t:form t:id="names">
    <t:errors/>
    <div class="input-box">
        <t:textfield type="text" name="birthdate[Day]"/>
        <t:textfield type="text" name="birthdate[Month]"/>
        <t:textfield type="text"  name="birthdate[Year]"/>
    </div>
    <div class="input-box">
        <div class="col-md-12">
            <input type="submit" name="proceed" class="btn" value="Proceed" />
        </div>
    </div>
</t:form>

I try fill it with birthdate[Day] = 20, birthdate[Month] = 08, birthdate[Year] = 1992, and just debug it on backend like this:

@Property
@Persist(PersistenceConstants.FLASH)
private List<String> birthdate;

Object onSuccess() {
    logger.info("data birthdate:            "+birthdate);  // print null
    logger.info("data birthdate toString:   "+birthdate.toString());  // print null
    logger.info("data birthdate 0:          "+birthdate.get(0));  // print null

    return null;
}

All of them only returned null..

How to solve this?

Thank you in advance


Solution

  • Coming from a Spring and Hibernate era, Tapestry feels weird. Based on my 10 minutes of reading you are not mapping fields properly and missing a few things such as

    • You are not using t:id
    • You have 3 text fields in your .tml and then a list in your .java not sure what you are planning to do there

    As an example I did something as follow Names.java

    package com.raf.test.pages;
    
    import java.util.Date;
    
    import org.apache.tapestry5.PersistenceConstants;
    import org.apache.tapestry5.annotations.Persist;
    import org.apache.tapestry5.annotations.Property;
    import org.apache.tapestry5.ioc.annotations.Inject;
    import org.slf4j.Logger;
    
    public class Names {
        @Inject
        private Logger logger;
    
        @Property
        @Persist(PersistenceConstants.FLASH)
        private String birthDay;
    
        @Property
        @Persist(PersistenceConstants.FLASH)
        private String birthMonth;
    
        @Property
        @Persist(PersistenceConstants.FLASH)
        private String birthYear;
    
        @Property
        @Persist(PersistenceConstants.FLASH)
        private String aDate;
    
        @Property
        @Persist(PersistenceConstants.FLASH)
        private String monthAndYear;
    
        @Property
        @Persist(PersistenceConstants.FLASH)
        private Date actualDateField;
    
        Object onSuccess() {
           logger.info("birthDay: " + birthDay);
           logger.info("birthMonth: " + birthMonth);
           logger.info("birthYear: " + birthYear);
    
           if(aDate != null && !aDate.isEmpty()) {
               String[] chunks = aDate.split("-");
               if(chunks.length > 2) {
                   logger.info("aDate [Year]: " + chunks[0]);
                   logger.info("aDate [Month]: " + chunks[1]);
                   logger.info("aDate [Day]: " + chunks[2]);
               }
           }
    
           logger.info("monthAndYear: " + monthAndYear);
    
           logger.info("actualDateField: " + actualDateField);
    
           return null;
        }
    }
    

    and the Names.tml as follow

    <html t:type="layout" title="test com.example"
          xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd"
          xmlns:p="tapestry:parameter">
    
    <t:form t:id="names">
        <t:errors/>
        <div class="input-box">
            <t:textfield type="text" name="birthdate[Day]" placeholder="birthdate[Day]" t:id="birthDay"/>
            <t:textfield type="text" name="birthdate[Month]" placeholder="birthdate[Month]" t:id="birthMonth"/>
            <t:textfield type="text"  name="birthdate[Year]" placeholder="birthdate[Year]" t:id="birthYear"/>
            <!-- Uses html5 date type-->
            <t:textfield type="date"  name="normladate" placeholder="Normal date" t:id="aDate"/>
    
            <!-- Uses html5 month -->
            <t:textfield type="month"  name="justmonth" placeholder="Month Year" t:id="monthAndYear"/>
    
            <!-- Actual date field -->
            <t:datefield  name="actualDateField" placeholder="Actual date" t:id="actualDateField"/>
        </div>
        <div class="input-box">
            <div class="col-md-12">
                <input type="submit" name="proceed" class="btn" value="Proceed" />
            </div>
        </div>
    </t:form>
    </html>
    

    And here is sample output in eclipse

    [INFO] pages.Names birthDay: bb
    [INFO] pages.Names birthMonth: aa
    [INFO] pages.Names birthYear: 2nineteen
    [INFO] pages.Names aDate [Year]: 2018
    [INFO] pages.Names aDate [Month]: 02
    [INFO] pages.Names aDate [Day]: 18
    [INFO] pages.Names monthAndYear: 2018-02
    [INFO] pages.Names actualDateField: Fri Feb 16 00:00:00 EST 2018
    

    As you can see each field in Names.tml is mapped to corresponding field in Names.java POJO.