当客户端通过get或post请求发送来的参数通过Controller中方法的参数接受,叫做参数绑定

Controller方法的返回值1:返回void类型



[java] view plain copy


print

?



1. @RequestMapping(“/test_void.action”)  
2. public void controller01(HttpServletRequest request,HttpServletResponse response) throws
3. <span style=”margin:0px;padding:0px;white-space:pre;”>  </span>request.setCharacterEncoding(“utf-8”);  
4. <span style=”margin:0px;padding:0px;white-space:pre;”>  </span>String username = request.getParameter(“username”);//通过HttpServletRequest获得请求参数
5. ”用户名:”+username);  
6. ”username”,username);  
7. new
8. <span style=”margin:0px;padding:0px;white-space:pre;”>  </span>u.setUsername(username);  
9. <span style=”margin:0px;padding:0px;white-space:pre;”>  </span>userService.insertUser(u);//插入数据库
10. ”/WEB-INF/jsp/success.jsp”).forward(request, response);//转发
11. }



@RequestMapping("/test_void.action")
public void controller01(HttpServletRequest request,HttpServletResponse response) throws Exception{
  request.setCharacterEncoding("utf-8");
  String username = request.getParameter("username");//通过HttpServletRequest获得请求参数
    System.out.println("用户名:"+username);
    request.setAttribute("username",username);
    User u = new User();
  u.setUsername(username);
  userService.insertUser(u);//插入数据库
    request.getRequestDispatcher("/WEB-INF/jsp/success.jsp").forward(request, response);//转发
}


Controller方法的返回值2:返回ModelAndView



[java] view plain copy


print

?


1. @RequestMapping(“/test_modelandview.action”)  
2. public ModelAndView controller02(HttpServletRequest request) throws
3. ”utf-8”);//转码,Tomcat默认是iso-8859-1
4. ”username”);  
5. ”用户名:”+username);  
6. new ModelAndView();<span style=“margin:0px;padding:0px;white-space:pre;”>   </span>//new一个ModelAndView
7. ”username”,username);<span style=“margin:0px;padding:0px;white-space:pre;”>  </span>//相当于request.setAttribute(attrName,attrValue);
8. ”WEB-INF/jsp/success.jsp”);//视图跳转
9. return
10. }



@RequestMapping("/test_modelandview.action")
public ModelAndView controller02(HttpServletRequest request) throws Exception{
    request.setCharacterEncoding("utf-8");//转码,Tomcat默认是iso-8859-1
    String username = request.getParameter("username");
    System.out.println("用户名:"+username);
    ModelAndView modelAndView = new ModelAndView();   //new一个ModelAndView
    modelAndView.addObject("username",username);  //相当于request.setAttribute(attrName,attrValue);
    modelAndView.setViewName("WEB-INF/jsp/success.jsp");//视图跳转
    return modelAndView;
}


Controller方法的返回值3:返回String类型(逻辑视图)



[java] view plain copy


print

?


1. @RequestMapping(“/test_string.action”)  
2. public String controller03(HttpServletRequest request) throws
3. ”utf-8”);<span style=“margin:0px;padding:0px;white-space:pre;”>    </span>//转码
4. ”username”);  
5. ”username”,username);<span style=“margin:0px;padding:0px;white-space:pre;”>    </span>//设置请求参数
6. ”用户名:”+username);<span style=“margin:0px;padding:0px;white-space:pre;”>  </span>  
7. return “/WEB-INF/jsp/success.jsp”;<span style=“margin:0px;padding:0px;white-space:pre;”>    </span>//返回String类型,代表逻辑视图
8. }


@RequestMapping("/test_string.action")
public String controller03(HttpServletRequest request) throws Exception{
    request.setCharacterEncoding("utf-8");    //转码
    String username = request.getParameter("username");
    request.setAttribute("username",username);    //设置请求参数
    System.out.println("用户名:"+username);  
    return "/WEB-INF/jsp/success.jsp";    //返回String类型,代表逻辑视图
}


Controller方法的返回值4:方法的参数是Model,返回值是String类型(逻辑视图)



[html] view plain copy


print

?


1. @RequestMapping(“/test_model.action”)  
2. public String controller04(HttpServletRequest request,Model model) throws Exception{  
3. <span style=“margin:0px;padding:0px;white-space:pre;”>  </span>request.setCharacterEncoding(“utf-8”);  
4. <span style=“margin:0px;padding:0px;white-space:pre;”>  </span>String username = request.getParameter(“username”);  
5. <span style=“margin:0px;padding:0px;white-space:pre;”>  </span>model.addAttribute(“username”, username);<span style=“margin:0px;padding:0px;white-space:pre;”>  </span>//等价于request.setAttribute(attrName,attrValue);  
6. <span style=“margin:0px;padding:0px;white-space:pre;”>  </span>System.out.println(“用户名:”+username);  
7. <span style=“margin:0px;padding:0px;white-space:pre;”>  </span>return ”/WEB-INF/jsp/success.jsp”;<span style=“margin:0px;padding:0px;white-space:pre;”> </span>//返回String类型,跳转到逻辑视图  
8. }


@RequestMapping("/test_model.action")
public String controller04(HttpServletRequest request,Model model) throws Exception{
  request.setCharacterEncoding("utf-8");
  String username = request.getParameter("username");
  model.addAttribute("username", username);  //等价于request.setAttribute(attrName,attrValue);
  System.out.println("用户名:"+username);
  return "/WEB-INF/jsp/success.jsp"; //返回String类型,跳转到逻辑视图
}


Controller方法的返回值5:返回重定向redirect后的逻辑视图名



[java] view plain copy


print

?


1. @RequestMapping(“/test_redirect.action”)  
2. public String controller05(HttpServletRequest request) throws
3. ”utf-8”);  
4. ”username”);  
5. new
6.     u.setUsername(username);  
7.     userService.insertUser(u);  
8. ”username”,username);<span style=“margin:0px;padding:0px;white-space:pre;”>    </span>//由于是redirect,所以请求参数失效
9. return “redirect:/controller/test_model.action”;//redirect:重定向到一个Controller里
10. }


@RequestMapping("/test_redirect.action")
public String controller05(HttpServletRequest request) throws Exception{
    request.setCharacterEncoding("utf-8");
    String username = request.getParameter("username");
    User u = new User();
    u.setUsername(username);
    userService.insertUser(u);
    request.setAttribute("username",username);    //由于是redirect,所以请求参数失效
    return "redirect:/controller/test_model.action";//redirect:重定向到一个Controller里
}


Controller方法的返回值6:返回farward转发后的逻辑视图名



[java] view plain copy


print

?



    1. @RequestMapping(“/test_forword.action”)  
    2. public String controller06(HttpServletRequest request) throws
    3. ”utf-8”);  
    4. ”username”);  
    5. new
    6.     u.setUsername(username);  
    7.     userService.insertUser(u);  
    8. ”用户名:”+username);  
    9. ”username”,username);<span style=“margin:0px;padding:0px;white-space:pre;”>    </span>//由于是转发,所以请求参数有效
    10. return “forward:/controller/test_model.action”;//转发,跳转到一个Controller里
    11. }



    @RequestMapping("/test_forword.action")
        public String controller06(HttpServletRequest request) throws Exception{
        request.setCharacterEncoding("utf-8");
        String username = request.getParameter("username");
        User u = new User();
        u.setUsername(username);
        userService.insertUser(u);
        System.out.println("用户名:"+username);
        request.setAttribute("username",username);    //由于是转发,所以请求参数有效
        return "forward:/controller/test_model.action";//转发,跳转到一个Controller里
    }



    参数绑定

    参数绑定的第一种方法:绑定普通类型



    [java] view plain copy


    print

    ?


    1. //参数绑定的第一种方法:客户端提交的请求的input的name属性会和Controller方法的参数名字一致才会绑定
    2. @RequestMapping(“/test_parambinding01.action”)  
    3. public void controller07(HttpServletRequest request,HttpServletResponse response,String username,String password) throws
    4. //必须进行转码
    5. new String(username.getBytes(“iso8859-1”),“UTF-8”);  
    6. ”username”,username);  
    7. ”/WEB-INF/jsp/success.jsp”).forward(request, response);      
    8. }


    //参数绑定的第一种方法:客户端提交的请求的input的name属性会和Controller方法的参数名字一致才会绑定
    @RequestMapping("/test_parambinding01.action")
    public void controller07(HttpServletRequest request,HttpServletResponse response,String username,String password) throws Exception{
        //必须进行转码
        username = new String(username.getBytes("iso8859-1"),"UTF-8");
        request.setAttribute("username",username);
        request.getRequestDispatcher("/WEB-INF/jsp/success.jsp").forward(request, response);    
    }


    参数绑定的第二种方法:绑定pojo类

    [java] view plain copy


    print

    ?

    1. //参数绑定的第二种方法:客户端的input标签的那么属性必须和User的属性名对应才可以映射成功
    2. @RequestMapping(“/test_parambinding02.action”)  
    3. public ModelAndView controller08(HttpServletRequest request,User user) throws
    4. //必须进行转码
    5. new String(user.getUsername().getBytes(“iso-8859-1”),“utf-8”));  
    6.     userService.insertUser(user);  
    7. new
    8. ”utf-8”);  
    9. ”username”,user.getUsername());  
    10. ”password”,user.getPassword());  
    11. ”/WEB-INF/jsp/success.jsp”);  
    12. return
    13. }


    //参数绑定的第二种方法:客户端的input标签的那么属性必须和User的属性名对应才可以映射成功
    @RequestMapping("/test_parambinding02.action")
    public ModelAndView controller08(HttpServletRequest request,User user) throws Exception{
        //必须进行转码
        user.setUsername(new String(user.getUsername().getBytes("iso-8859-1"),"utf-8"));
        userService.insertUser(user);
        ModelAndView modelAndView = new ModelAndView();
        request.setCharacterEncoding("utf-8");
        modelAndView.addObject("username",user.getUsername());
        modelAndView.addObject("password",user.getPassword());
        modelAndView.setViewName("/WEB-INF/jsp/success.jsp");
        return modelAndView;
    }




    参数绑定的第三种方法:当input的name与controller的参数名不一致时,可以采用@RequestParam注解



    [java] view plain copy


    print

    ?


    1. @RequestMapping(“test_RequestParam.action”)  
    2. //将客户端的请求参数”username”与”uname”绑定
    3. public ModelAndView controller09(@RequestParam(value=“username”) String uname,@RequestParam(value=“password”)String pwd) throws
    4. new String(uname.getBytes(“iso-8859-1”),“utf-8”);     
    5. new
    6. ”username”,uname);  
    7. ”/WEB-INF/jsp/success.jsp”);  
    8. return
    9. }



    @RequestMapping("test_RequestParam.action")
    //将客户端的请求参数"username"与"uname"绑定
    public ModelAndView controller09(@RequestParam(value="username") String uname,@RequestParam(value="password")String pwd) throws Exception{
        uname = new String(uname.getBytes("iso-8859-1"),"utf-8");   
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("username",uname);
        modelAndView.setViewName("/WEB-INF/jsp/success.jsp");
        return modelAndView;
    }



    </div>


    当客户端通过get或post请求发送来的参数通过Controller中方法的参数接受,叫做参数绑定

    Controller方法的返回值1:返回void类型



    [java] view plain copy


    print

    ?


    1. @RequestMapping(“/test_void.action”)  
    2. public void controller01(HttpServletRequest request,HttpServletResponse response) throws
    3. <span style=”margin:0px;padding:0px;white-space:pre;”>  </span>request.setCharacterEncoding(“utf-8”);  
    4. <span style=”margin:0px;padding:0px;white-space:pre;”>  </span>String username = request.getParameter(“username”);//通过HttpServletRequest获得请求参数
    5. ”用户名:”+username);  
    6. ”username”,username);  
    7. new
    8. <span style=”margin:0px;padding:0px;white-space:pre;”>  </span>u.setUsername(username);  
    9. <span style=”margin:0px;padding:0px;white-space:pre;”>  </span>userService.insertUser(u);//插入数据库
    10. ”/WEB-INF/jsp/success.jsp”).forward(request, response);//转发
    11. }



    @RequestMapping("/test_void.action")
    public void controller01(HttpServletRequest request,HttpServletResponse response) throws Exception{
      request.setCharacterEncoding("utf-8");
      String username = request.getParameter("username");//通过HttpServletRequest获得请求参数
        System.out.println("用户名:"+username);
        request.setAttribute("username",username);
        User u = new User();
      u.setUsername(username);
      userService.insertUser(u);//插入数据库
        request.getRequestDispatcher("/WEB-INF/jsp/success.jsp").forward(request, response);//转发
    }


    Controller方法的返回值2:返回ModelAndView



    [java] view plain copy


    print

    ?


    1. @RequestMapping(“/test_modelandview.action”)  
    2. public ModelAndView controller02(HttpServletRequest request) throws
    3. ”utf-8”);//转码,Tomcat默认是iso-8859-1
    4. ”username”);  
    5. ”用户名:”+username);  
    6. new ModelAndView();<span style=“margin:0px;padding:0px;white-space:pre;”>   </span>//new一个ModelAndView
    7. ”username”,username);<span style=“margin:0px;padding:0px;white-space:pre;”>  </span>//相当于request.setAttribute(attrName,attrValue);
    8. ”WEB-INF/jsp/success.jsp”);//视图跳转
    9. return
    10. }


    @RequestMapping("/test_modelandview.action")
    public ModelAndView controller02(HttpServletRequest request) throws Exception{
        request.setCharacterEncoding("utf-8");//转码,Tomcat默认是iso-8859-1
        String username = request.getParameter("username");
        System.out.println("用户名:"+username);
        ModelAndView modelAndView = new ModelAndView();   //new一个ModelAndView
        modelAndView.addObject("username",username);  //相当于request.setAttribute(attrName,attrValue);
        modelAndView.setViewName("WEB-INF/jsp/success.jsp");//视图跳转
        return modelAndView;
    }


    Controller方法的返回值3:返回String类型(逻辑视图)



    [java] view plain copy


    print

    ?



    1. @RequestMapping(“/test_string.action”)  
    2. public String controller03(HttpServletRequest request) throws
    3. ”utf-8”);<span style=“margin:0px;padding:0px;white-space:pre;”>    </span>//转码
    4. ”username”);  
    5. ”username”,username);<span style=“margin:0px;padding:0px;white-space:pre;”>    </span>//设置请求参数
    6. ”用户名:”+username);<span style=“margin:0px;padding:0px;white-space:pre;”>  </span>  
    7. return “/WEB-INF/jsp/success.jsp”;<span style=“margin:0px;padding:0px;white-space:pre;”>    </span>//返回String类型,代表逻辑视图
    8. }



    @RequestMapping("/test_string.action")
    public String controller03(HttpServletRequest request) throws Exception{
        request.setCharacterEncoding("utf-8");    //转码
        String username = request.getParameter("username");
        request.setAttribute("username",username);    //设置请求参数
        System.out.println("用户名:"+username);  
        return "/WEB-INF/jsp/success.jsp";    //返回String类型,代表逻辑视图
    }


    Controller方法的返回值4:方法的参数是Model,返回值是String类型(逻辑视图)



    [html] view plain copy


    print

    ?


    1. @RequestMapping(“/test_model.action”)  
    2. public String controller04(HttpServletRequest request,Model model) throws Exception{  
    3. <span style=“margin:0px;padding:0px;white-space:pre;”>  </span>request.setCharacterEncoding(“utf-8”);  
    4. <span style=“margin:0px;padding:0px;white-space:pre;”>  </span>String username = request.getParameter(“username”);  
    5. <span style=“margin:0px;padding:0px;white-space:pre;”>  </span>model.addAttribute(“username”, username);<span style=“margin:0px;padding:0px;white-space:pre;”>  </span>//等价于request.setAttribute(attrName,attrValue);  
    6. <span style=“margin:0px;padding:0px;white-space:pre;”>  </span>System.out.println(“用户名:”+username);  
    7. <span style=“margin:0px;padding:0px;white-space:pre;”>  </span>return ”/WEB-INF/jsp/success.jsp”;<span style=“margin:0px;padding:0px;white-space:pre;”> </span>//返回String类型,跳转到逻辑视图  
    8. }


    @RequestMapping("/test_model.action")
    public String controller04(HttpServletRequest request,Model model) throws Exception{
      request.setCharacterEncoding("utf-8");
      String username = request.getParameter("username");
      model.addAttribute("username", username);  //等价于request.setAttribute(attrName,attrValue);
      System.out.println("用户名:"+username);
      return "/WEB-INF/jsp/success.jsp"; //返回String类型,跳转到逻辑视图
    }


    Controller方法的返回值5:返回重定向redirect后的逻辑视图名



    [java] view plain copy


    print

    ?


    1. @RequestMapping(“/test_redirect.action”)  
    2. public String controller05(HttpServletRequest request) throws
    3. ”utf-8”);  
    4. ”username”);  
    5. new
    6.     u.setUsername(username);  
    7.     userService.insertUser(u);  
    8. ”username”,username);<span style=“margin:0px;padding:0px;white-space:pre;”>    </span>//由于是redirect,所以请求参数失效
    9. return “redirect:/controller/test_model.action”;//redirect:重定向到一个Controller里
    10. }


    @RequestMapping("/test_redirect.action")
    public String controller05(HttpServletRequest request) throws Exception{
        request.setCharacterEncoding("utf-8");
        String username = request.getParameter("username");
        User u = new User();
        u.setUsername(username);
        userService.insertUser(u);
        request.setAttribute("username",username);    //由于是redirect,所以请求参数失效
        return "redirect:/controller/test_model.action";//redirect:重定向到一个Controller里
    }


    Controller方法的返回值6:返回farward转发后的逻辑视图名



    [java] view plain copy


    print

    ?


    1. @RequestMapping(“/test_forword.action”)  
    2. public String controller06(HttpServletRequest request) throws
    3. ”utf-8”);  
    4. ”username”);  
    5. new
    6.     u.setUsername(username);  
    7.     userService.insertUser(u);  
    8. ”用户名:”+username);  
    9. ”username”,username);<span style=“margin:0px;padding:0px;white-space:pre;”>    </span>//由于是转发,所以请求参数有效
    10. return “forward:/controller/test_model.action”;//转发,跳转到一个Controller里
    11. }


    @RequestMapping("/test_forword.action")
        public String controller06(HttpServletRequest request) throws Exception{
        request.setCharacterEncoding("utf-8");
        String username = request.getParameter("username");
        User u = new User();
        u.setUsername(username);
        userService.insertUser(u);
        System.out.println("用户名:"+username);
        request.setAttribute("username",username);    //由于是转发,所以请求参数有效
        return "forward:/controller/test_model.action";//转发,跳转到一个Controller里
    }



    参数绑定

    参数绑定的第一种方法:绑定普通类型



    [java] view plain copy


    print

    ?



      1. //参数绑定的第一种方法:客户端提交的请求的input的name属性会和Controller方法的参数名字一致才会绑定
      2. @RequestMapping(“/test_parambinding01.action”)  
      3. public void controller07(HttpServletRequest request,HttpServletResponse response,String username,String password) throws
      4. //必须进行转码
      5. new String(username.getBytes(“iso8859-1”),“UTF-8”);  
      6. ”username”,username);  
      7. ”/WEB-INF/jsp/success.jsp”).forward(request, response);      
      8. }



      //参数绑定的第一种方法:客户端提交的请求的input的name属性会和Controller方法的参数名字一致才会绑定
      @RequestMapping("/test_parambinding01.action")
      public void controller07(HttpServletRequest request,HttpServletResponse response,String username,String password) throws Exception{
          //必须进行转码
          username = new String(username.getBytes("iso8859-1"),"UTF-8");
          request.setAttribute("username",username);
          request.getRequestDispatcher("/WEB-INF/jsp/success.jsp").forward(request, response);    
      }


      参数绑定的第二种方法:绑定pojo类

      [java] view plain copy


      print

      ?


      1. //参数绑定的第二种方法:客户端的input标签的那么属性必须和User的属性名对应才可以映射成功
      2. @RequestMapping(“/test_parambinding02.action”)  
      3. public ModelAndView controller08(HttpServletRequest request,User user) throws
      4. //必须进行转码
      5. new String(user.getUsername().getBytes(“iso-8859-1”),“utf-8”));  
      6.     userService.insertUser(user);  
      7. new
      8. ”utf-8”);  
      9. ”username”,user.getUsername());  
      10. ”password”,user.getPassword());  
      11. ”/WEB-INF/jsp/success.jsp”);  
      12. return
      13. }


      //参数绑定的第二种方法:客户端的input标签的那么属性必须和User的属性名对应才可以映射成功
      @RequestMapping("/test_parambinding02.action")
      public ModelAndView controller08(HttpServletRequest request,User user) throws Exception{
          //必须进行转码
          user.setUsername(new String(user.getUsername().getBytes("iso-8859-1"),"utf-8"));
          userService.insertUser(user);
          ModelAndView modelAndView = new ModelAndView();
          request.setCharacterEncoding("utf-8");
          modelAndView.addObject("username",user.getUsername());
          modelAndView.addObject("password",user.getPassword());
          modelAndView.setViewName("/WEB-INF/jsp/success.jsp");
          return modelAndView;
      }




      参数绑定的第三种方法:当input的name与controller的参数名不一致时,可以采用@RequestParam注解



      [java] view plain copy


      print

      ?



      1. @RequestMapping(“test_RequestParam.action”)  
      2. //将客户端的请求参数”username”与”uname”绑定
      3. public ModelAndView controller09(@RequestParam(value=“username”) String uname,@RequestParam(value=“password”)String pwd) throws
      4. new String(uname.getBytes(“iso-8859-1”),“utf-8”);     
      5. new
      6. ”username”,uname);  
      7. ”/WEB-INF/jsp/success.jsp”);  
      8. return
      9. }



      @RequestMapping("test_RequestParam.action")
      //将客户端的请求参数"username"与"uname"绑定
      public ModelAndView controller09(@RequestParam(value="username") String uname,@RequestParam(value="password")String pwd) throws Exception{
          uname = new String(uname.getBytes("iso-8859-1"),"utf-8");   
          ModelAndView modelAndView = new ModelAndView();
          modelAndView.addObject("username",uname);
          modelAndView.setViewName("/WEB-INF/jsp/success.jsp");
          return modelAndView;
      }



      </div>