jsp - Multiple Select in Spring 3.0 MVC -


ok i've been trying accomplish multiple selects in spring mvc while , have had no luck.

basically have skill class:

public class skill {     private long id;     private string name;     private string description;     //getters , setters } 

and employee has multiple skills:

public class employee {     private long id;     private string firstname;     private string lastname;     private set<skill> skills;     //getters , setters } 

all of these mapped hibernate shouldn't issue.

now able in jsp select skills employee <select multiple="true"> element.

i have tried in jsp no luck:

<form:select multiple="true" path="skills">     <form:options items="skilloptionlist" itemvalue="name" itemlabel="name"/> <form:select> 

here controller:

@controller @sessionattributes public class employeecontroller {      @autowired      private employeeservice service;       @requestmapping(value="/addemployee", method = requestmethod.post)      public string addskill(@modelattribute("employee") employee emp, bindingresult result, map<string, object> map) {          employeeservice.addemployee(emp);          return "redirect:/indexemployee.html";     }      @requestmapping("/indexemployee")     public string listemployees(@requestparam(required=false) integer id, map<string, object> map) {          employee emp = (id == null ? new employee() : employeeservice.loadbyid(id));          map.put("employee", emp);         map.put("employeelist", employeeservice.listemployees());         map.put("skilloptionlist", skillservice.listskills());          return "emp";     } } 

but not seem work. following exception:

severe: servlet.service() servlet jsp threw exception javax.servlet.jsp.jspexception: type [java.lang.string] not valid option items 

i feel should possible can have form model has multiple select list of options provided. best practice have form:select , form:options in spring 3.0 mvc?

thanks!

solution:

ok in case wonders solution is. in addition user 01001111 fix:

<form:select multiple="true" path="skills">     <form:options items="${skilloptionlist}" itemvalue="name" itemlabel="name"/> <form:select> 

we need add customcollectioneditor controller follows:

@controller @sessionattributes public class employeecontroller {      @autowired     private employeeeservice employeeservice;      @autowired     private skillservice skillservice;      @initbinder     protected void initbinder(webdatabinder binder) {         binder.registercustomeditor(set.class, "skills", new customcollectioneditor(set.class)           {             @override             protected object convertelement(object element)             {                 long id = null;                  if(element instanceof string && !((string)element).equals("")){                     //from jsp 'element' string                     try{                         id = long.parselong((string) element);                     }                     catch (numberformatexception e) {                         system.out.println("element " + ((string) element));                         e.printstacktrace();                     }                 }                 else if(element instanceof long) {                     //from database 'element' long                     id = (long) element;                 }                  return id != null ? employeeservice.loadskillbyid(id) : null;             }           });     } } 

this allows spring add sets of skills between jsp , model.

you need treat items attribute variable, not reference variable name:

<form:select multiple="true" path="skills">     <form:options items="${skilloptionlist}" itemvalue="name" itemlabel="name"/> </form:select> 

put ${skilloptionlist} instead of skilloptionlist


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -