Reputation: 2039
My question boils down to: How you do get spring mvc to call @ModelAttribute
after it has already been populated?
To elaborate, I'm messing around with the BookCatalog example from chapter 8 from Portlets in Action
by Ashish Sarin (Which is really good) and I've run into a problem where the method annotated with
@ModelAttribute
is only called once even upon successive loads. For instance:
I've added sysouts to the begining of each method to see what is being called where. The results are as follows:
showBooks
getBook!
initBinder
showEditBookForm
showBooks
initBinder <- Where is getBook!?
showEditBookForm
I read somewhere that @ModelAttribute
methods are called whenever @ModelAttribute
parameters are requested. So I tried adding @ModelAttribute("book") Book book
to the showEditBookForm but that still doesn't seem to trigger the @ModelAttribute
method.
I'm using Spring 3.1.3.RELEASE and deploying with maven to Liferay 6.1.1.
Any assistance would be appreciated!
EditBookController:
@Controller
@RequestMapping(value="VIEW")
@SessionAttributes({"book"})
public class EditBookController {
...
@RenderMapping(params="myaction=editBookForm")
public String showEditBookForm(@ModelAttribute("book") Book book) {
System.out.println("showEditBookForm");
return "editBookForm"
}
@InitBinder("book")
public void initBinder(WebDataBinder binder) {
System.out.println("initBinder");
binder.registerCustomEditor(Long.class, new LongNumberPropertyEditor());
binder.setDisallowedFields(new String[] {"isbnNumber"});
}
@ModelAttribute("book")
public Book getBook(@RequestParam Long isbnNumber) {
System.out.println("getBook!");
return bookService.getBook(isbnNumber);
}
...
}
BookController:
@Controller
@RequestMapping(value = "VIEW")
public class BookController {
...
@RenderMapping
public String showBooks(RenderResponse response,SessionStatus sessionStatus) {
System.out.println("showBooks");
return "home";
}
...
}
Upvotes: 2
Views: 2700
Reputation: 2039
Just to illustrate what I eventually landed on:
@RenderMapping(params="myaction=editBookForm")
public String showEditBookForm(
@ModelAttribute("book") Book book
, @RequestParam Long isbnNumber
, SessionStatus sessionStatus) {
System.out.println("showEditBookForm");
if(book != null
&& book.getIsbnNumber() != isbnNumber) {
sessionStatus.setComplete();
}
return "editBookForm";
}
Upvotes: 0
Reputation: 7243
According to this line of code:
@SessionAttributes({"book"})
public class EditBookController {
You're using the "book" attribute as a Session Attribute. This implies that the first time a Request is made to the Portal, the framework will verify an attribute with that name is present in Portlet Session. If it is not, it will call the corresponding @ModelAttribute
method to generate an instance and put it into Session. But if there's already an attribute in Session with that name, the Controller will use the existing object.
So, the first time you're invoking the Book Catalog Portlet an instance of the Book Attribute is generated and stored in Session. But the second time -as an object is already in Session- the Portlet will use the object generated in the first request. If you want to clear every object stored in Session after a render or action request you just need to call SessionStatus.setComplete()
to trigger a Session cleanup.
That's what theory and the book said, but there's some buggy behaviour in SessionStatus.setComplete()
and the Book Author has some thoghts about it.
Upvotes: 3