Component controller in Spring-MVC - spring-mvc

I am designing a web application that requires loading multiple components on a single page. (In terms of page layout is similar to Google reader) I like to keep these components separate from each other (for reasons such as reusability). For example, I have a left panel that let's user navigate among multiple feeds that he's subscribed to (user specific) and on the right side of that we show the contents of that feed, and maybe a right panel which shows some specific info about that specific feed.
My understanding of MVC and more specifically Spring-MVC is that each controller is in charge of the entire page. Here are 2 solutions that I have came up with after researching this a bit, and none of them sounds good to me.
Have a main controller that is mapped to that URL, and then load the other components from inside the jsp file. This is doable but doesn't sound like a good solution.
Using portlets.
I want to know what are the best practices. This sounds like a very common web design issue in MVC frameworks, how do people do it?

Spring MVC controller is usually "in charge" :-) of handling a particular request which does not necessarily mean that said request results in a monolithic page being presented to user.
Since you're talking about Google Reader-like functionality, perhaps you'll be using AJAX to load / navigate between different components on your page? If that's the case, you can easily map your 3 components to separate controllers (via separate URIs) plus have one "main" controller to initially load the entire page.
If that's not the case and your page is static, you can have your controller act as "router" of sorts by first instantiating your components and then directing commands / requests to an appropriate component as necessary. Each component would update its own part of the model after which your "main" controller would return the view to be rendered.
Can you use portlets for this? Sure. But if we're talking about a single page it might be a tad overkill.

Related

Asp.net Core 2 Domain dependent View selection?

I have a new multi-tenant web application in ASP.NET Core 2.0, single DB.
I've established a method of determining a particular request's 'TenantId' by examining the domain in a simple piece of middleware.
I've also established a DataContext which applies TenantId filters to applicable tables as needed.
The last thing I'm unclear about is how I can differentiate Views/partial views based on the TenantId whilst sharing the controllers.
I think some scheme where the app first looks in some kind of TenantId sub-folder for the customized tenant's view and if it can't find it, it goes through the regular steps to locate the view... might work okay? Is this a reasonable approach? In other words, it should use the specialized tenant view if it exists, otherwise use the default view.
Would this involve building a custom view engine of sorts?
I've tried something similar with tenant based Html fragments, but it was a pain maintaining it, so I'm looking for something more straight forward on this project.
I'm open to other suggestions of how to implement this functionality as well.
Thanks in advance!
You don't need a whole custom viewengine, you can implement an IViewLocationExpander to make it check various locations for views.
In my project I have mutliple tenant support with both shared themes and tenant specific themes. I can override any view by dropping a copy below the theme folder, ie the main view could be Views/SiteAdmin/Index and I can override it in /pathtothemes/themefolder/SiteAdmin/Index
You can see my implementation of IViewLocationExpander here.
How you register your IViewLocationExpander is like this:
services.AddMvc()
.AddRazorOptions(options =>
{
options.ViewLocationExpanders.Add(new cloudscribe.Core.Web.Components.SiteViewLocationExpander());
})
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
;

Should I use Data Cache or Output Cache for a complex navigation menu?

I am attempting to cache sections of a navigation menu according to different criteria.
For example, news and articles need to be refreshed on a duration basis, whereas login and profile stuff should be cached on a per user basis.
I'm considering 2 options - would anyone be kind enough to enlighten me about the pros / cons of each? And if possible suggest a better approach to take!
Option 1.
Simply cache all required html as strings in the Data Cache. Check for user differences manually when required.
I (perhaps incorrectly) imagine that this would be the most work to implement, but also the most efficient way of caching the different sections.
Option 2.
Have a NavigationController with different child action for each section of the menu. (We can apply a different outputCacheProfile on each child action as required.)
But this would require us to call a separate RenderAction for each section of the navigation menu. And I'm worried about this because of a comment on one of Phil Haack's blog posts:
[Render Action] is very similar to making another request since we
need to run through routing to make sure we have the appropriate route
data and context to call the action method. So each call to
RenderAction is going to add up.
Full post here: http://haacked.com/archive/2009/11/18/aspnetmvc2-render-action.aspx
I think there's been a general avoidance of this question because there is no right answer here.
In fact, your choice of Architecture for implementing complex navigation will determine the best Caching strategy.
I'm deeply partial to Navigation through Partial Views with Child Actions.
I agree with you that referencing files is more work. I prefer to have database entries with navigation options grouped by keys and referenced by argument to child actions.
So your Navigation Table may look like this
grpId Title Path
1 Home Page /
1 About Page /Home/About
1 Reports Page /Reports
2 Home Page /
2 Admin Page /Admin
2 Reports Page /Reports
and your child action would take a grpId
[OutputCache(Duration = 60000, VaryByParam = "grpId")]
public PartialViewResult NavigationPage(int grpId)
could pull all your navigation group options and render a custom navigation menu. This form of output cache is customized by time (60000 secs and your param)
Conclusion:
I suspect I have told you nothing new, but have only confirmed what you were already leaning toward. The MVC framework is very robust and provides tools to handle nicely what you want to do. Using files and Data Cache is also a valid method, but it would be greater head-ache and work on your part to implement.
Keep in mind: Haacks's post is over 4 years old (MVC 2 Beta) . The framework and output cache has evolved nicely since then. You can now Cache Partials without worrying about caching the entire page. This recent reference to caching with MVC 4 doesn't speak directly to Phil's earlier concerns, but notably neglects them.

Smarty plugin-like functionality within Spring MVC possible?

I have a software design question. I am looking for Smarty plugins-like functionality in a Spring MVC app. Let me try to explain the case.
I am using Spring MVC to build a webapp and I want the system to be able to display application wide 'system messages' like
<div class="sysmessage'>Your user profile is incomplete, please fix it</div>
or
<div class="sysmessage'>System down for maintenance next friday between 13:00 and 14:00</div>
I already have a service method that returns system messages when the user needs to be informed of something. The normal approach to this (I think) would be to call the service method from each and every Controller method and add any system message to the model, display it in the main grid JSP and voila... application wide system messages.
But this approach seems so labour intensive. I would need to add the call to the service method to every controller method, add it to the model every time, etc..
My question: is there some kind of 'reversed' setup possible in Spring that resembles the Smarty plugin functionality? A smarty plugin is a piece of HTML/template code that is backed by PHP code. I need the same in my app. It would be much faster and cleaner if I could place a piece of JSP code that is backed by Java code in my template. I know a custom JSP tag basically does this, but I was wondering if there is something packed with Spring that can achieve the same. It seems like such a common scenario. I looked at Spring Protlet MVC, but I am not entirely sure if that fits my needs.
Several options:
Spring interceptor
Tiles view preparer
ServletAPI filter
In all cases, you will need to add some code to your jsp's to output the message in your page.
This would be best done in a tiles layout which is used by all the pages that should display the message.

How to keep DRY a Ajax web forms solution

At the moment we have a solution which is Web forms ASP.Net 4.0. We do a number of things such as using web methods and services either calling them using the standard web forms way or sometimes to reduce the footprint directly calling them with jQuery ajax posts and gets.
We are looking to improve the way we work but we have heavy constricts regarding how the solution is at the moment and not being able to completely rewrite it.
Updating the page using Ajaxs for data, forms and for example pulling "the next 20" items and displaying them on the page it what I would like to heavily stream line.
Using template's due as PURE and jQuery Templates is fantastic way to produce fast calls back and forth between the servers but results in having two copies of the html. (the template for the jQuery and the code in the actual first render of the page)
We have thought about possible producing a empty template and then always populating it via json data we post down to the server but I feel this isn't how things should be done...
can anyone reckoned the best way we can do this without having two copies of our 'template' (e.g. a row of a table)
You mean you have a template in asp and the same template in javascript, but you'd rather just have 1 or the other?
I think that is really subjective. It is always different based on use case. That being said I'd do it by modifying my views and templates. My views (non-js) would simply have containers for that dynamic content. In other words I'd never load the dynamic portions of content into the views initially. Rather, on page load I would simply load up the template and the json that fills it in.
If you think about it that's 2 more requests, but it makes your life easier. The user also is able to see something on the page sooner.
This is one of those questions that really depends on what you are doing. There are trade-offs to be analyzed with every solution.

ASP.NET MVC State Management

I am working on a .net mvc web application that has a bunch of web parts on the homepage. I realize that a webpart is a .net forms terminology, but wasn't sure what to name those mini sections. So anyways, for now these sections are called when that page is rendered, but eventually I will plug in JQuery and call these sections using AJAX. These mini sections or widgets will need to keep state as the user navigates between the homepage and back. With .net forms, the page state info is kept in the viewstate, but with .net mvc, that is not available (thankfully).
So, is there a framework already created for such functionality for MVC? If not, what would be the best way to handle this situation? I was thinking to leverage the HttpContext object and store everything in there, but not sure if that object has any size limitations.
Just like with anything else in MVC you're going to need to store the state somewhere. As other users have pointed out, using Partial Views will reduce the complication of the design by allowing you to have controllers that handle just those small parts. They can then be responsible for saving/restoring/tracking the state or info for that part. (This could be using a database, in process memory, whatever.)
If you put the controls in the master page you can have the parts' implementation removed from your other logic so you don't need to worry about capturing and returning data related to those parts with the rest of your model meant for your view.
Having the parts separate like that will make it a lot easier to AJAXify them as well since they would already be operating independently of your view data even though they are rendered at the same time.
I think you want to check out Partial Views.
You can use output caching or data caching, both supported by MVC 1.0/2.0.
Not sure if you're after this but project Orchard has a notion of widgets: http://www.orchardproject.net/docs/Default.aspx?Page=widgets&NS=&AspxAutoDetectCookieSupport=1.

Resources