tags:

views:

454

answers:

1

I'm new to JSP and Tiles, as well as Java. We are currently replatforming our site using these, but I am confused as to when something should be put into a view preparer vs coming from the controller.

For example, the current page I am working on will have a tile for pagination, including the content type (X of Y content-type). My original plan was to use a View Preparer to take the data being sent in by the controller (HashMap) and output a couple attributes for the pagination tile, but a co-worker told me this should be done in the controller instead.

If this is the case, what is the point of the View Preparer? I'm just slightly confused. I checked the Tiles docs and they are pretty basic/unusable.

Can someone give me a proper use case for a View Preparer?

+1  A: 

The controller is for executing business or transactional logic. That is, in response to a user action the application needs to perform one or more actions and then decide which view to render. Thats why its called a controller because it "controls" the flow of an application. As the controller performs its job the changes to the data will have to become visible to the user. However, preparing that data for display is not the job of the controller. Just making sure that the necessary actions are done and that the data is available.

The view, in this case your jsp pages, will then take the data and display it. As I understand it the View Preparer helps you factor some aspects of the view out so that a preparation that may have to be done in several diffent views may be done by the Preparer.

So the example in the docs of preparing a menu is a good use case. Menus are sometimes dynamic, in the sense that it depends on the state of the system what exactly is displayed for the user. Let us say that you want to display a login link in the menu when the user is not logged in and remove that link from the menu when the user is logged in. Rather than code that logic into every page that has to display a menu you can use a ViewPreparer that generates the menu implementing any logic that is necessary. That View Preparer can then be associated with several pages.

Think of it more as manipulating the data for viewing rather than business logic.

Vincent Ramdhanie