Need help with binding Install with Spring MVC form

I have been trying for the last 3 days, but I can not solve my problem.

I have a Person class

@SuppressWarnings("rawtypes") @OneToMany(cascade = CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="person") @JoinColumn(name="person_id") public Set<Book> books = new HashSet<Book>(); class Book book_id person_id 

In my JSP form, I have

 <c:forEach items="${BookList}" var="var1" varStatus="counter"> <input type="checkbox" name="books[${counter.index}].book_id" value="${var1.book_id}" >${var1.book_name}</input> </c:forEach> 

I insert books into the table depending on the checkboxes. The list of books is populated using the refrenceData model.

CONTROLLER

 @RequestMapping(value = "/persons/add", method = RequestMethod.GET) public String getAdd(Model model) { logger.debug("Received request to show add page"); // Create new Person and add to model // This is the formBackingOBject model.addAttribute("personAttribute", new Person()); // This will resolve to /WEB-INF/jsp/addpage.jsp return "hibernate/addpage"; } @RequestMapping(value = "/persons/add", method = RequestMethod.POST) public String add(@Valid @ModelAttribute("personAttribute") Person person, BindingResult result) { logger.debug("Received request to add new person"); if (result.hasErrors()) return "hibernate/addpage"; else personService.add(person); // This will resolve to /WEB-INF/jsp/addedpage.jsp return "hibernate/addedpage"; } 

Now, if I have one Book object, then it works fine, and the data is entered into the database, but if I installed it, then it speaks of an invalid property book [1]

After a lot of searching on SO and Google I leart, I have two options

 PropertyEditor AutoPopulatingList 

I do not know how to use them in my case. Can someone help me, where should I use them and how to use it?

+4
source share
3 answers

Look at this question. Link objects in the Set collection.

You need to use a different type of collection. I would recommend using a List instead of a Map. When you submit from a form a parameter with a name such as:

 name="books[0].book_id" 

SpringMVC will search in the book property (which is Set for you) and then try to get the first element by executing books.get (0). A set has no access because Set has no order.

You can use an AutoPopulatingList to implement the list. This is an implementation of a lazy list that will create an object if it does not exist. For example, if you call books [0] .id and you did not add the book to position 0 of the list, it will throw a NullPointerException, but if you use AutoPopulatingList, it will create a new book and add it to this position if this position is empty.

 public List<Book> books = new AutoPopulatingList<Book>(new ElementFactory<Book>() { @Override public Book createElement(final int index) throws ElementInstantiationException { //call the constructor as you need return new Book(); } }); 

if you are going to create it with a standard book constructor (i.e. Book ()), you can use a syntax like this:

 public List<Book> books = new AutoPopulatingList<Book>(Book.class); 
+8
source

Binding a collection is not possible with Spring MVC, since collections do not have indexes to work with. Although you can iterate through a lot in JSP and show results. Solutions may be -

  • Use a different type of collection, such as List.
  • Wrap your set in POJO, use your Set to display its contained values ​​in the JSP. After you want to publish a form containing your selection, add a new property to the POJO, which is String (or similar), and provide this property as your PATH in the JSP tag that will receive the selection data from the JSP. Then, in the backend code, enter your set with this value.

In case your POJO is also an entity for creating a database using Hibernate, just put @Transient on top of it. Hibernate will ignore this property when creating the table.

+1
source

When I have such a complex form, I honestly prefer to use JSON and send it using AJAX.

 {"person":{"id":1,"books":[{"person_id":2,"book_id":3},{"person_id":2,"book_id":6},{"person_id":3,"book_id":4}]} @RequestMapping(value = "/persons/add", method = RequestMethod.POST) @ResponseBody public String add(@RequestBody Person person){ //ad your business logic } 

Your code will be checked using the de-serializer, and you can save it. You can learn more about this in this post:

http://blog.springsource.com/2010/01/25/ajax-simplifications-in-spring-3-0/

0
source

Source: https://habr.com/ru/post/1342531/


All Articles