Reputation:
I have the following situation - I have front controller, one controller, one model and several jsp pages. As I understand these jsp pages are my views. For every http request controller chooses one of jsp pages.
If we use service to worker pattern then we have the following scheme:
1) request-> 2) front controller-> controller-> model-> 3) front-controller-> view -> 4) responce
So front controller renders view. So, Controller must pass to front controller some object that will keep the name of jsp file. So my question, how to name the class of this object? View? But view is jsp..
Upvotes: 2
Views: 1789
Reputation: 9848
I think your biggest confusion is JSP vs. View. JSP is one of many templating technologies supported by Spring, others include Velocity, Freemarker, Thymeleaf, Jasper, etc. Your Controllers will typically return a View name (or it can be an actual View subclass object). View resolvers take over there, you can have chains of them for rendering different views and if one doesn't have the definition for the view you are looking for, it passes it to the next resolver in the chain. Once the View you returned from your Controller method is resolved to a JSP view or a Freemaker view or whatever other view it is, the underlying templating technology takes over and does the actual rendering.
For example, you could have a view called "widgets" which resolves to a JSP template and is then rendered by a JSP and another view called "widget-reports" which is rendered by Japser and produces an Excel report.
To summarize, Spring's View is the link between what you return from your controllers and whatever templating technology you use.
Upvotes: 1
Reputation: 143
In MVC design pattern, M is Model , V is View, C is Controller.
Controller is using as a tunnel to View and Model.
View is your JSP/HTML whatever you are using for representation
In case of simple MVC pattern, you can redirect view(i.e., jsp) from a controller (i.e., servlet) using below code :
ServletContext context = getServletContext();
RequestDispatcher dispatcher = context.getRequestDispatcher("/viewPage.jsp");
dispatcher.forward(request,response);
In case of Spring MVC pattern, you can redirect view from controller using below code :
ModelAndView modelAndView = new ModelAndView("/viewPage.jsp");
return modelAndView;
(You can also configure ViewResolver in dispatcher-servlet file and render the view contents.)
Upvotes: 1
Reputation: 176
I've recently been through this quandary myself and after some trial and error came to the following design pattern implementation
A Front Controller handles all incoming requests and delegates out to the required Controller. This usually consists of mapping the incoming URL to a file path somehow, but you may also be loading some initial data or base class, or setting up some application environment settings.
Once the Controller is loaded the Front Controller isn't used again until there is a new request. The (for want of a better word) 'Main' Controller now loads any required Model class(es) and calls methods in them to obtain any required data.
The Model has methods to extract and process the requested data from the data sources (i.e. a database). It cannot access anything except for other models and the DB connection.
Once the data has been loaded the Controller loads the relevant View class and 'pushes' the data into variables held within the View.
The View class itself has no 'higher' logic functions and has no access to the Model or Controller methods. It is really only there to provide methods for processing data into the required output media (i.e. html) through various getSomeVar() or renderSomeData() methods.
Finally the Controller calls the View's method to render the page, at which point the Controllers job is done. The View will load the necessary template files which will be interpreted to produce the output for the browser.
The template file(s) are mostly structure HTML with no programming logic. The data is only loaded into the page via calls to various renderSomething() methods within the View.
My thinking behind this was that the Controller should be in control of everything!
Upvotes: 0