Spring MVC web widgets - spring-mvc

I am planning to create generic commenting module, that will work for any object in my application just by passing object_type, object_id.
Comment module will give list of comments along with form to enter new comment, I want to include this module in other jsp, like product jsp and others, so this should request the comment module before the o/p renders.
I know this is possible with PHP Zend framework, for those who dont understand my question properly refer 'Zend Action View Helper'
http://framework.zend.com/manual/en/zend.view.helpers.html#zend.view.helpers.initial.action

I got the solution we need to use <c:import url="/user/11"> this will do what I am expecting

Spring frame work supports a number of view technologies. If your using velocity for view you can use velocity macros as view helper. And JSP and other 'view technologies' also have smae kind of technologies.

Related

how to feed the ::base.html.twig with datas of database more proprely?

in my symfony application i use many controllers . They render their templates who all extend ::base.html.twig template. I feed these templates with an array of datas for display the dynamic content , thanks to my controllers. But the ::base.html.template need also to receive his array of datas for display 4 pictures in a kind of slider . These datas (url of pictures) come from datas base and are available for all pages in my website so i'am asking myself how to send just one time this array of picture? For moment in every controllers I have to repeat the same code for get the url of pictures from database .
Sorry if my question is not clear
I'd personally move the code that creates these sliders out of the controllers into a service layer, be it a proper service or simply a class you create and use dependency injection you need to access things like the EntityManager within it. Services are fine, but they have an associated overhead.
By moving the code out of the controllers, you have a method you can call from all the controllers and one copy of the code, instead of multiple copies through your controllers.
Rendering controllers within twig is certainly possible, but it seems a bit of a fix for bad design (in my opinion anyway). It's a quick win, but not exactly an efficient one.
Remember, controllers should be thin, not bloated fat with code.
I think the correct way would be to create a Twig extension that will get this data for you.

How to do the same thing as Struts <s:action> in Spring MVC?

I would use the Struts2 <s:action> tag to embed an action into a page. What would be the equivalent in SpringMVC? I mean create the page dynamically inserting content when needed. How can I achieve this?
If your goal is to "print" or render the jsp content inside of your current page, you can do check this answer here
Otherwise, you can use another framework like Tiles, which is also supported by Spring MVC, you can see the whole information here

Symfony2, how create a widget like a standalone class

Let's start with basic thing, simple example is Yii. It has such thing as widgets. Standalone, configurable and callable from any place we want classes. And I'm wondering can symfony2 has the same? What it will be? Controller in bundle? Action simple (method)? Widget (twig) with parameters?
In Yii we create class (of widget), standalone, describe it and use (by calling in template). How will it look like in symfony2?
Simple example 'i want create menu navigation using widget, where it will construct html by user roles'.
Symfony doesn't provide such a feature however you can make them yourself. They are few ways of doing it.
I'll just admit that we are talking about widgets that could do backend work (i.e. get data from DB, call an API, etc.).
1 - The scalable way
The use of the render tag in Twig which allows you to call a controller action from a template. It's scalable because you could use esi tags with Varnish (you can also implement your own caching profiles).
As a bonus, the profiler will show details about the specific render calls in the timeline (it will look like a subset of the entire request).
2 - Include a template
The included template gathers the data through a Twig function call. By experience, it's a bit faster than the first solution however it's not easily scalable.
3 - Render through a custom TwigExtension
The twig function will get the data and call the renderView method of the template service. If you are planning on doing this, you probably want to use the first method.
Conclusion
If you have a big website with modules/widgets that gets a lot of traffic (or "hit"): use the first solution.
If you have a small website with no support for caching: use the second solution. You'd use this solution if the module/widget is super light.
If you are thinking about the third solution... it's probably a good idea to use the first solution.
Personally, I'll always try to use the first solution and try to boost the performance one way or another. The render call in Twig has been significantly improved since the last versions of Symfony2.
Hopefully, my answer will provide you some guidelines.

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.

MVC + Templates

I am working on a system that gets templatse dynamicly, they contain tags like {{SomeUserControl}} {{SomeContent}}
I was wonder how I could use MVC to render those templates and replacing the tags in the best possible way as the templates will be edited via a web front end, and the content / macros will be create from the same web front end.
You might wanna take a look at maybe using another view engine, here are some examples.
NHaml
Spark
NVelocity
Brail
I'm sure there are many more but these are the ones I could think of.

Resources