PDA

View Full Version : Reference Data in <Integer, String>?


hezjing
Jun 7th, 2008, 02:47 AM
Hi

I have a command class that allows the user to enter an integer quantity value,
public class OrderCommand {
private int quantity;
// quantity in 6000 or 600
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}

Then, my controller creates and returns the reference data like the following,
protected Map referenceData(HttpServletRequest request) throws Exception {
Map<Integer, String> quantities = new LinkedHashMap<Integer, String);
quantities.put(new Integer(6000), "6000");
quantities.put(new Integer(600), "600");
Map<String, Object> m = new HashMap<String, Object>();
m.put("quantities", quantities);
return m;
}

Here is what i coded in FreeMarker template, to allow the user to select the quantity of 6000 or 600 from radio buttons,
<@spring.formRadioButtons "${orderCommand.quantity}", quantities, "" />

Then, I encounter the following error when the page is being loaded,
Expected number, sequence, or string. options evaluated instead to freemarker.template.SimpleHash on line 272, column 26 in spring.ftl.
The problematic instruction:
----------
==> ${options[value]?html} [on line 272, column 24 in spring.ftl]
in user-directive spring.formRadioButtons [on line 32, column 5 in library.ftl]
...

Do you know what FreeMarker is giving me this error?


This problem is resolved if my controller creates a reference data of <String, String> instead of <Integer, String>
protected Map referenceData(HttpServletRequest request) throws Exception {
Map<String, String> quantities = new LinkedHashMap<String, String);
quantities.put("6000", "6000");
quantities.put("600", "600");
Map<String, Object> m = new HashMap<String, Object>();
m.put("quantities", quantities);
return m;
}

hezjing
Jun 14th, 2008, 04:47 AM
Hi

Can someone explains why I cannot create a reference data quantities as Map<Integer, String> like the following?
protected Map referenceData(HttpServletRequest request) throws Exception {
Map<Integer, String> quantities = new LinkedHashMap<Integer, String);
quantities.put(new Integer(6000), "6000");
quantities.put(new Integer(600), "600");
Map<String, Object> m = new HashMap<String, Object>();
m.put("quantities", quantities);
return m;
}
I'm doing this because my command class has an int property which takes the value from the reference data (@spring.formRadioButtons),
public class OrderCommand {
private int quantity;
// quantity in 6000 or 600
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}

Please help, thank you!

hezjing
Jun 16th, 2008, 10:32 AM
The following is the formRadioButtons macro defined inn spring.ftl
<#macro formRadioButtons path options separator attributes="">
<@bind path/>
<#list options?keys as value>
<#assign id="${status.expression}${value_index}">
<input type="radio" id="${id}" name="${status.expression}" value="${value?html}"<#if stringStatusValue == value> checked="checked"</#if> ${attributes}<@closeTag/>
<label for="${id}">${options[value]?html}</label>${separator}
</#list>
</#macro>
I think my error is caused by the line <#if stringStatusValue == value>.
What is stringStatusValue? Is it a string?

What will happen if the value is an integer?

hezjing
Jun 17th, 2008, 11:03 AM
This problem is resolved and explained in The Map for formRadioButtons must be Map<String, String> (http://forum.springframework.org/showthread.php?t=56019)