2011-09-14 5 views
8

フォームがチェックボックスコントロールにバインドされているように見えません。私はここで多くの記事を読んで、いくつかのテクニックを試しましたが運はありませんでした。たぶん新鮮な目が助けになるでしょう。春のバインディングリスト<Object>からフォーム:チェックボックス

私のコントローラ:

public String editAccount(@RequestParam("id") String id, Model model) { 
    model.addAttribute("account", accountService.getAccount(id)); 
    model.addAttribute("allRoles", roleService.getRoles()); 
    return EDIT_ACCOUNT; 
} 

私のjsp:

<form:form action="" modelAttribute="account"> 
<form:checkboxes items="${allRoles}" path="roles" itemLabel="name" itemValue="id" delimiter="<br/>"/> 
</form> 

生成されたHTML:

<span><input id="roles1" name="roles" type="checkbox" value="1"/><label for="roles1">User</label></span><span><br/><input id="roles2" name="roles" type="checkbox" value="2"/><label for="roles2">Admin</label></span><span><br/><input id="roles3" name="roles" type="checkbox" value="3"/><label for="roles3">SuperAdmin</label></span<input type="hidden" name="_roles" value="on"/> 

私はそれを確認する(図示せず)各ループのために第二の使用モデルオブジェクトにはロールが含まれていました。それでも、チェックボックスはチェックされていませんし、私はロールオブジェクトを提出するときは常に空です。誰かが私に行方不明を教えてもらえますか?

おかげ

EDIT

申し訳ありませんがちょうどアカウントと役割のオブジェクトを参照すると便利かもしれません実現:

public class Account { 

    private String username, firstName, lastName, email; 
    private List<Role> roles; 

    @NotNull 
    @Size(min = 1, max = 50) 
    public String getUsername() { 
     return username; 
    } 

    public void setUsername(String username) { 
     this.username = username; 
    } 

    @NotNull 
    @Size(min = 1, max = 50) 
    public String getFirstName() { 
     return firstName; 
    } 

    public void setFirstName(String firstName) { 
     this.firstName = firstName; 
    } 

    @NotNull 
    @Size(min = 1, max = 50) 
    public String getLastName() { 
     return lastName; 
    } 

    public void setLastName(String lastName) { 
     this.lastName = lastName; 
    } 

    @NotNull 
    @Size(min = 6, max = 50) 
    public String getEmail() { 
     return email; 
    } 

    public void setEmail(String email) { 
     this.email = email; 
    } 

    public List<Role> getRoles() { 
     return roles; 
    } 

    public void setRoles(List<Role> roles) { 
     this.roles = roles; 
    } 

    public String toString() { 
     return ReflectionToStringBuilder.toString(this); 
    } 

} 

public class Role { 

private int id; 
private String name; 

public Role() {} 

public Role(int id, String name) { 
    this.id = id; 
    this.name = name; 
} 

public int getId() { 
    return id; 
} 

public void setId(int id) { 
    this.id = id; 
} 

@NotNull 
@Size(min = 1, max = 50) 
public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

}

EDIT#2

コントローラー投稿方法

@RequestMapping(value = "edit", method = RequestMethod.POST) 
public String updateAccount(@RequestParam("id") String id, @ModelAttribute("account") @Valid AccountEditForm form, BindingResult result) { 
    System.out.println("FORM VALUES AFTER: " + form); 
    return (result.hasErrors() ? EDIT_ACCOUNT : ACCOUNT_REDIRECT); 
} 

AccountEditFormはフォームバッキングオブジェクトです。 GETを実行すると、Accountオブジェクトを取得し、その値をAccountEditFormに渡してから、画面を表示します。明快にするためにAccountEditFormを添付します。アカウントオブジェクトと非常に似ています。私はちょうど私のモデルオブジェクトから私のフォームオブジェクトを分離するために起こった。

public class AccountEditForm { 

    private String username, firstName, lastName, email; 
    private List<Role> roles = new ArrayList<Role>(); 

    @NotNull 
    @Size(min = 1, max = 50) 
    public String getUsername() { 
     return username; 
    } 

    public void setUsername(String username) { 
     this.username = username; 
    } 

    @NotNull 
    @Size(min = 1, max = 50) 
    public String getFirstName() { 
     return firstName; 
    } 

    public void setFirstName(String firstName) { 
     this.firstName = firstName; 
    } 

    @NotNull 
    @Size(min = 1, max = 50) 
    public String getLastName() { 
     return lastName; 
    } 

    public void setLastName(String lastName) { 
     this.lastName = lastName; 
    } 

    @NotNull 
    @Size(min = 6, max = 50) 
    public String getEmail() { 
     return email; 
    } 

    public void setEmail(String email) { 
     this.email = email; 
    } 

    public List<Role> getRoles() { 
     return roles; 
    } 

    public void setRoles(List<Role> roles) { 
     this.roles = roles; 
    } 

    public String toString() { 
     return ReflectionToStringBuilder.toString(this); 
    } 

} 

EDIT#3役割プロパティエディタ私のコントローラで定義され

public class RolePropertyEditor extends PropertyEditorSupport { 

    private Map<Integer, Role> roleMap = new HashMap<Integer, Role>(); 

    public RolePropertyEditor(List<Role> roleList) { 
     for (Role r : roleList) roleMap.put(r.getId(), r); 
    } 

    public void setAsText(String incomingId) { 
     Role role = roleMap.get(incomingId); 
     System.out.println("PROPERTY EDITOR ROLE " + role); 
     setValue(role); 
    } 

    public String getAsText() { 
     System.out.println("PROPERTY EDITOR ID " + ((Role)getValue()).getId()); 
     return String.valueOf(((Role)getValue()).getId()); 
    } 
} 

のような:

@InitBinder 
public void initBinder(WebDataBinder binder) { 
    binder.setAllowedFields(new String[] { 
      "username", "password", "confirmPassword", "firstName", "lastName", "email", "acceptTerms", "currentPassword" 
    }); 
    binder.registerCustomEditor(Role.class, new RolePropertyEditor(roleService.getRoles())); 
} 

EDIT#4 NEW ProeprtyEditor

public class SecurityRolePropertyEditor extends PropertyEditorSupport { 

    private RoleService roleService; 

    public SecurityRolePropertyEditor(RoleService roleService) { 
     this.roleService = roleService; 
    } 

    public void setAsText(final String name) { 
     Role role = roleService.getRoleByName(name); 
     setValue(role); 
    } 
} 

答えて

3

にequalsメソッドを追加します。あなたのロールエンティティ。

これを参照してくださいanswer (Spring MVC Pre Populate Checkboxes):詳細については同様の質問です。

+0

お返事ありがとうございます。私はRoleオブジェクトに等価を追加しました。スクリーンを見ると、チェックボックスには正確な値が設定されますが、変更や送信を行うと、フォームにバインドされたアカウントオブジェクトのrolesプロパティは常に空になります。 – blong824

+0

@ blong824 Roleオブジェクトがサブミットされず、Accountオブジェクトに移入されない、または保存されないという問題がありますか? – Ralph

+0

保存するすべてのコードを削除し、postステートメントにprintステートメントを追加しました。 rolesオブジェクトは常に空です。したがって、Accountオブジェクトには埋め込まれません。 – blong824

0

この問題は、パス内の複雑なオブジェクトのリストを使用していることがわかりました。 comboxは「値」フィールドにあるものだけを提出するようです。 私は、Listに代わってpathにバインドされた変数をListに変更し、そのリストにIDが入力されていることを確認しました。

0

あなたのエンティティに実装equalshashCodeメソッドがありません。

関連する問題