Spring Boot-10modelandview, Model, and @modelattribute annotations

Source: Internet
Author: User


 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.