【问题标题】:Multiple Select in Spring 3.0 MVCSpring 3.0 MVC 中的多选
【发布时间】:2011-05-18 21:47:01
【问题描述】:

好吧,我一直在尝试在 Spring MVC 中完成多项选择,但没有运气。

基本上我拥有的是技能类:

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

还有一个拥有多种技能的员工:

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

所有这些都映射到 Hibernate,但这应该不是问题。

现在我希望在 JSP 中能够从 &lt;select multiple="true"&gt; 元素中为员工选择技能。

我在 JSP 中尝试过,但没有成功:

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

这是我的控制器:

@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";
    }
}

但这似乎不起作用。我得到以下异常:

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

我觉得应该有可能我们可以为模型提供一个表单,该表单可以从提供的选项列表中进行多项选择。在 Spring 3.0 MVC 中使用 form:selectform:options 的最佳做法是什么?

谢谢!

解决方案:

好的,以防万一有人想知道解决方案是什么。除了用户 01001111 修复:

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

我们需要给控制器添加一个CustomCollectionEditor,如下:

@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;
            }
          });
    }
}

这允许 Spring 在 JSP 和模型之间添加技能集。

【问题讨论】:

    标签: spring jsp select spring-mvc


    【解决方案1】:

    您需要将 items 属性视为变量,而不仅仅是引用变量名:

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

    ${skillOptionList}代替skillOptionList

    【讨论】:

    • 好的,这很有帮助。现在,我可以在我的 JSP 中看到 SkillOptionList,但从未调用过 setSkills,并且在我单击保存后,没有任何技能与我的 Employee 相关联。有什么想法吗?
    • 我想通了。如果您愿意,请将有关 CustomCollectionEditor 的部分添加到您的解决方案中,如我的帖子中所述,以防其他人遇到同样的问题。谢谢!
    【解决方案2】:

    您不需要自定义编辑器 - 这就是我所做的一切,它会正确地来回复制值:

    <form:select path="project.resources">
        <form:option value="XX"/>
        <form:option value="YY"/>
    </form:select>
    
    Project class:-
    private Set<String> resources;
    

    这就是我在控制器中添加数据的方式:

    Set<String> resources3 = new HashSet<String>();
    resources3.add("XX");
    

    【讨论】:

      【解决方案3】:

      我发现上述不起作用。 除了下面提到的事情,我还使用了答案:Spring select multiple tag and binding (只需覆盖等于和哈希码)。我还根据上面的评论更改了 initBinder

      我花了很多时间来应对它,所以我想给任何看起来相同并且遇到我遇到的问题的人一个提示。

      【讨论】:

        【解决方案4】:

        tkeE2036:我想知道你是怎么说它对你有用的?每个选项的值是“name”,而不是“id”。但是然后在 convertElement 方法中,您将接收到的元素(这是一个名称)视为 id。我的猜测是,如果你尝试将每个选项的值设置为“id”,你会得到一个空字符串作为每个选项的值,因为你使用了错误的 PropertyEditor。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-06-26
          • 2011-08-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-04-26
          相关资源
          最近更新 更多