Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3440820
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:28:40+00:00 2026-05-18T08:28:40+00:00

Ok so I’ve been trying to accomplish multiple selects in Spring MVC for a

  • 0

Ok so I’ve been trying to accomplish multiple selects in Spring MVC for a while and have had no luck.

Basically what I have is a Skill class:

public class Skill {
    private Long id;
    private String name;
    private String description;
    //Getters and Setters
}

And an Employee who has multiple Skills:

public class Employee {
    private Long id;
    private String firstname;
    private String lastname;
    private Set<Skill> skills;
    //Getters and Setters
}

All of these are mapped to Hibernate but that shouldn’t be an issue.

Now I would like to be able to do in the JSP is to select Skills for an Employee from a <select multiple="true"> element.

I have tried this in the JSP with no luck:

<form:select multiple="true" path="skills">
    <form:options items="skillOptionList" itemValue="name" itemLabel="name"/>
<form:select>

Here is my 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 this does not seem to work. I get the following Exception:

SEVERE: Servlet.service() for servlet jsp threw exception
javax.servlet.jsp.JspException: Type [java.lang.String] is not valid for option items

I feel like it should be possible where we can have a form for a Model that has multiple select from a list of options provided. What is the best practice to have form:select and form:options in Spring 3.0 MVC?

Thanks!

Solution:

Ok so just in case anyone wonders what the solution is. In addition to user 01001111 fix:

<form:select multiple="true" path="skills">
    <form:options items="${skillOptionList}" itemValue="name" itemLabel="name"/>
<form:select>

We need to add a CustomCollectionEditor to the controller as 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 the JSP 'element' will be a String
                    try{
                        id = Long.parseLong((String) element);
                    }
                    catch (NumberFormatException e) {
                        System.out.println("Element was " + ((String) element));
                        e.printStackTrace();
                    }
                }
                else if(element instanceof Long) {
                    //From the database 'element' will be a Long
                    id = (Long) element;
                }

                return id != null ? employeeService.loadSkillById(id) : null;
            }
          });
    }
}

This allows Spring to add Sets of Skills between the JSP and Model.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T08:28:41+00:00Added an answer on May 18, 2026 at 8:28 am

    You need to treat the items attribute as a variable, not just reference the variable name:

    <form:select multiple="true" path="skills">
        <form:options items="${skillOptionList}" itemValue="name" itemLabel="name"/>
    </form:select>
    

    put ${skillOptionList} instead of skillOptionList

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.