简体   繁体   中英

Model model in Controller's method parameters in Spring MVC

This may be a very naive question to ask but I would like to know as a beginner in Spring MVC framework instead of returning ModelAndView Object can we just return a jsp name from a Controller's method definitions ? Also Apparently all the methods seem to take Model model as a argument. What does they mean ?

@RequestMapping(value = "/", method = RequestMethod.GET)
     public String home(Locale locale, Model model) {

        logger.debug("Entering renderShoeStoreApplication method");

        //model.addAttribute("orderList", orderList);

        logger.debug("Exiting renderShoeStoreApplication method");
        return "home";
    }

Instead of we could also write I suppose

/*  
    @RequestMapping(value = "/", method = RequestMethod.GET)
     public ModelAndView home(Locale locale, Model model) {

        logger.debug("Entering renderShoeStoreApplication method");

        //model.addAttribute("orderList", orderList);

        logger.debug("Exiting renderShoeStoreApplication method");
        ModelAndView homeModel = new ModelAndView("home");
        return homeModel ;
    }*/

Please explain the basics . Thanks in advance.

i think you have got two question

  1. All the methods seem to take Model model as a argument

this is necessarily not required, you can get http request as param and you can derive your model from the request parameters. eventually spring mvc does this for you to make the work much simpler using binding-result after bean validation.

 @RequestMapping(value = "/", method = RequestMethod.GET)
 public String home(Locale locale, HttpRequest request) {
       String name = request.getParamter("name");
       // you can manually derive your model here
 }
  1. can we just return a jsp name from a Controller's method definitions ?

you can pass the just jsp name in your controller method, provided if you have configured your view resolver to handled this.

<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/views/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

if you return string from controller, by default controller direct to view resolver to resolve the view. pls check this SO post

You can go through the below resources to understand the basics of Spring MVC, first and foremost the official docs from spring MVC community and some of the quick heads up of the POC's using below links

  1. http://earldouglas.com/publications/barebones-spring-mvc/

  2. http://spring.io/guides/gs/serving-web-content/

  3. http://www.mkyong.com/tutorials/spring-mvc-tutorials/

    Why model is passed as a parameter to controller method and what is the purpose of ModelView object?..All such questions will get answered once you will get the basic understanding of Spring MVC architecture.

    And Yes, In the event that there isn't any data to be returned by the method, the controller method can simply return a String that represents the view that should be rendered.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM