zoukankan      html  css  js  c++  java
  • Spring Boot—10ModelAndView、Model,以及@ModelAttribute注解


    package com.sample.smartmap.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    
    import com.sample.smartmap.entity.User;
    import com.sample.smartmap.service.UserService;
    
    @Controller
    @RequestMapping("/model")
    public class ModelAndViewController {
        
        @Autowired UserService userService;
        /**
         * 一个beetl模板测试。因为视图扩展名字是btl
         * @param userId
         * @param model
         * @return
         */
        @GetMapping(path = "/{userId}/get.html")
        public String getUser(@PathVariable Long userId,Model model) {
             User userInfo =  userService.getUserById(userId);
             //model.addAttribute(userInfo); 与下面一行作用一样,但这会有潜在问题
             model.addAttribute("user", userInfo);
             return "/userInfo.html";
        }
        /**
         * 使用freemaker模板测试,freemaker会寻找/userInfo.ftl 模板
         * @param userId
         * @param view
         * @return
         */
        @GetMapping(path = "/{userId}/get2.html")
        public ModelAndView getUser2(@PathVariable Long userId,ModelAndView view) {
             User userInfo =  userService.getUserById(userId);
             //model.addAttribute(userInfo);
             view.addObject("user", userInfo);
             view.setViewName("/userInfo");
             return view;
        }
        
        
    }


    package com.sample.smartmap.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.ModelAttribute;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import com.sample.smartmap.controller.form.OrderPostForm;
    import com.sample.smartmap.service.UserService;
    
    @Controller
    @RequestMapping("/modelattribute")
    public class ModelAttributeController {
        @Autowired UserService userService;
        /**
         * Controller方法中的公共放啊,调用方法前先调用此方法。
         * @param id
         * @param model
         */
        @ModelAttribute
        public void findUserById(@PathVariable Long id,Model  model) {
            model.addAttribute("user", userService.getUserById(id));
        }
        
        @GetMapping(path = "/{id}/get.json")
        @ResponseBody
        public String getUser(Model model) {
            System.out.println(model.containsAttribute("user"));
            return "success";
        } 
        
    }
  • 相关阅读:
    剑指OFFER之合并有序链表(九度OJ1519)
    剑指OFFER之反转链表(九度OJ1518)
    剑指OFFER之链表中倒数第k个节点(九度OJ1517)
    一分钟教你在博客园中制作自己的动态云球形标签页
    剑指OFFER之调整数组顺序使奇数位于偶数前面找(九度OJ1516)
    剑指OFFER之打印1到最大的N位数(九度OJ1515)
    剑指OFFER之矩形覆盖(九度OJ1390)
    剑指OFFER之数值的整数次方(九度OJ1514)
    剑指OFFER之变态跳台阶(九度OJ1389)
    剑指OFFER之二进制中1的个数(九度OJ1513)
  • 原文地址:https://www.cnblogs.com/gispathfinder/p/8921184.html
Copyright © 2011-2022 走看看