千家信息网

Spring3 MVC中怎么获取请求参数

发表于:2025-12-03 作者:千家信息网编辑
千家信息网最后更新 2025年12月03日,本篇文章给大家分享的是有关Spring3 MVC中怎么获取请求参数,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。通过@PathVaria
千家信息网最后更新 2025年12月03日Spring3 MVC中怎么获取请求参数

本篇文章给大家分享的是有关Spring3 MVC中怎么获取请求参数,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。


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

@RequestMapping(value="user/{id}/{name}",method=RequestMethod.GET)    public String printMessage1(@PathVariable String id,@PathVariable String name, ModelMap model) {                System.out.println(id);        System.out.println(name);        model.addAttribute("message", "111111");        return "users";    }

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


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

```
a: b:
```

Java Pojo如下

    public class Pojo{        private String a;        private int b;    }

Java Controller如下

@RequestMapping(method = RequestMethod.POST) public String processSubmit(@ModelAttribute("pojo") Pojo pojo) {         return "helloWorld"; }

@RequestBody获取POST请求的FORM表单数据

@RequestBody接收的是一个Json对象的字符串,而不是一个Json对象。然而在ajax请求往往传的都是Json对象,后来发现用 JSON.stringify(data)的方式就能将对象变成字符串。同时ajax请求的时候也要指定dataType: "json",contentType:"application/json"这样就可以轻易的将一个对象或者List传到Java端,使用@RequestBody即可绑定对象或者List.

js代码






0