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 回答
0

您收到的错误消息很清楚:您必须设置一个属性编辑器或转换服务,以让活页夹知道如何从字符串中创建一个 AdminRole 对象(这实际上是您为每个复选框所拥有的)。

如果您检查您的 jsp 页面源代码,您可能会看到复选框表示为字符串数组(实际上,您会看到与复选框条目大小相同的字段名称多次)。当您发布表单时,活页夹会尝试创建一个 AdminRoles 集合,但如果没有适当的转换策略,它可以做的最好的事情是从数组中创建一个字符串集合。

于 2015-02-26T22:50:19.643 回答