Is it possible to remove session attribute in freemarker? - freemarker

is it possible in freemarker to remove a session attribue ?

No, it's not possible. FreeMarker is a template engine, and does not modify the model in any way.

It's possible if the framework within which you are using FreeMarker provides a method or directive for that... or if you add a method yourself that does that. But since FreeMarker is (normally...) used as the View in the MVC paradigm, you shouldn't do that. Creating/removing sessions is the duty of the Controller in MVC. That's the whole point of MVC... separating these concerns.

Related

How to use expression tags in Spring MVC 3.1

I'm using Spring MVC 3.1. I want to show values from my models in my asp and sometimes I need to use conditional logic. I know about JSTL tags but I'm wondering if there is a better alternative shipped with 3.1?
Currently I show model values in jsp like so: ${model.name}
But I want to do stuff like: if name is John then do X or else do Y
I don't want to have thymleaf dependency.
Are there any alternatives?
The short answer:
Spring does not ship with something that duplicates (or eases) the JSTL tags.
The long answer:
I think for JSP Spring expects that JSTL is the way to go for a taglib. Spring does have a taglib, but it is all about filling in forms with your model.
I would strongly discourage using ${model.attribute} for placing text in a page, especially if the text has come from any form of user input. For security reasons when working with user-entered data you should use c:out to ensure the text is properly escaped.
You can do your conditional with c:if or c:choose. The accepted answer to if...else within JSP or JSTL may help with that.
With JSP 2.0 Expression Language you may use the "Ternary/Conditional" operator:
E.g.
<p>${myobj.test == xy ? 'hello' : 'nohello'}</p>

Where's the best place to create local variables that will be used in a display (using MVC principles)?

I was thinking of creating them in the controller and just pass the variable to the JSP but unsure if this is the way to go.
thanks :)
Controllers pass data to Views via Models - see the Spring MVC documentation
Basically the layer of segregation that has been made by MVC architecture itself says that you should do the processing from backend data [after you get it from Model Bean] in the Controller.
In your question you have clearly mentioned that you want to know the
right place where to declare variables ? -> So you would want to do some processing on them e.g. change values based on some data or calculations right!!
So make the variables in controller, obviously private data has to be taken care so do that in controller or the class file itself.

Is there a way to Iterate all Controllers/Actions in an ASP.NET MVC3 Site?

I'm trying to make a dynamic menu function in an ASP.NET MVC 3 website - and I'd like to know if there is a built-in way to get all of the Controllers and Actions at runtime?
I realize that I can use reflection to find all public methods on my controllers, but this doesn't exactly give me the relative URL that I should put in the <a href="..."> tag.
Also, I'm going to be decorating some of the 'actions' with filter attributes that dictate whether the current user can see/goto those pages. So it would be best if I had access to the filters as well so as to be able to call the IsAccessGranted() method.
What are my options? What is the best option?
I actually just did that two weeks ago.
var q = from type in Assembly.GetExecutingAssembly().GetTypes()
where type.IsClass && type.Namespace != null && type.Namespace.Contains("Controller")
select type;
q.ToList().ForEach(type => doWhatYouNeedToDo(type)));
if you are using T4MVC, then this script will return double entries. To avoid this, work with
&& !type.Name.Contains("T4MVC")
In the method doWhatYouNeedToDo() you could transform the Type object into a DTO that suits your needs and add work further with it.
As far as your dynamic menu is concerned, you could use the MvcSiteMapProvider and implement your own dynamic sitemapprovider with it, so you are no longer bound to the static sitemap xml file.
But reflection is quite slow in .NET, so you might want to store representations of your controllers and method in the database.
There is no built in mechanism in MVC to enumerate over all of your controllers and actions. You would have to use reflection to inspect all the loaded types and look at their methods and the associated attributes. Of course this is assuming that you are using the default reflection-based action dispatching mechanism. Since MVC's pipeline can be replaced in a number of places its easy to inject a system for invoking action methods that is not based on CLR classes and methods. But if you have complete control over your application than you life is easier.
Try TVMVC. You'll still have to use reflection, but the t4 templates will generate a class that's easier to iterate over.

MVC Patterns - controllers and views

When using the MVC pattern, should I implement a seperate controller for each view?
Create a new one if you need to. Don't if you don't.
Patterns are not about data structures, they're about organizational patterns among communicating components. If the same controller is appropriate for more than one view, great - especially if you can use it without modification.
If you have to change it, then you have a case for two separate controllers. If there is shared code between them, then consider moving it to another class - either a base class or (my personal preference) shared via aggregation.
The easiest way to think about MVC is a command-line program. The program is the Model. The Controller is STDIN. The View is STDOUT.
I believe there is no 'the' MVC pattern. There are almost as many MVC patterns as there are users of a MVC architecture. That being said, in my opinion, the answer to your question is 'no'.
I use to implement a controller for each module of my application, not for each view. A controller can call methods of other controller. I'm not sure if this is the better way to do it, but I think It's working well for me.
the idea is to separate/decoupling M, V and C, its not a problem if you want a single controller control multiple view, as long as the view and controller is decoupled

What design pattern? I need two modes in my app, edit and view

If I need two modes in my application what design pattern would I use so I can prevent ugly conditional code? App is currently MVC, but I don't want conditional code in my controllers and don't want two controllers for each view unless I have to.
Any suggestions?
A different subclass for each implementation, with shared functionality either in a common superclass or using the Template Method pattern.
Perhaps the State Pattern?
Abstract Factory, or Proxy. Your controller would contain some kind of Factory or Proxy instance that is used to retrieve a "mode" and act on it accordingly.
It is difficult to say for sure without more information, but I would suggest the strategy pattern. You could use the same controller and just swap out the strategy object to produce the desired change in behavior.
Here is an article you may find useful:
http://www.javaworld.com/javaworld/jw-04-2002/jw-0426-designpatterns.html
take a look at JSR-168, java portlet and its reference implementation, it should be similar to what you are trying to achieve.
The appropriate place for such a decision is the controller of MVC. I would recommend you write it there first. If it really is repetitive, it may be straightforward to figure out how to clean it up: you can move the conditional logic into a base class, or depending on the language, may be able to handle it with some sort of filter. You may also be able to create some "factory" for the views, which understands the "mode" of your application. Architecturally, though, all this is in the controller.
You are right to not want it in the view. This would be pretty messy. You probably want two versions of the views, one for "view" and one for "edit".
In the end, this is what controllers are for. Good luck!
In CafeTownsend demo made with PureMVC there is a similar situation where there are two different views and two separate Mediators. You absolute don't need conditional code for that. I don't know what technology and programming language you are using, but in Flex it will be a ViewStack with the ListView and EditView as children:
Corresponding mediator is registered by demand when the view is created. You can check other implementations using previous link.

Resources