I'm building a Java web app that follows the MVC model.
I have a number of JSP pages that act as views for the respective objects in the DB, for example product_details.jsp.
I plan to use URLs like product_details.jsp?id=123, and since I have quite a lot of View pages, I was wondering if it was necessary to write a servlet for each of them that only retrieves data from a DAO or if there was a clean way to call a DAO from inside the JSP itself.
Would this violate the MVC model even if no controller is needed? What would be the best approach?
Related
I have a form which collects user input. Then a servlet collects all the request parameters and stores it to the database. As per MVC I should have passed it to a bean and the bean should have updated the database. Is it bad programming?
Also I have to parameters "title" and "source URL". I want the title to embed the source URL.
The JSP is your view, and the Servlet is your controller.
To have full MVC, there should be another Java class who's job is simply to represent the objects being stored; that's the model. Or it may be more than one class, if the form takes multiple objects, depending on your data model.
If you're writing SQL directly into a Servlet, that's not great code; any changes you ever make to your database will require a painful update to many of your Servlets. The idea of pulling the data model code away into another class lets you make changes to the data and database without having to edit everything you write.
So, if you're writing a really simple app, this is likely fine. If you're writing something complex, this is fine for a prototype, but you'll want a stricter MVC implementation if it's something you'll have to build, maintain, and own for any length of time.
I want to know data flow in the Struts 1.2 framework .
I found Different components like
struts.XML
action class
form class
Vo class
Dao class
After studying a lot I came know that dao act as communicator with database. One thing I can't understand is the form concept of struts and what is the vo class means? How vo class is populated using the value given in the front end.
Please help.
Well todays world belong to new generation of MVC frameworks like Struts 2, Spring MVC etc which helps developer to focus on their core work in cleaner,modular and scalable style. If you are learning better to start with them. But if you are working on some legacy project then it makes sense. I used to work on struts 1.2 9 years before. Here is what i recall
Major components are Jsp(View), Action(controller), Form(Model/backing bean for form populated data ).
When you submit the form on jsp, request goes to web.xml where it founds the mapping for Struts 1.2 front controller, This controller internally resolves the request path mapping from struts.xml and inititates the action. Front controller also populates the form bean from request parameter and supplies it as mathod parameter to called action method. Main point to remember is there will be single instance of action class per container, so it should be stateless. There are different kind of actiona which you can explore further.
VO are the value objects . Some calls them domain objects(DO)/Data Transfer Objects(DTO)/Java beans.
Basically they represent the represent the persistent entity in Database and act as a carrier.
DAO/Service layer is not a part of struts framework. Its your business layyer. Basically struts work ends at action class beyond which it further delegates the call to service layer.
What is the difference between the push & pull models of MVC?
Is Struts2, Spring MVC Pull based?
According to Struts2 Interview Questions and Answers
Struts2 is a Pull-MVC based architecture, in which all data is stored in Value Stack and retrieved by view layer for rendering.
Specifically:
In case of Push-MVC the data (Model) is constructed and given to the
view layer by the Controllers by putting it in the scoped variables
like request or session. Typical example is Spring MVC and Struts1.
Pull-MVC on the other hand puts the model data typically constructed
in Controllers are kept in a common place i.e. in actions, which then
gets rendered by view layer.
The concept of push and pull refers to what the server does in relation to the client. In a "push" application, a server is pushing or sending data to clients at it's own initiation. In a "pull" application, the server is pulling or waiting for and receiving messages initiated by clients.
A good explanation is given here mvc-pattern-its-importance-push-pull and here pull-vs-push-mvc-architecture
Struts1 and Spring both use Push MVC. This question might be helpful spring-mvc-complex-model-population-from-multiple-sources Struts2 uses Pull
Struts2, Play! and so on are all kinds of pull model implementations of the MVC pattern.
Terms "push" and "pull" refer directly to the type of implementation of the observer pattern used between View and Model. As stated in GoF Observer pattern explaination, we have:
At one extreme, which we call the push model, the subject sends observers detailed information about the change, whether they want it or not. At the other extreme is the pull model; the subject sends nothing but the most minimal notification, and observers ask for details explicitly thereafter.
This means that the implementation of push model requires that View and Model are implemented using the same language and they are executed in the the same environment. Good examples of this kind of implementation are Javascript single page applications, in which View and Model components execute inside the browser and a framework, i.e. Backbone, provides MVC (a.k.a. Observer) mechanism. Often, Model component interacts with some kind of server API, to persist and get persisted datas.
On the other hand, pull model lets you implement MVC using different technologies for View component, and Controller / Model components. In this kind of MVC, there is not an explicit use of the Observer pattern and View interacts exclusively with Controller. View component, which usually executes into the browser, sends to Controller component request of model's updates or model's state. Usually requestes are implemented using HTTP protocol. This kind of implementation requires the use of some type of "augmented HTML scripting language", such as JSP, which allows to create automatically the link between View and Controller.
Sorry .. I dont think that struts 1, struts 2 and spring MVC can be taken as PUSH MVC ..
as both of all frameworks use a front controller[Filer class for Struts , And Controller Listener for Spring ] defined in their respective deployment descriptor. Now both of all these frame work save the form data in their respective bean[Or model] object using that controller by reflection.
Now from our Action Controller we can receive the bean object and will get the values but in the front controller where the bean object[or model] is actually generated set the value in their resperctive field by using request.getParameter(param) or request.getParameterValues(param) methods internally.. So this can be considered as PULL.
So as per my thinking we can say that These framework can use two layers namely PULL layers used by end programmer and PUSH layers used by framework base classes.
I try understand architecture MVC for a Java Web App, I am doing a project where I try implement it. But I have a doubt. I have a HTML form (View) where users input some data. This form will have to send to a servlet (Controller) which updates a Java Bean (Model). At here all fine.
But, what happen when the form input data affects two or more models? How is have to be the controller responsable about manage this models?
Because I think only can exists 1 Controller - for - 1 Model ¿What is the pattern for that?
The controller will parse the form and update any number of models accordingly. There is no strict 1 to 1 relationship between a controller and a model.
I would do just the opposite:
1 view has 1 controller and can talk to one or multiple models.It makes sense to me as the model is long living and the views can easily be replaced, but when you replace a view you will have to replace it's corresponding controller.
In your specific case this means that your HTML can post to a servlet that can process the http POST made, and communicates this to the different models.
I have some text data from database that I want to display it on all pages on my site. (for example news block etc.) How can I load and pass it to View in Spring Framework 3.0?
I don't want to create some method, which will retrieve data, and call it from each controller...
Use a HandlerInterceptor, which is built for this purpose - you can specify a piece of logic to execute before or after the execution of your controllers (or a list of controllers - it is up to you to map an interceptor to the controllers it should apply to) by implementing a preHandle() and/or postHandle() method. The latter receives the ModelAndView as an input, allowing you to add model attributes to it.
This way the logic to add some data to the model exists in just one place, can be selectively configured to apply to all or some pages, etc.