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";
        }
        
  • 相关阅读:
    Android MVP框架实现过程
    bga-banner-引导页滑动第三方控件
    好的习惯是成功的一半之开发
    Java基础复习之String字符串format处理
    ButterKnife--View注入框架的使用
    div阴影
    JavaScript函数的4种调用方法详解
    JavaScript的三种对话框是通过调用window对象的三个方法alert(),confirm()和prompt()
    HTML文字闪烁
    HTML文本框样式大全
  • 原文地址:https://www.cnblogs.com/sunxun/p/9053883.html
Copyright © 2011-2022 走看看