Spring MVC - Share Data - spring-mvc

I'm new to Spring and I've been using Spring boot with Freemarker template to buld a simple blog application. In my application I have a navbar.ftl which is shared by all other views.
In this nav bar I want to access a list of all Authors in order to show their names in a drop down (code below), but I'm not sure how to access it here, since this navbar is shared between all views.
<li class="dropdown">
Authors <span class="caret"></span>
<ul class="dropdown-menu">
<#--Need to list all authors here. How???-->
</ul>
</li>
I have an AuthorRepository where I can find all authors, but I'm not sure how to use it in my view (I don't think the view could access the model directly anyway).
#Repository
public interface AuthorRepository extends CrudRepository<Author, Long> {
}
Any idea how I could have a list of authors in this navbar so I can show their names?

I think controller you can create function common ShareData. Put all data for method, when need you use this with call function ShareData(). That's all I thought.

Related

How to hide link by guard angular2

How to hide admin link in view html. I have to guard :Admin and Manager
Router config :
{
path: 'manager',
component: ManagerComponent,
canActivate: [ManagerGuard]
},
{
path: 'user',
component: UserAdminComponent,
canActivate: [AdminGuard]
}
In view :
<li>
<a routerLink="/user" routerLinkActive="active-link">User</a>
</li>
I want to hide link on /user for ManagerGuard when but show for AdminGuard.
Also if already answered in a way which might be helpful, I used here another approach. As you already have your canActivate method in the Guard, you could also inject your navigation component with that Guards and you can call these canActivate methods directly:
In your component which holds the navigation
constructor(private adminGuard: AdminGuard)
and then in the template
<li *ngIf="adminGuard.canActivate()">
<a routerLink="/user" routerLinkActive="active-link">User</a>
</li>
Edit
This does not work in prod mode in my case (if you use parameters which have to be injected). I just tried to compile it but angular complains about the missing parameters. If you do not use parameters, this works fine, or, if you do not use the parameters in your function - then simply pass
<li *ngIf="adminGuard.canActivate(null,null)">
<a routerLink="/user" routerLinkActive="active-link">User</a>
</li>
Another thing: If you is variables like the 'adminGuard' above in the template, it must be public - not private.
In my opinion this has nothing to do with the router itself.
Your Guards may call another service which has the information about which kind of user is logged in: Admin or Manager.
You should create a service that knows the type of the user. Then inject this service with Dependency Injection into your component where you have the routerLink.
There you can ask the service and toggle the link with *ngIf:
<li *ngIf="myService.getCurrentUser().isAdmin()">
<a routerLink="/user" routerLinkActive="active-link">User</a>
</li>
So the service provides a function that gives you the user which is currently logged in and the isAdmin()-function of the user returns true or false. If the user is admin then the <li> will be shown, otherwise it will be hidden.
This is just an example but I hope you get the point. It has more to do with basic functionality of Angular 2 rather than the router of Angular 2. More information about *ngIf can be found here.

Uikit Dropdown for dynamically created (reactive) DOM in Meteor

An app I'm building shows email accounts in a sidenav as a dropdown (or accordion) as described here.
The dropdown works fine when the data exist on load time, thanks to the UIkit observer.
The code looks something like this (simlified):
<div id="app_wrapper" data-uk-observe>
<ul class="uk-nav uk-nav-side uk-nav-parent-icon" data-uk-nav data-uk-observe>
{{#each mailAccounts}}
<li class="uk-parent ct-mail-account-side-nav-entry">
user#domain.tld
<ul class="uk-nav-sub">
<li><i class="uk-icon-inbox"></i> Inbox</li>
<li><i class="uk-icon-folder"></i> Sent</li>
<li><i class="uk-icon-folder"></i> Trash</li>
<li><i class="uk-icon-folder"></i> Spam</li>
</ul>
</li>
<li class="uk-nav-divider"></li>
{{/each}}
</ul>
</div>
When adding new accounts through a meteor method, the entry appears yet the dropdown is stone dead. Only if the template is completely rerendered it comes to life. That usually requires a little detour to some other page where the menu doesn't exist.
How can I make the observer aware of the change?
Why doesn't the observer notice the change and apply it's magic to the newly created entry?
Is there a way to manually assign the JavaScript dropdown magic to the newly created element?
UIkit Version: 2.19.0
Meteor uses ractive variables to modify DOM at runtime. What this means is that your mailAccounts need to be a ractive variable. There are two cheap (in terms of effort) methods to achieve this
Define mailAccounts as a session variable
Have mailAccounts be part of a collection
There is a third way as well, if you don't mind writing some more code. Use ractiveVar package and define mailAccounts as a reactiveVar.

Umbraco multi-color menu tabs

I'm using the Asp.Net CMS Umbraco and would like some help to determine whether or not there is something specific about the Umbraco CMS that might make the task of creating a multi-colored menu bar... different from standard html and css manipulation.
The menu bar code that I'm using is based upon the friendly-ghost theme shipped with Umbraco.
Now the normal method for employing different colours in a menu would be something like
div # menu ul.rMenu li.page-item-(some number) {background-color: # (whatever);}
...
...
<li class "rMenu-expand page-item-(the same number as above)"><a href="(link to whatever site on the menu)">
<span> Wording for the particular tab </span></a></li>
And bingo... the (some-number) tab has the (whatever) colour!
However, Umbraco seems to be using a different set-up that is not entirely compatible with the above strategy. Should I be looking at *umbTextpage id *parentID *nodeType ... or something entirely different that I haven't, as yet, noticed?
I believe that nodeName is being used for the wording of particular tabs... should I take it, then, that nodeType refers to the tabs themselves ???
That's great, thanks. However, the menu is generated dynamically using xslt. Do you think I should I attempt to insert that code (converted to xslt) into the menu generation process...
<xsl:for-each select="$currentPage/ancestor-or-self::* [#level=$level]/* [#isDoc and string(umbracoNaviHide) != '1']">
<li>
<xsl:if test="#id = $currentPage/#id">
<xsl:attribute name="class">current</xsl:attribute>
</xsl:if>
<a class="navigation" href="{umbraco.library:NiceUrl(#id)}">
<span><xsl:value-of select="#nodeName"/></span>
</a>
</li>
... or attempt to use razor code after the fact (i.e. have the menu be created and subsequently cycle through the menu's elements, giving a numerical 'tag' to each node)?
The beauty of Umbraco is that you have complete control of the layout. You can have it build your menu how ever you want.
I don't know whether you are using xslt or razor to generate your menu, but If you're looking trying to get a unique number to append to "page-number-" then use the node's Id, that won't change even if the name of the node is edited.
Using Umbraco 4.7 razor you do something like this:
#{
<ul>
#foreach (var node in Model.AncestorOrSelf().Children.Where("Visible"))
{
<li class="rMenu-expand page-item-#node.Id">
<a href="#node.Url">
<span>#node.Name</span>
</a>
</li>
}
</ul>
}

Working with Collections containing Collections in Asp.Net MVC

Looking for some guidance on this issue (below is a parallel example):
I have a view in which I need to display a list of basic user information (e.g. First Name/Last Name) per row. In addition, I need to provide a list of check boxes for that represent permission levels for each user. The list of possible permissions is the same for everyone; however, needs to be dynamically driven by the database since different permission sets may be added or removed.
So...I have a list of users, and a common yet variable list of permissions that needs to be tracked per user.
What's the best way to manage this from an object bind standpoint in a ASP.Net view?
I've solved a similar scenario using EditorTemplates
You could have a Users collection in the AssignPermissionViewModel, and a Permission collection in each user (UserModel). Then in your AssignPermission view, you should render the users editor:
#Html.EditorFor(model => model.Users) //here model is a AssignPermissionViewModel
And inside the user editor template you should render the permissions template:
#Html.EditorFor(model => model.Permissions) //here model is a UserModel
This way you avoid having foreach loops all over your AssignPermission view, and you could re-use any editor in another view... (although these are very specific)
NOTE: It would be more appropriate to use a display template instead of an editor template to display the users information, since you're not going to edit any user in this view, check this out: ASP.NET MVC 3 - Partial vs Display Template vs Editor Template
You should create a List of Users in your View Model. A User should be a Class in which you create a property that is a list of Permission Levels, so in your View you sould call something like this:
<ul>
foreach(var user in Model.Users)
{
<li> user.FirstName...
<ul>
foreach(var permissions in user.Permissions)
{
<li> permission.SomeProperty </li>
}
</ul>
</li>
}
</ul>

Repository is Nothing but added in the container later (correctly) (using Unity, Repositories) in ASP.Net MVC 3

I have a "MenuService", which uses an "MenuRepository".
When the MenuService is created, it loads correctly with the MenuRepository.
Then it wants to save it in the UnityControllerFactory, but the MenuRepository in the MenuService is Nothing then (it wasn't before).
Very strange.
Does anyone has an idea why this is happening?
Some additional info:
I'm using the Unity and Repository pattern.
The problem happens with a partial view (for a menu), which i have included in the "HomeController", it could be relevant information, so i included this, just in case:
Function Menu() As ActionResult
Return PartialView("~\Views/Shared/_Menu.vbhtml", _MenuService.GetAllMenuItems)
End Function
Also, my partial "menu" view is like this:
~/_Menu.vbhtml
#ModelType IEnumerable(Of FacturatieMVCv2.Domain.Slave.MenuItem)
#*<div id="myslidemenu" class="jqueryslidemenu">*#
<ul>
#For Each Item In Model.Where(Function(el) IsNothing(el.HasHigherMenuItem))
#<li> #Html.ActionLink(Item.Naam,Item.Action,Item.Controller)
<ul>
#For Each SubItem In Model.Where(Function(el) el.HasHigherMenuItem.MenuItemID.Equals(Item.MenuItemID))
#<li>
#Html.ActionLink(SubItem.Naam,SubItem.Action,SubItem.Controller)
</li>
Next
</ul>
</li>
Next
</ul>
And i'm calling the partial view with:
#Html.Action("Menu","Home")
You need to setup your IoC before your application starts and you need to get your service from IoC.
This article will help you a lot.
I this case repository dependencies in the service will be resolved by IoC (Unity)

Resources