Search code examples
thymeleafspring-roo

Spring Roo Detail Object


I'm using Spring roo v2.0. I created a simple app with two entities: Library and Book. A Library can have more books so I created a relashionship one-to-many. I used the following command to create the project:

project setup --topLevelPackage com.library 
jpa setup --provider HIBERNATE --database HYPERSONIC_IN_MEMORY 
entity jpa --class ~.Library 
field string --fieldName identifier  --notNull
entity jpa --class ~.Book  
field string --fieldName identifier  --notNull
focus --class ~.Library
field set --fieldName book --type ~.Book --mappedBy library --notNull false --cardinality ONE_TO_MANY
repository jpa --all --package ~.repository
service --all --apiPackage ~.service.api --implPackage ~.service.impl 
web mvc setup
web mvc view setup --type THYMELEAF
web mvc controller --all --responseType JSON
web mvc controller --all --responseType THYMELEAF
web mvc detail --all --responseType THYMELEAF

My problem.. When I edit/show a library detail I'd want to show its books as happens in the list librabry page after select one.

And then... in this form I'd want to have an Add button that create a book for that library, not a generic book.

How can I do it? Thanks in advice


Solution

  • You should customize manually the show.html view related with the Library entity to include a table with that information.

    To do that, you could include in the model of the method that serves the show page the complete list of books using a findBooksByLibrary method. Something like this:

    @Override
    @GetMapping(name = "show")
    public ResponseEntity<Library> show(@ModelAttribute Library library, Model model) {
       model.addAttribute("books", getBookService().findByLibrary(library));
       return new ModelAndView("libraries/show");
    }
    

    After that, edit the show.html page including a table element that shows the previous related books:

    <table data-th-each="item : ${books}">
     <tr>
       <td data-th-text="${item.identifier}"></td>
     </tr>
    </table>
    

    NOTE: Remember that you could use Datatables JQuery plugin to give extra features to the table above.

    Hope it helps