spring mvc 接收参数方式

1.使用HttpServletRequest接收

java 代码

/**

* 测试 HttpServletRequest 接收参数

*

* */

@RequestMapping(value = "/test")

@ResponseBody

public String queryAll(HttpServletRequest request) {

System.out.println(request.getParameter("date"));

return request.getParameter("date");

}

jsp 代码

<div>

<form id="mobileCreateOrderListForm" method="post">

<table style="width:100%;">

<tr>

<td>日期:</td>

<td><input name="date" id="date" class="mini-datepicker" ></input></td>

</tr>

</table>

</form>

</div>

2.使用 @RequestParam 接收参数

java 代码 

/**

* 测试 @RequestParam 接收参数

*

* */

@RequestMapping(value = "/test")

@ResponseBody

public void queryDicByTypeId(@RequestParam Integer typeId){

System.out.println(typeId);

}

jsp 代码

<div>

<form id="mobileCreateOrderListForm" method="post">

<table style="width:100%;">

<tr>

<td>类型:</td>

<td><input name="typeId" id="typeId" class="mini-textbox" ></input></td>

</tr>

</table>

</form>

</div>


3.使用 @PathVariable 接收参数


Java 代码


/**

* 测试 @RequestParam 接收参数 一个参数

*

* */

@RequestMapping(value = "/testOne/{name}")

@ResponseBody

public void testOne(@PathVariable String name){

System.out.println(name);

}


/**

测试 @RequestParam 接收参数 多个参数

*

* */

@RequestMapping(value = "/testMany/name/{name}/sex/{sex}")

@ResponseBody

public void testMany(@PathVariable String name,@PathVariable String sex){

System.out.println(name);

}


3.使用 @RequestBody 接收参数

/**

测试 @RequestBody 接收参数  转换成对象 

*

* */

@RequestMapping(value = "/test")

@ResponseBody

public void testMany(@RequestBody People people){

System.out.println(people.getName());

   System.out.println(people.getSex());

}

jsp代码

<div>

<form id="mobileCreateOrderListForm" method="post">

<table style="width:100%;">

<tr>

<td>姓名:</td>

<td><input name="name" id="name" class="mini-textbox" ></input></td>

<td>性别:</td>

<td><input name="sex" id="sex" class="mini-textbox" ></input></td>

</tr>

</table>

</form>

</div>


时间: 2024-08-24 10:45:15

spring mvc 接收参数方式的相关文章

spring mvc接收参数方式,json格式返回请求数据

1 使用方法形参使用变量接收提交的数据 2 在方法的形参中使用模型接收数据 3 如果在提交的表单中有多个数据模型,需要创建一个新的Bean,里面的属性是要接收的对象变量. 4 接收提交的日期字符串,转换成Date类型.需要使用@InitBinder来转换 5 批量删除数据,使用数组接收要删除的id,在页面中使用相同name属性 6 批量提交,如何接收数据?需要新建一个Bean,List或者LinkedList/ArrayList来接收. 7 在两个不同的action方法之间执行转发.在retur

【spring mvc】后台spring mvc接收List参数报错如下:org.springframework.beans.BeanInstantiationException: Failed to instantiate [java.util.List]: Specified class is an interface

后台spring mvc接收List参数报错如下:org.springframework.beans.BeanInstantiationException: Failed to instantiate [java.util.List]: Specified class is an interface org.springframework.beans.BeanInstantiationException: Failed to instantiate [java.util.List]: Speci

spring mvc接收数组

(一)前言 对于springmvc接收数组的问题啊,我试验过几次,但是了有时候成功了,有时候失败了,也不知道为啥的,然后现在又要用到了,所以打算具体看看到底怎么回事,但是了我实验成功了顺便找了好多资料的. (二)spring mvc接收数组测试代码 @ResponseBody @RequestMapping(value = "/test/array", method = RequestMethod.POST) public JSON test(@RequestParam(value =

Spring基础系列14 -- Spring MVC 请求参数的几种获取方法

Spring MVC 请求参数的几种获取方法 转载:http://www.cnblogs.com/leiOOlei/p/3658147.html 一.      通过@PathVariabl获取路径中的参数 @RequestMapping(value="user/{id}/{name}",method=RequestMethod.GET) public String printMessage1(@PathVariable String id,@PathVariable String n

spring mvc接收ajax提交的JSON数据,并反序列化为对象

需求:spring mvc接收ajax提交的JSON数据,并反序列化为对象,代码如下: 前台JS代码: //属性要与带转化的对象属性对应 var param={name:'语文',price:16}; $.ajax({ url: "/book/adddata", type: "POST", dataType: 'json', //必需设定,后台@RequestBody会根据它做数据反序列化 contentType:"application/json&quo

spring MVC接收list,map,数组

spring mvc 接收复杂类型1.接收数组    1.jsp页面:        <form action="home/hello" method="post">           3:<input type="checkbox" name="ll" value="adfadf" id="myfile"><br/>                

如何让Spring MVC接收的参数可以转换为java对象

场景: web.xml中增加了一个DispatcherServlet配置,并在同级目录下添加了**-servlert.xml文件,搭建起了一个spring mvc的restful访问接口. 问题描述: Controller的@RequestBody, 如果参数定义类型为String,则可以获取到数据; 如果参数定义类型为其他java对象,就接收不到. 下面记录完整的解决方法: 1. web.xml <!-- spring mvc依赖的大环境,此参数会被ContextLoaderListener使

spring mvc接收JSON格式的参数

1.配置spring解析json的库   <dependency>         <groupId>org.codehaus.jackson</groupId>         <artifactId>jackson-mapper-asl</artifactId>         <version>1.9.8</version>         <type>jar</type>         &

Spring MVC 接收多个实体参数

在SpringMVC 的接收参数中,如果接收一个实体对象,只需要在方法参数中这样做:@RequestBody User user //单个的时候这样接收 @RequestMapping(value = "/user/default/save",method = RequestMethod.POST) public ResponseBasicBean saveUserAndMore(@RequestBody User user) { return null; } //批量的时候这样接收