2010-12-02 14 views
24

だから私はしばらくの間、複数の選択をSpring MVCで達成しようとしていて、運がなかった。これらのすべては、HibernateにマッピングされているSpring 3.0で複数選択MVC

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

が、それは問題ではありません。

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

と複数のスキルを持っている従業員:

基本的に私が持っているものは、スキルクラスであります。

今私はJSPで<select multiple="true">要素からスキルを選択することができるようにしたいと考えています。

ノー運と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を持つベストプラクティスは何ですか?

ありがとうございます!

ソリューション:

念のために誰もが解決策とは何か不思議ので、[OK]をクリックします。ユーザー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; 
      } 
      }); 
    } 
} 

これは、春にはJSPとモデルの間でスキルのセットを追加することができます。

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

ではなくskillOptionList

答えて

16

は、ちょうど変数名を参照しない、変数としての属性すべて?各オプションの値は "id"ではなく "name"です。しかし、convertElementメソッドでは、受け取った要素(名前)をIDのように扱います。私の推測では、各オプションの値を "id"として設定しようとすると、間違ったPropertyEditorを使用しているため、各オプションの値として空の文字列が得られます。

+0

そのように助けられました。 JSPでskillOptionListが表示されるようになりましたが、saveをクリックしてもsetSkillsは呼び出されず、Employeeにスキルは関連付けられません。何か案は? –

+2

私はそれを理解しました。あなたが望むなら、他の誰かが同じ問題を抱えている場合の私の投稿に記載されているように、あなたのソリューションにCustomCollectionEditorについての部分を追加してください。ありがとう! –

0

tkeE2036の${skillOptionList}を置く:私はあなたはそれがであなたのために働いたか疑問あなたがアイテムを処理するために必要

1

カスタムエディタを必要としない - これは私がすべてであり、それをコピー前後に正しく値:

<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"); 
0

上記は機能していません。 以下に挙げることに加えて、私は答えをSpring select multiple tag and binding で使用しました(等号とハッシュコードを単に置き換えてください)。私はまた、上記のコメントに基づいてinitBinderを変更しました

私はそれに対処するために多くの時間がかかりましたので、私は同じことを見て、私が会った問題を持つ人にヒントを与えると思った。