【问题标题】:Binding objects from spring multiple <form:select>从 spring 多个 <form:select> 绑定对象
【发布时间】:2014-04-04 07:44:07
【问题描述】:

不得不豆:

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

Where ${authors} - 包含来自数据库的作者对象的列表。使用 POST 请求。

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

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

当我在没有选择作者的情况下创建书籍时,但输入其他信息,一切都很好,书籍保存在数据库中。 When select some authors get this - The request sent by the client was syntactically incorrect.

【问题讨论】:

    标签: java spring spring-mvc model-view-controller collections


    【解决方案1】:

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

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

    附:我怎么了?在我在这里提问之前,我自己找不到正确的答案)

    【讨论】:

      【解决方案2】:

      您需要在控制器中实现 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;
                  }
                });
          }
      

      【讨论】:

      • 尝试添加这个,为我的例子更正。但结果相同The request sent by the client was syntactically incorrect
      • 您是否检查过您的 init binder 在您选择作者时被调用?
      猜你喜欢
      • 2013-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多