zoukankan      html  css  js  c++  java
  • springMVC接收请求参数的几种方式

    1.  用注解@RequestParam绑定请求参数

    用注解@RequestParam绑定请求参数a到变量a,当请求参数a不存在时会有异常发生,可以通过设置属性required=false解决,例如: @RequestParam(value="a", required=false)

    JS与 controller 如下: js中post数据为json对象时:

     

    对应controller中的写法:

    2. @ModelAttribute获取POST请求的FORM表单数据

    jsp页面如下:

    其中,onsubmit 属性 用来在提交表单时执行一段 JavaScript。

     对应的controller如下: 表单中的name属性值是controller中User 这个POJO中的属性,这样后台自动接收表单中的数据,封装到User中

        3.  通过@PathVariabl获取路径中的参数

    例如:

     1     @RequestMapping(value="user/{id}/{name}",method=RequestMethod.GET)
     2 
     3     public String printMessage1(@PathVariable String id,@PathVariable String name, ModelMap model) {
     4 
     5 
     6 
     7         System.out.println(id);
     8 
     9         System.out.println(name);
    10 
    11         model.addAttribute("message", "111111");
    12 
    13         return "users";
    14 
    15     }

     访问user/123/lei路径时,执行以上方法,其中,参数id=123,name=lei

     4.最原始的方法:直接用HttpServletRequest获取

    1 @RequestMapping(method = RequestMethod.GET) 
    2 public String get(HttpServletRequest request, HttpServletResponse response) { 
    3    System.out.println(request.getParameter("a")); 
    4     return "helloWorld"; 
    5 }

    参考资料: https://www.cnblogs.com/blog411032/p/5909512.html

  • 相关阅读:
    让用户打开你app的位置功能
    函数递归与栈的关系
    公务员考试
    毕达哥拉斯的故事
    OC5_NSMutableString操作
    OC4_NSString操作
    OC_NSString
    OC3_MyRect
    OC6_类方法
    OC5_构造方法与self指针
  • 原文地址:https://www.cnblogs.com/enjoyjava/p/9280156.html
Copyright © 2011-2022 走看看