Announcement Announcement Module
Collapse
No announcement yet.
How to get data from a multiselect form ?!!!! Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to get data from a multiselect form ?!!!!

    I'm new to Spring, and i'm trying to get Data from a multiselect form. but i had this erreur :

    Failed to convert property value of type java.lang.String to required type java.util.Collection for property roles; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [fr.spring.model.ModelRole] for property roles[0]: no matching editors or conversion strategy found


    My code is :

    .jsp
    Code:
    <tr>
    	<td>Roles</td>
    	<td>
    		<form:select  path="roles" multiple="true" >
    			<c:forEach items="${roles}" var="role">
    				<form:option value="${role.id}" ><spring:message code="value.${role.role}"/></form:option>
    			</c:forEach>
    		</form:select>
    	</td>
    	<td><form:errors path="roles" class="form-input-error" /></td>
    </tr>

    Controller

    Code:
    	@RequestMapping(value = { "patient/add" }, method = RequestMethod.GET)
    	public String addPatientShowForm(@ModelAttribute("addpatient") FormPatient patient, Model m) {
    		patient.getRoles();
    		Collection<ModelPatient> patients = serviceCrud.listPatient();
    		Collection<ModelRole>    roles    = serviceCrud.getRoles(); 
    		m.addAttribute("patients", patients);
    		m.addAttribute("roles", roles);
    		return "crud/insert-patient-form";
    	}
    	
    	@RequestMapping(value = { "patient/add" }, method = RequestMethod.POST)
    	public String addPatientValidation(
    			@Valid @ModelAttribute("addpatient") FormPatient patient,
    			BindingResult br, Model m) {
    
    		Collection<ModelPatient> patients = serviceCrud.listPatient();
    		Collection<ModelRole>    roles    = serviceCrud.getRoles(); 
    		m.addAttribute("patients", patients);
    		m.addAttribute("roles", roles);
    		if (br.hasErrors()) {
    			m.addAttribute("message", "Error inserting patient ");
    		} else {
    			serviceCrud.addPatient(patient);
    			m.addAttribute("message","Insert successful of patient ");
    		}
    
    		return "crud/insert-patient-form";	
    	}

    Converter config
    Code:
    <!-- Converter -->
    	<bean id="conversionService"
          	class="org.springframework.context.support.ConversionServiceFactoryBean">
        	<property name="converters">
            	<list>
                	<bean class="fr.spring.converter.ConverterObjectToModelRole"/>
            	</list>
       		</property>
    	</bean>

    Converter class

    Code:
    package fr.spring.converter;
    
    import javax.annotation.Resource;
    import org.springframework.core.convert.converter.Converter;
    import fr.spring.dao.DaoInterfaceRole;
    import fr.spring.model.ModelRole;
    
    public class ConverterObjectToModelRole implements Converter<String, ModelRole> {
    
    	@Resource
    	private DaoInterfaceRole daoInterfaceRole;
    	
    	@Override
    	public ModelRole convert(String element) {
    		// TODO convert String to ModelRole
    		System.out.println("convert");
    		Integer id = Integer.parseInt(element);
    		return id != null ? daoInterfaceRole.load(id) : null;
    	}
    }
    Model
    Code:
    public class ModelUser {
    	
    	protected int id;
    	protected String username;
    	protected String password;
    	protected int enabled;
    	protected Collection <ModelRole> roles;
    	
    // getter + setter

  • #2
    Thank you, i will see this topic and let u know if it fix my problem or not

    Comment

    Working...
    X