• SpringMVC之学习(2)值得接收和传递


    springmvc中

    @Controller 来标识一个控制器

    @RequestMapping来标识请求路径,可以写在类名上,也可以写在方法名上。写在类,表示所有的方法都在此路径下。

    package com.sun.action;
    
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    @RequestMapping("/view")
    public class ViewPageController {
    
        
        @RequestMapping("/index")
        public String Index(){
            
                
            return "index";
        }
        
        
        @RequestMapping("/index2")
        public String Index2(){
            
                
            return "index2";
        }
    }

    传递值给页面显示的方式

    一种是Model 一种是ModelAndView

        @RequestMapping("/hello")
        public String hello(Model model){
            
            //讲参数传递给页面显示
            model.addAttribute("name", "page name ====== hello");
            return "hello";
        }

    这里return 的内容就是jsp的名字。

        //返回页面,同时返回值
        @RequestMapping(value = "/queryListMV.do")
        public ModelAndView queryListMV(HttpServletRequest request,
                HttpServletResponse response) {
            ModelAndView mv = new ModelAndView();
            mv.setViewName("/newuser"); //返回页面名
            mv.addObject("data", new User());//返回map对象
            return mv;
        }

    这里ViewName 里面的内容就是jsp页面的名。

    参数的接收

    第一种,当时是直接从request里面接收表单或者URL传过来的值。

        @RequestMapping(value = "/queryListMV2.do",method=RequestMethod.GET)
        public ModelAndView queryListMV2(HttpServletRequest request,
                HttpServletResponse response) {
            
            String name  = request.getParameter("name");
            Integer age  = Integer.valueOf(request.getParameter("age"));
            
            User user = new User();
            user.setName(name);
            user.setAge(age);
            ModelAndView mv = new ModelAndView();
            mv.setViewName("/newuser"); //返回页面名
            mv.addObject("data",user);//返回map对象
            return mv;
        }

    第二种 从path 上获取指定的参数

    	/*
    	 * @PathVariable 指定path上面的参数
    	 */
    	@RequestMapping("/pathview/{age}/{years}/{month}")
    	public String pathview(Model model,
    			@PathVariable(value="age") String age,
    			@PathVariable(value="years") String years,
    			@PathVariable(value="month") String month
    			){
    		
    			model.addAttribute("age", age);
    			model.addAttribute("years", years);
    			model.addAttribute("month", month);
    			model.addAttribute("name", "page name ====== pathview");
    		
    		
    		return "pathview";
    	}  

    第三种:用requestParam 接收

        /*
         * @RequestParam 用来接收超链接的参数,可以设置默认值
         */
        
        @RequestMapping("/userInfo")
        public String userInfo(Model model,@RequestParam(value="name",defaultValue=adminuser) String name){
            
            
            if("admin".equals(name)){
                //讲参数传递给页面显示
                model.addAttribute("name", "page name ====== "+ name);
            }else{
                model.addAttribute("name", "page name ====== "+ name);
            }
            
            return "userInfo";
        }

    第四种, 用 @ModelAttribute接收表单对象

        /*
         *  * 传递对象,通过对象,接受form表单的对象值
         */
         
        @RequestMapping(value="/adduser",method=RequestMethod.POST)
        public String adduser(Model model,@ModelAttribute("SpringWeb")User user){
            
            model.addAttribute("name", user.getName());
            model.addAttribute("age", user.getAge());
            model.addAttribute("id", user.getId());
            
            System.out.println("12231313");
            return "adduser";
        }
        
  • 相关阅读:
    398. Random Pick Index
    382. Linked List Random Node
    645. Set Mismatch
    174. Dungeon Game
    264. Ugly Number II
    115. Distinct Subsequences
    372. Super Pow
    LeetCode 242 有效的字母异位词
    LeetCode 78 子集
    LeetCode 404 左叶子之和
  • 原文地址:https://www.cnblogs.com/sunxun/p/9053883.html
Copyright © 2020-2023  润新知