my dog learned polymorphism*
The moose likes Spring and the fly likes which is better ModelAndView or string return with controller Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "which is better ModelAndView or string return with controller" Watch "which is better ModelAndView or string return with controller" New topic
Author

which is better ModelAndView or string return with controller

kartik hedau
Greenhorn

Joined: Feb 12, 2013
Posts: 8
Hi all
i am newbie in spring MVC.
i want to know which is better way,either to return ModelAndView or just return String with controller?? I have implemented simple login demo with both way and both are working well.




Naresh Chaurasia
Ranch Hand

Joined: May 18, 2005
Posts: 356
Returning ModelAndView is better choice, as you can pass values from controller to target JSPs. Check out the following link for example: Spring MVC


SCJP 1.4, SCWCD1.4, OCA(1Z0-007)
Nam Ha Minh
Ranch Hand

Joined: Oct 31, 2011
Posts: 498

kartik hedau wrote:Hi all
i am newbie in spring MVC.
i want to know which is better way,either to return ModelAndView or just return String with controller?? I have implemented simple login demo with both way and both are working well.


Well, in my opinion, it depends on a particular context. Sometimes I find returning a String is useful, sometimes returning a ModelAndView object is preferred. It's flexible, depending one's need and habit.


PM Me If You Want to get Urgent Help on Java Programming
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1646
    
    7

I pretty much always return a String. Never a ModelAndView.

If I need a Model I just pass it in as a parameter

In the above code snippet I can add any thing I need to the model provided and it will be available to the view for rendering.


More often though you pass in a model attribute



In this case if its a POST you will probably get a Person object populated from the form being submitted. But if the person attribute is not there, then Spring will call the default constructor on the Person object and pass in a new instance of Person and automatically add it to the model for use in the view. In either event there is never a need to return a ModelAndView anymore.


[How To Ask Questions][Read before you PM me]
kartik hedau
Greenhorn

Joined: Feb 12, 2013
Posts: 8
Well, in my opinion, it depends on a particular context. Sometimes I find returning a String is useful, sometimes returning a ModelAndView object is preferred. It's flexible, depending one's need and habit.



thank you for your thoughts, but can you elaborate more on " depends on a particular context"
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1646
    
    7

That is the beauty, you can choose whatever you are most comfortable with.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: which is better ModelAndView or string return with controller
 
Similar Threads
How to handle form re-submission in spring-mvc-3
InternalResourceViewResolver failing JUnit test -- Spring Demo Source
Difference between handleRequest() and handle()
Spring MVC problem
Simple Spring Mvc