Java 从spring multiple绑定对象<;表格:选择>;

Java 从spring multiple绑定对象<;表格:选择>;,java,spring,spring-mvc,model-view-controller,collections,Java,Spring,Spring Mvc,Model View Controller,Collections,必须: @Entity @Table(name="book") public class Book { @Id @Column(name="id_book") @GeneratedValue(generator="increment") @GenericGenerator(name="increment", strategy="increment") private int id;

必须:

    @Entity
    @Table(name="book")
    public class Book {
        @Id
        @Column(name="id_book")
        @GeneratedValue(generator="increment")
        @GenericGenerator(name="increment", strategy="increment")
        private int id;

        @Column
        @Size(min=1,max=100)
        private String title;

        @Column 
        @Size(min=1,max=400)
        private String description;


        @Column
        private Integer year=0;

        @ManyToMany(cascade={CascadeType.ALL},fetch = FetchType.EAGER)
        @Fetch (FetchMode.SELECT)
        @JoinTable(name="book_author", 
                 joinColumns={@JoinColumn(name="book_id_book")}, 
                     inverseJoinColumns= {@JoinColumn(name="author_id_author")})
        private List<Author> author=new ArrayList<Author>();

       //getters/setters
   }

当我在没有选择作者的情况下创建图书,但输入另一个信息时,一切正常,图书保存在数据库中。当select some authors获得此消息时-
客户端发送的请求在语法上不正确。

您需要在控制器中实现initBinder,下面可能是暂定代码(未测试)


您需要在控制器中实现initBinder,下面是暂定代码(未测试)


外接程序控制器解决的问题:

@InitBinder
protected void initBinder(WebDataBinder binder){
    binder.registerCustomEditor(Author.class, new Editor(hibarnateService));
}
并创建类:

public class Editor extends PropertyEditorSupport {

    private final Dao hibernateService;

    public Editor(Dao hibernateService){
        this.hibernateService=hibernateService;
    }

    @Override
    public void setAsText(String text) throws IllegalArgumentException{
        Author author=hibernateService.getAuthor(Integer.parseInt(text));
        setValue(author);
    }
}

还有,我怎么了?我自己找不到正确的答案,直到我在这里提问)

外接程序控制器解决的问题:

@InitBinder
protected void initBinder(WebDataBinder binder){
    binder.registerCustomEditor(Author.class, new Editor(hibarnateService));
}
并创建类:

public class Editor extends PropertyEditorSupport {

    private final Dao hibernateService;

    public Editor(Dao hibernateService){
        this.hibernateService=hibernateService;
    }

    @Override
    public void setAsText(String text) throws IllegalArgumentException{
        Author author=hibernateService.getAuthor(Integer.parseInt(text));
        setValue(author);
    }
}

还有,我怎么了?我自己找不到正确的答案,除非我在这里提问)

尝试添加此选项,并针对我的示例进行了更正。但是有相同的结果
客户端发送的请求语法不正确
您是否检查了您的init binder在选择author时是否被调用?请尝试添加此内容,并针对我的示例进行了更正。但是有相同的结果
客户端发送的请求在语法上不正确
您是否检查了您的init binder在选择author时是否被调用?
@InitBinder
protected void initBinder(WebDataBinder binder){
    binder.registerCustomEditor(Author.class, new Editor(hibarnateService));
}
public class Editor extends PropertyEditorSupport {

    private final Dao hibernateService;

    public Editor(Dao hibernateService){
        this.hibernateService=hibernateService;
    }

    @Override
    public void setAsText(String text) throws IllegalArgumentException{
        Author author=hibernateService.getAuthor(Integer.parseInt(text));
        setValue(author);
    }
}