You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cocoon.apache.org by Jeremy Quinn <je...@media.demon.co.uk> on 2003/08/08 21:46:52 UTC

accessing a HashMap from JXTemplate

Hi All,

In my model, validation errors on a FormBean are added to a HashMap 
that is a property of that FormBean.

ie.

if (getName() == null || "".equals (getName()) || DEFAULT_NAME.equals 
(getName ())) {
	mErrors.put (Constants.NAME_FIELD, "Please enter a proper name");
}

I then expected to be able to extract these errors from JXTemplate like 
this:

<t:if test="#{form/errors[constants/NAME_FIELD] != ''}">
	<span style="color:red">#{form/errors[constants/NAME_FIELD]}</span>
</t:if>

but it does not output anything, so I tried this :

<t:forEach items="#{form/errors}">
	<tr valign="top">
		<td>#{name()}</td>
		<td>#{.}</td>
	</tr>
</t:forEach>

but got unexpected results :

<tr valign="top">
	<td>{name=Please enter a proper name}</td>
	<td>{name=Please enter a proper name}</td>
</tr>


Anyone work out what I am doing wrong?

thanks for any help

regards Jeremy


RE: accessing a HashMap from JXTemplate

Posted by Hugo Burm <hu...@xs4all.nl>.

It took me some time to find out how you can iterate over a map in jxpath:

   <c:forEach  select="#{values(/units)}">
      #{./id} <br/>
   </c:forEach>


/units is a map of unit objects.
You can call a java function on a java object by giving the instance as the
first parameter. So I am calling the java values() function on my /units map
which creates a collection of its values.
#{./id} prints the id field of each of my unit objects.

The jexl syntax works without conversion:

   <c:forEach  var="item" items="${mapU}">
	${item.getId()} <br/>
   </c:forEach>


Hugo

> -----Original Message-----
> From: Jeremy Quinn [mailto:jeremy@media.demon.co.uk]
> Sent: Friday, August 08, 2003 9:47 PM
> To: dev@cocoon.apache.org
> Subject: accessing a HashMap from JXTemplate
>
>

> but it does not output anything, so I tried this :
>
> <t:forEach items="#{form/errors}">
> 	<tr valign="top">
> 		<td>#{name()}</td>
> 		<td>#{.}</td>
> 	</tr>
> </t:forEach>
>
> but got unexpected results :
>
>