Spring MVC jsp view - spring

To design views that each jsp views have common navigation menu ( a treetable, when clicking on a tree node it navigates to another page) what is a good approach for this?
Adding a new node to navigation menu will effect all pages.
Navigation menu tree nodes will be populated from db (a menu table on db)
Is it possible to cache menu view on client so navigation menu will not be populated each time user navigates another page?
Thank you

There are two separate questions asked in the post, I will answer them accordingly:
1) I would recommend using a Template Engine which integrates with JSPs.
Apache Tiles fits in this category. Using this framework, you can define template which contains common sections for header, footer, body and menu layouts. Furthermore, it supports derived templates via inheritance.
2) Caching solution : Since you want to load values from database to make the menu dynamic, caching and pre-populating the menu will minimize page load times. This should be done on server side.
A separate thread/process should be made responsible for pre-populating and refreshing cache periodically.
From clean code perspective, JSP or Tiles template should not invoke database calls. Instead the page should interact with a DAO/component to separate concerns and ensure MVC pattern is not broken.
Helpful links:
Tiles Example
Thymeleaf - an alternative to Tiles

Related

MVC 3. Load menu and content separately

I have an app with 3 sections:
Main menu;
Context Menu - Related to selected item in main menu;
and Page body - Related to selected item in context menu;
"Main menu" and "Context menu" are based on membership. I don't want to load them everytime my page loads, because that would consume resources database. So, I'm using ajax to load main menu only one time, and when an item is selected, I load the context menu for that item.
My problem is: Every form's post will erase my menu.
Question: Will I have to build my entire application using ajax? I don't wanna do that, because it is too much simpler do a post in the form then send all data to controller with ajax.
Until now, I have 2 options:
Load my menus with ajax and the page body with IFRAME, so the post's will not render again my menus.
Do everything using ajax;
Is there any alternative to load my menus with ajax and be able to use form's post?
Sorry if I wasn't clear enough.
The sentence that gave me a pause is this "I don't want to load them everytime my page loads, because that would consume resources database."
You see, I've build quite a lot of apps, that display menus and sub-menus based on user roles (what you called membership). This has never been an issue from the resources or database perspective.
You can access all the membership information that you need once, when your used is being logged in. In the simplest case user's identity will be stored in the context along with the roles they have (HttpContext.User), so you do not to need a database lookup at all to get this information on every request. Note that with this scenario no ajax is required either.
If for whatever reason you can't store your membership information in the context like this, you still can store in in session (if in-memory) or in encrypted Cookies.
Now, I understand, that I don't all the details of your scenario, and that may be in your scenario what you are trying to do is warranted, however I suggest you think it through again, as under normal circumstances what you indicate is a problem (database resource) should not be a problem at all.
The bottom line is: if you alter your application that it stores the membership information when user logs on you won't have your problem to start with.
You don’t have to build all of your application using Ajax. But in this scenario Ajax may be the best way forward.
Following is my suggestion
Create your data entry for inside a dev
Have each input controller marked with a class (say ‘dataEntry’)
Create a javascript function to iterate the dev and build a list of all elements that has class dataEntry
Build a json object using the list. Use the name of each element as property name and value as the property value
Use jquery ajax to post this to the controller action
[optional] you can use .done and .fail methods to take action on success or failures of the call
I know this may look intimidating, but if you have many data entry forms, you can re-use this code.

Multiple independent charts / widgets on MVC 4 view

I am working on a dashboard application that has multiple independent widgets based on the common query string parameters passed. In MVC, each widget has separate controller --> view --> JS file (that makes AJAX calls to get JSON from WCF). Now, I need to create a master page that will "host" all of these independent widgets. How can I make sure that these widgets are truely "independent"? IE has concurrent connection cap that could be made to a single server. Will iframe serve the purpose? Should I host each widget in a separate iframe on this master page? What are more suitable and acceptible alternatives? Use partial views?

Avoid re-display of menu using MVC

My team is creating an MVC4 application which has an upper menu structure that contains menu items specific for the currently logged-in user, and displays the content for the selected menu item below. The menus are displayed in a partial view "_MainMenu" and the MainMenuController Index action is responsible for getting the menu data from the database and returning the menu partial view. The main layout "_Layout" renders the menu by calling #Html.Action("Index", "MainMenu").
We want to avoid hitting the database to get the menu structure data every time the page is refreshed, and we also ideally want to avoid any visible reload of the menu structure when a different menu item is selected.
I've been looking into AJAX, and I've implemented something where selecting a new menu item will load the content for that item below the menu by replacing a div with ID="mainContent" with the content as a partial view (this div lives on the loaded view "MainContent"). In this way, the menu is never re-loaded and the page never refreshes completely (only the different partial content views are swapped in and out as different menu items are selected).
This solution works, but I'm not sure if it's really best practice, or if there's a better solution involving caching. One of the problems with the AJAX solution is the URL never changes from "http://MySite/MainContent". This means that clicking the Back button on the browser doesn't work as expected, and also hitting F5 to refresh the browser page loses the currently displayed page. The refresh issue can probably be resolved by remembering the selected menu item, but I'm still not convinced this isthe way AJAX was intended to be used.
Can someone please tell me if they think caching would be a better solution for this scenario based on their experience.
If the user's menu items are not likely to change, then you could use a simple static Dictionary (the long is the user id) to cache the data in the backend, and refresh it periodically.
Generally, if you do any caching you have to make sure the cached data gets refreshed when underlying data is updated. Therefore it's only simple and safe if you have only one app accessing your DB. If there are multiple apps, then you'll have to add a mechanism to keep the cache in sync.
In project I'm working on we've stored User-Role-Module entities and relationships in DB, and bound module permissions to roles rather than users. Since role permissions aren't likely to change, they're safe to cache.

Mixing asp.net mvc controllers/views and static content management pages from database

I'm trying to build my first page using asp.net mvc3/razor which will mix pages showing entities like a list of news items and pages loaded from database created from a wysiwyg form (=content management page, cm). Therefore I would like all cm pages to use the same controller/view.
One "ugly" way to solve it is using a Pages action on each controller but I rather have a way to lookup if the link is an action or should be loaded from DB. Can I create routes like that? And if I can should I or is there a better solution?
For example if I have this structure
Home (Controller)
News (Action, page built up dynamically using list of news items)
About (Action, page from db)
Contact (Action, page from db)
Calendar (Action, page built up dynamically using a list of calendar events)
Orienteering (controller)
What is orienteering (Action, page from db)
I would like the url to the About page to be /Home/About not Home/Pages/About and the What is orienteering to be Orienteering/What-is-orienteering. These two pages are created from a wysiwyg editor in the gui by the users.
The News and Calendar should be loaded like standard controller/view page.
The bottomline is that I would like a base set of dynamic pages displaying structured data and on top of that the users should be able to create new pages from that GUI that is not controllers/view but loaded directly from the database.
I could create a Pages controller but then the URLs won't mirror the menu the user is seeing.
What is best practise in this case? :)

MVC - How to develop a wizard like control functionality in ajax

I thought I would ask here about how to develop a wiazrd like control
in ajax for MVC before I started it.
The control will have pages that have data entry controls and
on each page submission insert data into a database.
Any ideas how to go about this?
Malcolm
Are you using a JavaScript library? I know dojo has a wizard in dojox namespace, and I'm pretty sure there are wizards for jquery. If not, I'd do a wizard just like I would tabs, without the tab bar, and each view contains a form and next, previous, cancel and or restart buttons, all of which would handle the ajax calls/state management and change the view.
If it's a wizard you probably actually should use separate web pages rather than having a single page that's updated through ajax calls, that way the forward and back buttons can work as nature intended in the browser for moving forward and back through the wizard.
Of course you can use fragment tricks to get forward / back working in a single page but in my experience this is tricky.

Resources