0

私は次のクラスを持っています:

public class TerminalAdmin {

    ....
    private Set<AdminRole> adminRoles;
}

この:

public class AdminRole {
    ....
    private Long adminId;       
    private String role;
}

コントローラ:

model.addAttribute("adminRoles",terminalAdminService.findAllAdminRoles());
model.addAttribute("newAdmin",new TerminalAdmin());
return "admin/adminUsers";

jsp:

<form:form modelAttribute="newAdmin" action="/admin/addNewAdmin">            
    <div class="line">
            <label for="">role</label>
            <form:checkboxes items="${adminRoles}" path="adminRoles"/>
            <div class="clear"></div>
     </div>
     <div class="line">
            <input class="btn" type="submit" value="save"/>
            <div class="clear"></div>
     </div>
</form:form>

foem を送信すると、次のメッセージが表示されます。

HTTP Status 400 -

type Status report

message

description The request sent by the client was syntactically incorrect.

春のログ:

Field error in object 'terminalAdmin' on field 'adminRoles': rejected value [ROLE_IMAGE_MODERATOR]; codes [typeMismatch.terminalAdmin.adminRoles,typeMismatch.adminRoles,typeMismatch.java.util.Set,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [terminalAdmin.adminRoles,adminRoles]; arguments []; default message [adminRoles]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Set' for property 'adminRoles'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.terminal.domain.admin.AdminRole] for property 'adminRoles[0]': no matching editors or conversion strategy found]
DEBUG: org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver - Resolving exception from handler [public java.lang.String com.terminal.controller.admin.AdminController.adminUsers(com.terminal.domain.admin.TerminalAdmin)]: org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'terminalAdmin' on field 'adminRoles': rejected value [ROLE_IMAGE_MODERATOR]; codes [typeMismatch.terminalAdmin.adminRoles,typeMismatch.adminRoles,typeMismatch.java.util.Set,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [terminalAdmin.adminRoles,adminRoles]; arguments []; default message [adminRoles]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Set' for property 'adminRoles'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.terminal.domain.admin.AdminRole] for property 'adminRoles[0]': no matching editors or conversion strategy found]
DEBUG: org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Resolving exception from handler [public java.lang.String com.terminal.controller.admin.AdminController.adminUsers(com.terminal.domain.admin.TerminalAdmin)]: org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'terminalAdmin' on field 'adminRoles': rejected value [ROLE_IMAGE_MODERATOR]; codes [typeMismatch.terminalAdmin.adminRoles,typeMismatch.adminRoles,typeMismatch.java.util.Set,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [terminalAdmin.adminRoles,adminRoles]; arguments []; default message [adminRoles]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Set' for property 'adminRoles'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.terminal.domain.admin.AdminRole] for property 'adminRoles[0]': no matching editors or conversion strategy found]

次のエラーを回避するためにコードを書き直す方法は?

4

2 に答える 2

0

次のコードを使用して問題を解決しました。

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(AdminRole.class,  new PropertyEditorSupport() {
            public void setAsText(String name) {
                AdminRole adminRole = terminalAdminService.findRoleByName(name);
                setValue(adminRole);
            }
        });
    }
于 2015-02-26T23:01:41.720 に答える