16

I have a jstl loop and I want to format the date of a form:input. I have tried many permutations of some of the suggestions that I've fond online but I just cannot get it to work.. Can someone please have a look?

I've included the full loop for context but the problem lies in the last <td></td> block.

<c:forEach items="${valueTransactionsModel.transactions}" var="transaction" varStatus="loopStatus">

                        <tr class="${loopStatus.index % 2 == 0 ? 'even' : 'odd'}">
                            <spring:message code="valueTransactions.transactionType" var="transactionTypeLbl" />
                            <tags:dropdown id="transactionTypeId${loopStatus.index}" path="transactions['${loopStatus.index}'].valueTransactionType.id" 
                                fieldName="${transactionTypeLbl}" classStyle="mandatory" items="${transactionTypes}" itemLabel="value"/>
                        </tr>
                        <tr class="${loopStatus.index % 2 == 0 ? 'even' : 'odd'}">
                            <spring:message code="valueTransactions.transactionAmount" var="valueTransactionAmountLbl${loopStatus.index}" />
                            <tags:input id="transactionAmountId${loopStatus.index}" 
                                path="transactions['${loopStatus.index}'].valueTransactionAmount"
                                fieldName="valueTransactionAmountLbl${loopStatus.index}"
                                maxlength="30" classStyle="mandatory" />
                            <spring:message code="valueTransactions.transactionDate"
                                var="valueTransactionDateLbl${loopStatus.index}" />
                            <td>
                                <form:input type="text" path="transactions['${loopStatus.index}'].valueTransactionDate" cssClass="mandatory" value="<fmt:formatDate value="transactions['${loopStatus.index}'].valueTransactionDate"  type="date" pattern="yyyy-MM-dd"/>" />
                            </td>
                        </tr> 


            </c:forEach>

My most recent issue is:

JSPG0055E: Unable to create an xml attribute from name [transactions[] value [${loopStatus.index}]

1
  • 1
    Yes!!! Thank you for forming this question correctly! 2 days of this issue.....
    – tom
    Commented May 8 at 10:40

2 Answers 2

23

You can't use a JSP tag in an attribute of another JSP tag. STore the result of the date formatting in a page attribute, and use this page attribute (as you would do with a variable in Java):

<fmt:formatDate value="transactions['${loopStatus.index}'].valueTransactionDate"  
                type="date" 
                pattern="yyyy-MM-dd"
                var="theFormattedDate" />
<form:input type="text" path="..." value="${theFormattedDate}"/>
2
  • 2
    Thanks! That pretty much did it. I just had to reference the model in the value attribute value="${valueTransactionsModel.transactions[loopStatus.index].valueTransactionDate}" Commented Jul 31, 2012 at 11:07
  • Thanks many years later! After two days, this was my solution. I really appreciate it! I am using latest Spring 6 but I could not get any other solution to work for editable currency with Spring MVC. Also note, property editor does not get called if the input has standard formatting for BigDecimal, e.g. 10.0000. It has to be something like $10.00 for the editor to get invoked!!!!!
    – tom
    Commented May 8 at 10:42
9

In the latest releases of the spring JSTL tags you can't use the attribute value into de tah form:input. If you try, you will get a value attribute is not valid for tag <form:input> error.

In this case you must use a normal HTML input tag and put the path as the name of the input to trigger the binding to the Spring form like this

<fmt:formatDate var="fmtDate" value="${form.bean.dateProperty}" pattern="dd/MM/yyyy"/>
<input type="text" name="bean.dateProperty" value="${fmtDate}"/>

Ugly but works for me! ;)

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.