Announcement Announcement Module
Collapse
No announcement yet.
mapping checkbox values to ArrayList Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • mapping checkbox values to ArrayList

    I am new to spring and I am writing a small application using spring mvc to retrieve the values of checkboxes. I have difficulties mapping the checkbox values into an ArrayList of objects. I know that is some "glue" missing but I can't figure out how to do it. I get the following error when I submit the form:

    "Field error in object 'commandBin' on field 'bins': rejected value [[Ljava.lang.String;@ed5d9d]; codes [typeMismatch.commandBin.bins,typeMismatch.bins,typ eMismatch.java.util.ArrayList,typeMismatch]; arguments [org.springframework.context.support.DefaultMessage SourceResolvable: codes [commandBin.bins,bins]; arguments []; default message [bins]]; default message [Failed to convert property value of type [java.lang.String[]] to required type [java.util.ArrayList] for property 'bins'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [java.lang.String[]] to required type [java.util.ArrayList] for property 'bins': no matching editors or conversion strategy found], commandBin=se.vgr.kiv.util.VfCommandBin@8e4805} and static attributes {}"

    Controller code:
    Code:
    public class UpdateBinsController extends SimpleFormController {
    	
    	protected Object formBackingObject(HttpServletRequest request) throws Exception {
    		VfCommandBin commandBinForm = (VfCommandBin) super.formBackingObject(request);
    
    		VastfolketUpdateBin[] binarray = VastfolketUpdateBin.values();
    		ArrayList<VFLetterGroup> binlist = new ArrayList<VFLetterGroup>(binarray.length);
    		
    		for(VastfolketUpdateBin bin : binarray) {
    			binlist.add(VastfolketUpdateBin.getVastfolketBinMembers(bin));
    		}
    		commandBinForm.setBins(binlist);
    		
    		return commandBinForm;
    	}
    
    	public ModelAndView onSubmit(Object command) throws ServletException {
    
    		VfCommandBin vfb = (VfCommandBin) command;
    
    		 List<VFLetterGroup> binlist = vfb.getBins();
    
    		for(VFLetterGroup bin : binlist) {
    				 System.out.println("Members:" + bin.getLetterGroupAsString());
    		 }
                     // Do some work
    		 return new ModelAndView(getSuccessView());l
    	}
    }

    the jsp looks like this:

    Code:
    <%@ page contentType="text/html" %>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
    
    <html>
    	<head><title>My page</title></head>
    	
    	<body>
            <h2><font color="#AA0000">First try</font></h2>
    
    	<form:form method="POST" commandName="commandBin">
    			<form:checkboxes items="${commandBin.bins}" path="bins" itemLabel="letterGroupAsString" itemValue="binName" />
    	<input type="submit"/>
    	</form:form>
    	</body>
    </html>

    the context configuration:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
    		<bean id="updateBinsController" class="se.vgr.controller.UpdateBinsController">
    			<property name="sessionForm" value="true"/>
    			<property name="commandName" value="commandBin"/>
    			<property name="commandClass" value="se.vgr.kiv.util.VfCommandBin"/>
    			<property name="formView" value="updateStartPage" />
    			<property name="successView" value="updateResult" />
    		</bean>
    		
    		<bean id="viewResolver"
    			class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    			<property name="prefix" value="/WEB-INF/jsp/" />
    			<property name="suffix" value=".jsp" />
    		</bean>
    
    		<bean id="urlMapping"
    		class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    			<property name="mappings">
    				<props>
    					<prop key="/updateBins.htm">updateBinsController</prop>
    				</props>
    			</property>
    		</bean>
    </beans>
    the command class:

    Code:
    import java.util.ArrayList;
    
    public class VfCommandBin {
    	private ArrayList<VFLetterGroup> bins;
    
    	public void setBins(ArrayList<VFLetterGroup> bins) {
    		this.bins = bins;
    	}
    
    	public ArrayList<VFLetterGroup> getBins() {
    		return bins;
    	}
    }

  • #2
    Using List instead of ArrayList and registering a PropertyEditor for the objects in the List solved the problem.

    Comment

    Working...
    X