• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

Doubt regarding parameter of type Model in methods of spring controller

 
Ranch Foreman
Posts: 2684
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2684
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, you lost me, can't follow, please be more clear
 
Monica Shiralkar
Ranch Foreman
Posts: 2684
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2684
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2684
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2684
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
What? What, what, what? What what tiny ad:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic