PackageCom.sample.smartmap.controller;Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.stereotype.Controller;ImportOrg.springframework.ui.Model;Importorg.springframework.web.bind.annotation.GetMapping;Importorg.springframework.web.bind.annotation.PathVariable;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.servlet.ModelAndView;ImportCom.sample.smartmap.entity.User;ImportCom.sample.smartmap.service.UserService, @Controller @requestmapping ("/model") Public classModelandviewcontroller {@Autowired userservice userservice; /*** One beetl template test. Because the view extension name is BTL *@paramUserId *@parammodel *@return */@GetMapping (Path= "/{userid}/get.html") PublicString getUser (@PathVariable Long userid,model Model) {User userInfo=Userservice.getuserbyid (userId); //Model.addattribute (UserInfo), as in the following line, but there are potential problemsModel.addattribute ("User", UserInfo); return"/userinfo.html"; } /*** Using Freemaker template test, Freemaker will look for/USERINFO.FTL template *@paramUserId *@paramView *@return */@GetMapping (Path= "/{userid}/get2.html") PublicModelandview getUser2 (@PathVariable Long userid,modelandview view) {User UserInfo=Userservice.getuserbyid (userId); //Model.addattribute (userInfo);View.addobject ("User", UserInfo); View.setviewname ("/userinfo"); returnview; } }
PackageCom.sample.smartmap.controller;Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.stereotype.Controller;ImportOrg.springframework.ui.Model;Importorg.springframework.web.bind.annotation.GetMapping;ImportOrg.springframework.web.bind.annotation.ModelAttribute;Importorg.springframework.web.bind.annotation.PathVariable;Importorg.springframework.web.bind.annotation.PostMapping;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestParam;ImportOrg.springframework.web.bind.annotation.ResponseBody;ImportCom.sample.smartmap.controller.form.OrderPostForm;ImportCom.sample.smartmap.service.UserService, @Controller @requestmapping ("/modelattribute") Public classModelattributecontroller {@Autowired userservice userservice; /*** Public put in the Controller method, call this method before calling the method. * @paramID *@paramModel*/@ModelAttribute Public voidFinduserbyid (@PathVariable Long id,model Model) {Model.addattribute ("User", Userservice.getuserbyid (ID)); } @GetMapping (Path= "/{id}/get.json") @ResponseBody PublicString GetUser (model model) {SYSTEM.OUT.PRINTLN (Model.containsattribute ("User")); return"Success"; } }
Spring Boot-10modelandview, Model, and @modelattribute annotations