从弹簧多个<form:select> </form:select>绑定对象

时间:2014-04-04 07:44:07

标签: 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;

        @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
   }

@Entity
@Table(name="author")
public class Author {

    @Id
    @Column(name="id_author")
    @GeneratedValue
    private Integer id;


@Column
private String name;


@Column
private String surname;

@ManyToMany(mappedBy="author")
private Set<Book> book=new HashSet<Book>();

    //getters/setters
}

在我的jsp中,我有输入数据的表单,以及来自DB的选择作者的多个列表,仅在选择的作者中有问题,因此只提供此代码:

<sf:select multiple="true" path="author" items="${authors}" size="7" >
</sf:select>

其中$ {authors} - 列出对象来自DB的作者。使用POST请求。

在我的本页控制器中有这个(我知道它不正确):

@RequestMapping(value="/addbook", method=RequestMethod.POST)
    public String addBook(Book book){
        hibarnateService.saveBook(book);
        return "redirect:/books";
    }

当我创建没有选择作者的书籍,但输入另一个信息时,一切都很好,书籍保存在DB中。当选择一些作者得到这个 - The request sent by the client was syntactically incorrect.

2 个答案:

答案 0 :(得分:3)

通过添加控制器解决问题:

@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);
    }
}

P.S。我怎么了?在我问这里之前,我找不到合适的答案。

答案 1 :(得分:1)

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

@InitBinder
    protected void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(List.class, "authors ", new CustomCollectionEditor(List.class)
          {
            @Override
            protected Object convertElement(Object element)
            {
                Long id = null;

                if(element instanceof Long) {
                    //From the database 'element' will be a Long
                    id = (Long) element;
                }

                return id != null ? authorService.loadAuthorById(id) : null;
            }
          });
    }