Spring MVC各种提交形式以及参数接收(form表单提交以及Json提交)
一晃就到了11月30号了,明天就是2018年的最后一个月份了,时间呀!!!!
form表单参数接收的三种方式:
- HttpServletRequest
@RequestMapping(value = "/getParmByReq.do", method = RequestMethod.POST)
@ResponseBody
public String getParmByReq(HttpServletRequest request) {
String name = request.getParameter("name");
int age = Integer.parseInt(request.getParameter("age"));
System.out.println("接收到的参数,name=" + name + ";age=" + age);
return "接收到的参数,name=" + name + ";age=" + age;
}
用curl 测试结果: (-d 表示post请求)
curl -d "name=admin&age=34" http://localhost:8080/ssm-req/form/getParmByReq.do
3. 一个个参数接收,单个参数按照名称接收:
@RequestMapping(value = "/requestParam.do", method = RequestMethod.POST)
@ResponseBody
public String getBarBySimplePathWithRequestParam(
@RequestParam(value = "name") String name,
@RequestParam(value = "age") Integer age
) {
System.out.println("接收到的参数,name=" + name + ";age=" + age);
return "接收到的参数,name=" + name + ";age=" + age;
}
用curl 测试结果:
curl -d "name=ceshi&age=22" http://localhost:8080/ssm-req/form/requestParam.do
4. 自定义实体类接收,使用注解:@ModelAttribute
@RequestMapping(value = "modelAttribute.do", method = RequestMethod.POST)
@ResponseBody
public String getModelAttribute(@ModelAttribute() User user) {
System.out.println("接收到的参数,name=" + user.getName() + ";age=" + user.getAge());
return "接收到的参数,name=" + user.getName() + ";age=" + user.getAge();
}
用curl 测试结果:
curl -d "name=jaywei&age=33" http://localhost:8080/ssm-req/form/modelAttribute.do
5. 不用流的形式接收
@RequestMapping(value = "/getNotStream.do")
@ResponseBody
public String getNotStream(String order) {
JSONObject jsonObject = JSONObject.parseObject(order);
System.out.println("接收到的参数,name=" + jsonObject.getString("name") + ";age=" + jsonObject.getString("age"));
return "接收到的参数,name=" + jsonObject.getString("name") + ";age=" + jsonObject.getString("age");
}
用curl 测试结果
curl -d "order={'name':'张三','age':12}" http://localhost:8080/ssm-req/form//getNotStream.do
小结:
推荐使用:@ModelAttribute 接收参数的方式。
PS: 解决中文乱码问题,在web.xml文件中加入字符过滤器:
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
JSON提交参数接收
使用@RequestBody注解,该注解是将参数放在请求体中
@RequestMapping(value = "getJson2.do",produces = "application/json")
public String getJson2(@RequestBody String userStr) {
User user = JSON.parseObject(userStr, User.class);
System.out.println("接收到的参数,name=" + user.getName() + ";age=" + user.getAge());
return JSON.toJSONString("接收到的参数,name=" + user.getName() + ";age=" + user.getAge());
}
用curl测试结果
curl -H "Content-Type:application/json" -X POST -d "{'name':'sange','age':12}" http://localhost:8080/ssm-req/json/getJson2.do
源码地址:
https://github.com/XWxiaowei/JavaWeb/tree/master/ssm-request-demo
作者:码农飞哥
微信公众号:码农飞哥