Announcement Announcement Module
Collapse
No announcement yet.
Help using formCheckboxes FreeMarker macro in Spring MVC Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Help using formCheckboxes FreeMarker macro in Spring MVC

    I am using the following macro in my FTL code:

    <@spring.formCheckboxes "location.color", colorMap, "<br>", 'id="color"'/>

    The colorMap is a TreeMap that is created in referenceData as follows:

    protected Map referenceData(HttpServletRequest request) throws Exception {
    Map m = new HashMap();

    Map colorMap= new TreeMap();
    colorMap.put("red", "Red");
    colorMap.put("yellow", "Yellow");
    m.put("colorMap", colorMap);

    return m;
    }

    However before this page loads I get the following error:

    javax.servlet.ServletException: Expected collection or sequence. list evaluated instead to freemarker.template.SimpleScalar on line 286, column 16 in spring.ftl.

    According to the documentation, formCheckBoxes uses a map just like formSingleSelect . Am I doing something wrong? Thanks for your help.

    -Nalin Mittal

  • #2
    Hi Nalin,

    I have the exact same error. Did you ever sort out what was doing this?

    Cheers,
    Deakin

    Comment


    • #3
      re:Help using formCheckboxes FreeMarker macro in Spring MVC

      I was having a similar problem, except with the freemarker macro formSingleSelect.

      Code:
      <@spring.formSingleSelect 'purchase.fundType', fundtypes />
      My referenceData method created a hashmap and passed it back to the freemarker page like so:

      Code:
      protected Map referenceData(HttpServletRequest request, int page) throws ServletException {
      
      		 
      		  Map fundTypes = new  HashMap();
      		  fundTypes.put("noyear", "No Year Funds");
      		  fundTypes.put("multiyear", "Multi Year Funds");
      		  fundTypes.put("annual", "Annual Funds"); 
      		  fundTypes.put("naf", "Non-Appropriated Funds (Same as NAF)");
      		  
      		  Map m = new HashMap();
      		  m.put("fundtypes", fundTypes);
      		 
      		  return m;
      		}
      However when trying to load the freemarker template page I was getting the same error:
      javax.servlet.ServletException: Expected collection or sequence. list evaluated instead to freemarker.template.SimpleScalar

      It seems that it is a configuration issue, with how to setup freemarker to work with Maps, see:
      http://www.nabble.com/Preserving-ord...d15211401.html

      I couldn't figure out an easy way to change the xml configuration for the spring servlet with just a properties setting, so I extended the FreeMarkerConfigurer to do some post processing to set an objectWrapper with the simpleMapWrapper property to true.


      Code:
      public class FreemarkerConfiguration extends
      		org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer {
      	freemarker.ext.beans.BeansWrapper objectWrapper;
      
      	public freemarker.ext.beans.BeansWrapper getObjectWrapper() {
      		return objectWrapper;
      	}
      
      	public void setObjectWrapper(freemarker.ext.beans.BeansWrapper objectWrapper) {
      		this.objectWrapper = objectWrapper;
      	}
      
      	/**
      	 * After configuration we change the bean wrapper to our custom one that
      	 * will allow us set the simpleMapWrapper to true to enable us to pass
      	 * hashmaps properly to freemarker without it complaining about extra
      	 * methods : see : http://freemarker.org/docs/app_faq.html#faq_simple_map
      	 * for an explanation of what we are trying to do.
      	 */
      	protected void postProcessConfiguration(Configuration config)
      			throws IOException, TemplateException {
      		config.setObjectWrapper(this.getObjectWrapper());
      	}
      }
      Spring Servlet configuration of freemarker:
      Code:
      <bean id="freemarkerConfig"
      		class="org.datastarve.sample.web.FreemarkerConfiguration">
      		<property name="objectWrapper">
      			<bean class="freemarker.ext.beans.BeansWrapper">
      				<property name="simpleMapWrapper" value="true" />
      			</bean>
      		</property>
      		<property name="templateLoaderPath" value="/freemarker/" />
      		<property name="freemarkerSettings">
      			<props>
      				<prop key="url_escaping_charset">UTF-8</prop>
      			</props>
      		</property>
      	</bean>
      After that I could then use freemarker macro formSingleSelect with a hashmap passed from my referenceData method. I suspect that your problem was similar to this?

      Comment


      • #4
        Thanks Rel for your code on Objectwrapper

        I was searching on how to set the Objectwrapper and your code helped me a lot !

        Thanks once again!

        Comment

        Working...
        X