1

If i use the MVC pattern to create my Spring project, is it wrong to call the Controller from the View?

Is this schema right?:

  • View calls the Controller
  • Controller performs operations and put data result into the Model
  • View reads data from the Model

Edit:

In my index jsp there is a menu with several categories of articles. I want to pass the name of the category to the controller. The controller calls the method of a beans which executes a query and returns the list of articles presents into the database.The Controller puts this list into the model and the View read this list from the Model.

Thanks

Alex
  • 2,075
  • 5
  • 27
  • 39
  • That depends on your definition of "view call the controller". Following the MVC idiom, it would be 'wrong' for a view to directly reference a controller. It's normal for something like a form in an HTML view to have an action which makes a call to an HTTP endpoint, which is intercepted by a controller. It might be better to explain what your actual problem is... – Steve Nov 21 '14 at 11:23
  • In my index jsp there is a menu with several categories of articles. I want to pass the name of the category to the controller. The controller calls the method of a beans which executes a query and returns the list of articles presents into the database.The Controller puts this list into the model and the View read this list from the Model. – Alex Nov 21 '14 at 11:30

3 Answers3

1

Depends what you mean by calling. But yes, View doesn't know anything about the controllers. It sends HttpRequests, and than the mechanism doing what you describe kicks in. There's the famous schema from spring docs, basically your bullets described via diagram. The point with respect to your question is that the view doesn't call the controller rather sends the request

enter image description here

Master Slave
  • 27,771
  • 4
  • 57
  • 55
  • Posted the same article here! Lol – julien carax Nov 21 '14 at 11:23
  • In my index jsp there is a menu with several categories of articles. I want to pass the name of the category to the controller. The controller calls the method of a beans which executes a query and returns the list of articles presents into the database.The Controller puts this list into the model and the View read this list from the Model. – Alex Nov 21 '14 at 11:31
1

I think you will find your answers in article mentioned below :

http://docs.spring.io/spring-framework/docs/2.5.3/reference/mvc.html

julien carax
  • 323
  • 6
  • 22
  • In my index jsp there is a menu with several categories of articles. I want to pass the name of the category to the controller. The controller calls the method of a beans which executes a query and returns the list of articles presents into the database.The Controller puts this list into the model and the View read this list from the Model. – Alex Nov 21 '14 at 11:30
  • Refer to this particular solution, it shows how you use form to send the data to your controller via RequestMapping. This should help you!! http://stackoverflow.com/questions/5590036/passing-parameters-from-jsp-to-controller-in-spring-mvc – julien carax Nov 21 '14 at 11:35
  • I want to say if generally it's wrong to call a controller from a jsp – Alex Nov 21 '14 at 11:37
1

What you say (in your comments) is not specially wrong, but it does not make sense.

Either the categories are known when you build the view, and then it is the controller role to collate all information and put it into the model before calling the view with the model.

Or the category is chosen through a user interaction. But at this moment, the JSP is over for a long time : the response has been committed and transmitted to the browser. The only possibility is to prepare a new request (with a form or with ajax), send this new request to the server, where it will be handled by a controller, which will collate data into a (new) model and pass it all to a view

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • The category is chosen by clicking to a link of a menu bar – Alex Nov 21 '14 at 11:48
  • @Alex In this sense, it is a perfectly normal view -> controller interaction. But in fact what you have is view -> browser -> controller not jsp -> controller – Serge Ballesta Nov 21 '14 at 13:59
  • Why do you say that "jsp is over for a long time"?Every Spring book that i have read uses Jsp. Many companies ,in my country, use jsp instead of jsf. – Alex Nov 21 '14 at 15:25
  • @Alex: I did not mean JSP in general is deprecated. JSP + JSTL is indeed a solid platform. I meant the JSP view has ended his job by the time when a user can click on it browser. The JSP is executed on server and the user clicks later on the client. – Serge Ballesta Nov 21 '14 at 16:15
  • Sorry if I didn't understand you correctly .. but i speak and write english not very well – Alex Nov 21 '14 at 16:29
  • @Alex : You are welcome. My english is often incorrect too, but this is an english language site :-) – Serge Ballesta Nov 21 '14 at 16:44