spring mvc - RedirectAttributes changes object id between controllers -


I am using Spring MVC and Hibernate in my project. Also, I 2 controllers UserController and BookController where BookController users redirected to the page and I have a passing now object Along with it - together.

I have found that I can do this with redirect properties but the problem is that the id pass book user.id has been modified during this transition.

BookController.java

  public class BookController {@RequestMapping ( "/ users / {user_id} / or books / edit / {book_id} ") public string editBook (@PathVariable (" user_id ") integer user_id, @PathVariable (" book_id ") integer book_id, last RedirectAttributes redirectAttrs) {bookDetail = this .bookService.getBookById (book_id) Reedayrekts.adflasatrybut (" book details " Book Details); Println (bookDetail); Return "redirect: / users /" + user_id; }}  

Prints :. id = 8, title = Motylek, details = details, user_id = 2

UserController Java

  public class UserController {@RequestMapping ( "/ users / {id}") public string detailUser (@ModelAttribute ( "bookDetail") now bookDetail, @PathVariable ( "ID") Int ID, model) {user U = Yhkyussersewayr. JetUSARBID (ID); Model.addAttribute ("User", U); Model.addAttribute ("bookDetail", bookDetail); Println (bookDetail); Return "user"; }}  

Prints :. id = 2, title = Motylek, description = some details, user_id = 2

Do you have more idea or is this a bug? Thank you.

I think your book is in the class property ID , i.e. getId () and setId (..) does wallets to a missing or setter

When spring requested URL is called, he related to the < Store Segment declared in code> @RequestMapping . Therefore

  / your-app / users / 2  

and

  @RequestMapping ("/ users / {ID} ")  

This will

  id = 2  

form a request parameter.

This will examine different requests,

Servlet attributes for an entry with the name, name < Code> bookDetail . (If he has not found one, then he will make it one and add it to the request attributes.) In your case, it will find the object in the HttpSession . This will then force any request parameter for the mailing object properties. Since the above parameter is called id , it will be bound to the book property id .

You should be good by changing

  @RequestMapping ("/ users / {id}")  

to

<<> @ @ @ @ div>

Comments