zoukankan      html  css  js  c++  java
  • 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";
        }
        
  • 相关阅读:
    Swift3 重写一个带占位符的textView
    Swift3 使用系统UIAlertView方法做吐司效果
    Swift3 页面顶部实现拉伸效果代码
    Swift3 倒计时按钮扩展
    iOS 获取当前对象所在的VC
    SpringBoot在IDEA下使用JPA
    hibernate 异常a different object with the same identifier value was already associated with the session
    SpringCloud IDEA 教学 番外篇 后台运行Eureka服务注册中心
    SpringCloud IDEA 教学 (五) 断路器控制台(HystrixDashboard)
    SpringCloud IDEA 教学 (四) 断路器(Hystrix)
  • 原文地址:https://www.cnblogs.com/sunxun/p/9053883.html
Copyright © 2011-2022 走看看