Spring MVC Controllers Return Type

后端 未结 6 1917
别跟我提以往
别跟我提以往 2020-12-23 11:39

I\'ve seen examples where a controller returns a String (which indicates the view)

@RequestMapping(value=\"/owners/{ownerId}\", method=RequestMethod.GET)
pub         


        
6条回答
  •  既然无缘
    2020-12-23 12:34

    public String findOwner(@PathVariable String ownerId, Model model) {
      Owner owner = ownerService.findOwner(ownerId);  
      model.addAttribute("owner", owner);  
      return "displayOwner"
    }                       
    

    in this method the return type is String and we adding Model as a parameter so to add a value with model we will add like

     modelparam.addAttribute("obj",value);  
    

    and return to the displayowner to jsp as per view resolver

    public ModelAndView helloWorld() {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("helloWorld");
        mav.addObject("message", "Hello World!");
        return mav;
    }
    

    in this method the return type is ModelAndView so we will return the model and here the mav is the object of ModelAndView so here we should add like

    model.addObject("object","value");
    

    here the viewname is returned to helloworld.jsp as per the viewResolver

提交回复
热议问题