• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

Doubt regarding parameter of type Model in methods of spring controller

 
Ranch Foreman
Posts: 2076
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I seen some Spring MVC controller code with parameter of the type Model. I have used spring controller but never used parameter of type Model. What is this parameter of type model in spring controller method for. Is it mandatory(I do not thing so as I have never used it and still run spring MVC code).

thanks
 
Ranch Hand
Posts: 56
4
Mac OS X IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Monika,

dou you know the meaning of "mvc"? Model, View Controller. Look this up on google and read about it. In short, it's a design pattern that aims to separate different types of logic into their own components to simply coding as well as to improve code reuse.
The model is the "thing" that contains your data and your business logic. The model is also used as a means to store data that should later be displayed in the view (UI). So in the controller, store the data to be displayed into the view, which in spring mvc, as far as I remember
is just something like a simple map. In the view, you can then later access this model to display it - this is a way to decouple your controller from the view.

Does that help?

Cheers,

Marcus
 
Monica Shiralkar
Ranch Foreman
Posts: 2076
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.

I am aware of MVC and have worked on MVC frameworks like Struts,Spring MVC. I know that model is used to store data but I have known model in model view controller as a concept but not as a keyword or type. Even if we are not having a parameter of type model in spring controller method, still it functions as MVC as the concept of model is used there. My doubt is regarding model the keyword,the type rather than model the concept.

 
Marcus Biel
Ranch Hand
Posts: 56
4
Mac OS X IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, you lost me, can't follow, please be more clear
 
Monica Shiralkar
Ranch Foreman
Posts: 2076
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I meant that I understand Model the concept (of MVC) but by doubt is regarding Model the keyword,the type (as used parameters of Spring controller method)
 
Marcus Biel
Ranch Hand
Posts: 56
4
Mac OS X IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is not just a "keyword". That is meant to be the model in mvc, well, at least a simplified version of it - It's just a map, as said before.
 
Monica Shiralkar
Ranch Foreman
Posts: 2076
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will explain the doubt with an example.In the below code there is model of type ModelMap in the parameter of spring controller method but is is not used at all. Without it , the code still works fine.


thanks
 
Ranch Hand
Posts: 672
4
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, In your example, there is no need to include ModelMap attribute, if you are not using it.

ModelAndView as the name suggests includes both Model and the name of the view. ModelMap includes only model.
Controller allows to return both ModelAndView as single object using ModelAndView class.

Using ModelMap.addAtrribute we can add the data to model, and return the view name as String, like below.


 
Monica Shiralkar
Ranch Foreman
Posts: 2076
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks

Using ModelMap.addAtrribute we can add the data to model, and return the view name as String, like below



Yes, I had read this but I could not understand any difference which it makes if we do as above or by returning ModelandView object.
 
Prasad Krishnegowda
Ranch Hand
Posts: 672
4
Eclipse IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Returning String object and passing data through ModelMap is a newer approach in Spring, this approach will be familiar for people coming from struts background.
This said, it does not mean newer approach is better, hardly there are any difference.
 
Monica Shiralkar
Ranch Foreman
Posts: 2076
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks. In my code since I am not using the ModelMap at all and rather the modelandview parameter, let me try to remove the parameter and check.

 
Morning came much too soon and it brought along a friend named Margarita Hangover, and a tiny ad.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic