Spring Roo Detail Object - thymeleaf

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

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

Related

ASP.NET MVC to PHP Laravel Migration: Html.RenderAction Alternative

I'm currently developing a CMS using PHP Laravel (5.4) based on an existing ASP.NET MVC version I've made in the past. Since most of the ASP.NET version is written in JS, I'm trying to reuse the most I can in the newer Laravel version I'm developing. I have already translated most of the C# code to PHP, thanks to Laravel's similarities with ASP.NET MVC architecture, this was somewhat straightforward.
Currently I'm facing a issue trying to call a controller action from within a view that will in-turn render a partial view. In the ASP.NET MVC version I've accomplished this using a Html.RenderAction helper.
ASP.NET MVC Example:
<div>
#{Html.RenderAction("Controller", "Action");}
</div>
I want know if there is any alternative to the Html.RenderAction helper that I can use to accomplish this task.
I've search the interwebs and Laravel's documentation, and from what I was able to find, View Composers seem to be closest alternative. Unfortunately, I didn't find any example that could resolve my issue.
#Farrukh suggested using View Composers and it does actually work as intended. The problem is that I will need to add a View Composer to the AppServiceProvider for every partial view I want to render.
I found another solution that allows me to call an action from a view and render a partial view:
<?php echo \App\Http\Controllers\PageController::listAll(); ?>
But this solution doesn't seem very clean.
ViewComposer behaves like a global component, all you have to do is put your business logic into a method in your Model.
e.g.
class Post extends Model {
public static function archives() {
return static::selectRaw('your query here')
->get()
->toArray();
}
}
Then goto providers/AppServiceProvider.php
add your viewcomposer code into boot method (a hook before any view loads).
e.g.
#include('layouts.sidebar')
in AppServiceProvier, boot method:
public function boot() {
view()->composer('layouts.sidebar', function($view) {
$view->with('archives', \App\Post::archives());
});
}
You can use the Service Injection:
<div>
#{Html.RenderAction("Controller", "Action");}
</div>
In Laravel would be:
#inject('Controller', 'App\Http\Controllers\HomeOrOtherController')
{!! $Controller->action() !!}.
With this you will render in the view the content of the view of the action in the controller.

Proper way of creating object using a form in its parents view?

Let's say I have two classes:
News (hasMany Comment)
Comment (belongsTo News)
In single news object view (news/show.gsp) I'm listing comments belonging to it after it's content.
Then I want to have a form which will allow some user (but security isn't a case here) to add new comment, and then show the same (news) view again.
Seems easy, but I couldn't find the anwser:
What is the proper way of doing it in Grails?
If you run grails generate-all News and grails generate-all Comment on the command-line, Grails will generate views and controller actions that allow you to CRUD each entity. If you read the generated code for the create and save actions and views of CommentController, that should set you straight.

Grails one-to-many relationship data binding

I am new to Grails and am trying to test a sample one-to-many form as outlined in the link below
http://www.2paths.com/2009/10/01/one-to-many-relationships-in-grails-forms/
The views seem to be working fine as I can add new text field for Book (child) from Parent (Author) and delete as necessary.
However, the databinding for child data (Books) doesn't seem to be working.
Books that belong to the Author are not added to the database when I add them in the view.
It was my understanding that the default controller should work in this case. Am I wrong on that?
Do I have to modify the save() method for the AuthorController?
I see that the above post was from 2010 and I am currently using Grails 2.4.2.
Could that be the issue?

How to model many-to-many relationships?

I have a problem with a model in a asp.net mvc3 application.
First of all, i have a table with items, each item has e.g.: id,name and description
where i also have a user with: id, name
It looks somewhat like:
class item {int id, string name, string description}
class user {int id, string name}
Now i also have a table which maps these two things to each other, with foreign keys.
so each user can have * items and each item also can have * users.
Now i need to create controller and view for the item. this works fine, the problem is, that i just create a item, but i want to create a item and the mapping to a user in one step.
how to solve this problem in mvc? (e.g. i just can add one model in a view and this is the item in the create process)
Short answer: you need to add drop-down of users while creating an item. This will help to link each item to a user.
However, your particulate issue seems to be in building many-to-many relationship in the model. How to do this? Well, this is already nicely explained here - ASP.NET MVC, Entity Framework, One-to-Many and Many-to-Many INSERTS
In addition, I would strongly suggest to go through - NerdDinner Tutorial. It will give you good start-up and solid background for development in asp.net mvc framework.
You may also refer to Tutorial samples in official website to learn and practice development in mvc.
Here is a link to complete code of the tutorial - NerdDinner 2.0 Complete ASP.NET MVC Sample App

How to create a dashboard user interface using ASP.NET MVC?

I am currently building an application using ASP.NET MVC. The data entry pages are fairly easy to code, I just make the Model for the page of the type of my business object:
namespace MyNameSpace.Web.Views.ProjectEdit
{
public partial class MyView : ViewPage<Project>
{
}
}
Where I am struggling is figuring out the best way to implement a dashboard like interface, with stand-alone parts, using ASP.NET MVC, where the Model for each part would be different? I'm assuming that each part would be an MVC user control.
Also, how could I make it so each part is testable?
I think that user controls is probably the way to go. I'm not sure what the concern is about testability. You should be able to test that your controller is providing the right view data -- since you'll have several models each of these will probably be stored in a separate view data item, rather than aggregating them in a single model. Aggregating in a single model is also possible, although probably more brittle. Each control would just need to check for a particular view data item, rather than being specific to a particular model. You could approximate the model variable on each view page by doing:
<% MyUserControlModel model = ViewData["MyUserControlModel"]
as MyUserControlModel; %>
<div id="myUserControl_dashboard" class="dashboard">
Name: <%= model.Name %><br />
Count: <%$ model.Count %>
</div>
If you need to test your view, then you're probably already using Selenium or some other web testing framework. I don't think that these would care how the page was constructed and you should be able to construct your tests pretty much like you always do.
Check out the notion is sub-controllers in MVC-Contrib http://www.codeplex.com/MVCContrib. Basically you run a full request to a partial then display that partial where you want in your existing code.
Alternatively you can check out this post: http://blog.codeville.net/2008/10/14/partial-requests-in-aspnet-mvc/
Codeplex.com has an open source project "DotNet.Highcharts". This project provides an ASP.NET control that wraps around the excellent (free for personal use) Highcharts charting library. that includes a sample MVC3 project in the downloads section.
URL: http://dotnethighcharts.codeplex.com/
I have posted an article on CodeProject.com that includes a downloadable VS2012 solution and some sample C# code. However I use a webform template in my example, not MVC. The bulk of the dashboard is done using JavaScript libraries, HTML & CSS. I have included some sample C# code to demo data retrieval in C# which in then merged with JavaScript code to render one of the many dashboard charts.

Resources