/**
* 微科集团-技术中心
* Date:2017-9-29上午9:23:40
* Copyright (c) 2017, veeker All Rights Reserved.
*
*/
package com.cls.wx.order;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
/**
* 功能描述: springMvc数据传输
* Date: 2017-9-29 上午9:23:40 <br/>
* @author xiaopan
* Description:请求参数Controller层
*/
@Controller
@RequestMapping("/request")
public class TestController {
/**
* 第一种交互,简单配置
* 不用@RequestParam,默认required=false
* 请求方式没有变化:get或者post
* @param par
* @param map
* 这种方式支持get和post,参数可选
* 访问方式简单的比如http://localhost:8080/springmvc-param/request/get0?p1=xxx。
*/
@RequestMapping(value="get0")
public void get0(String par,ModelMap map){
map.addAttribute("par", par);//网页面传递
}
/**
* 这种方式支持get,参数必须
* 这种方式和第一种不同的是,指定了访问访问必须为GET,而且参数是必须的,可以通过如下方式访问这个地址:
* http://localhost:8080/springmvc-param/request/get1?p1=xxxx。
* value="p1"表示参数名称<br>
* required=true表示如果没有传递参数"p1",则会报400参数异常<br>
* 使用void表示约定的路径,即request/get1.jsp
* @param p1
* @param map
*/
@RequestMapping(value="get1",method=RequestMethod.GET)
public void get1(@RequestParam(value="p1",required=true)String p1,ModelMap map){
map.addAttribute("p1", p1);//往页面传递
}
/**
* 这种方式仅支持GET,参数可选,这个方法和第二种唯一不同的就是参数是可选的,其他没有不同。
* 和get1不同的是,p1这个参数不一定非得需要,即使不给这个参数,也可以正常运行<br>
* 返回String是视图的名称,只要将map赋值,给的值也会带到前台页面
* @param p1
* @param map
* @return
*/
@RequestMapping(value="get2",method=RequestMethod.GET)
public String get2(@RequestParam(value="p1",required=false)String p1,ModelMap map){
map.addAttribute("p1", p1);//往页面传递
return "request/get2";
}
/**
* 这种方式仅支持GET,参数可选
* ModelAndView表示绑定了数据的视图,可以通过EL表达式去取值。
* 和get2不同的是,返回的对象是ModelAndView
* 表示绑定了视图和数据的对象,数据就是ModelMap中的Key-Value
* @param p1
* @param map
* @return
*/
@RequestMapping(value="get3",method=RequestMethod.GET)
public ModelAndView get3(@RequestParam(value="p1",required=false)String p1,ModelMap map){
map.addAttribute("p1", p1);
return new ModelAndView("request/get2",map);
}
/**
* 跳转到页面
* @throws NoSuchAlgorithmException
*/
@RequestMapping("userForm")
public String userForm(HttpServletResponse response) throws NoSuchAlgorithmException{
CookieUtils.writeCookie(response, -1, "x", "dddddddddddddd");
return "request/userForm";
}
先访问http://localhost:8080/springmvc-param/request/userForm这个方法,
跳转到一个页面,并向浏览器写入Cookie,第二个方法访问的时候即可通过@CookieValue方式来取到Cookie中的值
/**
* 读取Cookie的值
* @param x
* @return
*/
@ResponseBody
@RequestMapping("cookie")
public String cookie(@CookieValue("x")String x){
return x;
}
/**
* 绑定数据到User对象,支持Map,Set,List,Array等,但是需要使用下标,不是很灵活
* 请查看user2的写法
* @param user
* @param map
*/
绑定数据到一个对象上,支持get和post
一个User,一个Phone,一个User拥有多个Phone,为了演示,User中有一个List和Array的Phone的集合
public class User {
private String userName;
private String address;
private List<Phone> phones;
private Phone[] phones2;
//省略GET和SET...
}
public class Phone {
private String brand;//手机品牌
}
//承接上面
@RequestMapping(value="user")
public void user(User user,ModelMap map){
map.addAttribute("user", user);
}
/**
* 这里可以接受List,Array,Set等,写法是一样的,注意前端写法<br>
* 另外这个必须要使用MappingJacksonHttpMessageConverter这个消息转换器
* 请看我上面的配置
* @param user
* @return
* ,仅支持post,但是必须要在消息转换器中配置JSON解析器
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
并注册到RequestMappingHandlerAdapter的messageConverters中。
*/
@ResponseBody
@RequestMapping("user2")
public String user2(@RequestBody List<User> user){
System.out.println(user.size());
return "";
}
var userList= new Array();
userList.push({userName:"xx",address:"fff"});
userList.push({userName:"zzzz",address:"ggggg"});
$.ajax({
url:"request/user2",
type:"post",
data:JSON.stringify(userList),
dataType:"json",
contentType:"application/json",
success:function(data){
},error:function(data){
}
});
该方法仅支持POST的方式,会使用到json2.js这个类库,注意设置contentType:"application/json"这个属性,否则会报415未知的类型异常。
/**
* 这个方法只支持POST
* @param s
* @return
*/
var array=new Array();
array.push(1);
array.push(2);
array.push(3);
array.push(4);
array.push(5);
$.ajax({
url:"request/array",
type:"post",
dataType:"json",
data:JSON.stringify(array),
dataType:"json",
contentType:"application/json",
success:function(data){
},error:function(data){
}
});
和上面的方法类似,注意contentType:"application/json",否则同样的415错误
@ResponseBody
@RequestMapping("array")
public String array(@RequestBody String[] s){
System.out.println(s.length);
return "";
}
/**
* 这个比较奇葩,来自一位朋友的写法,即.xxx/5,4这样的请求,SpringMVC竟然也是支持的
* @param id
* @return
*/
@ResponseBody
@RequestMapping(value="array/{id}",method=RequestMethod.GET)
public String array2(@PathVariable("id")Long[] id){
System.out.println(id.length);
return "array length:"+id.length+"";
}
/**
* 一个表单对应多个Bean对象,这些Bean中有相同的属性,那么需要在分装他们的一个整体的对象
* 使之支持object.property的表达式
* @param c
*/
@ResponseBody
@RequestMapping("complex")
public void complexObject(C c){
System.out.println(c.getA().getX());
System.out.println(c.getB().getX());
}
/**
* 读取Cookie的值
* @param x
* @return
*/
@ResponseBody
@RequestMapping("cookie")
public String cookie(@CookieValue("x")String x){
return x;
}
}
* 微科集团-技术中心
* Date:2017-9-29上午9:23:40
* Copyright (c) 2017, veeker All Rights Reserved.
*
*/
package com.cls.wx.order;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
/**
* 功能描述: springMvc数据传输
* Date: 2017-9-29 上午9:23:40 <br/>
* @author xiaopan
* Description:请求参数Controller层
*/
@Controller
@RequestMapping("/request")
public class TestController {
/**
* 第一种交互,简单配置
* 不用@RequestParam,默认required=false
* 请求方式没有变化:get或者post
* @param par
* @param map
* 这种方式支持get和post,参数可选
* 访问方式简单的比如http://localhost:8080/springmvc-param/request/get0?p1=xxx。
*/
@RequestMapping(value="get0")
public void get0(String par,ModelMap map){
map.addAttribute("par", par);//网页面传递
}
/**
* 这种方式支持get,参数必须
* 这种方式和第一种不同的是,指定了访问访问必须为GET,而且参数是必须的,可以通过如下方式访问这个地址:
* http://localhost:8080/springmvc-param/request/get1?p1=xxxx。
* value="p1"表示参数名称<br>
* required=true表示如果没有传递参数"p1",则会报400参数异常<br>
* 使用void表示约定的路径,即request/get1.jsp
* @param p1
* @param map
*/
@RequestMapping(value="get1",method=RequestMethod.GET)
public void get1(@RequestParam(value="p1",required=true)String p1,ModelMap map){
map.addAttribute("p1", p1);//往页面传递
}
/**
* 这种方式仅支持GET,参数可选,这个方法和第二种唯一不同的就是参数是可选的,其他没有不同。
* 和get1不同的是,p1这个参数不一定非得需要,即使不给这个参数,也可以正常运行<br>
* 返回String是视图的名称,只要将map赋值,给的值也会带到前台页面
* @param p1
* @param map
* @return
*/
@RequestMapping(value="get2",method=RequestMethod.GET)
public String get2(@RequestParam(value="p1",required=false)String p1,ModelMap map){
map.addAttribute("p1", p1);//往页面传递
return "request/get2";
}
/**
* 这种方式仅支持GET,参数可选
* ModelAndView表示绑定了数据的视图,可以通过EL表达式去取值。
* 和get2不同的是,返回的对象是ModelAndView
* 表示绑定了视图和数据的对象,数据就是ModelMap中的Key-Value
* @param p1
* @param map
* @return
*/
@RequestMapping(value="get3",method=RequestMethod.GET)
public ModelAndView get3(@RequestParam(value="p1",required=false)String p1,ModelMap map){
map.addAttribute("p1", p1);
return new ModelAndView("request/get2",map);
}
/**
* 跳转到页面
* @throws NoSuchAlgorithmException
*/
@RequestMapping("userForm")
public String userForm(HttpServletResponse response) throws NoSuchAlgorithmException{
CookieUtils.writeCookie(response, -1, "x", "dddddddddddddd");
return "request/userForm";
}
先访问http://localhost:8080/springmvc-param/request/userForm这个方法,
跳转到一个页面,并向浏览器写入Cookie,第二个方法访问的时候即可通过@CookieValue方式来取到Cookie中的值
/**
* 读取Cookie的值
* @param x
* @return
*/
@ResponseBody
@RequestMapping("cookie")
public String cookie(@CookieValue("x")String x){
return x;
}
/**
* 绑定数据到User对象,支持Map,Set,List,Array等,但是需要使用下标,不是很灵活
* 请查看user2的写法
* @param user
* @param map
*/
绑定数据到一个对象上,支持get和post
一个User,一个Phone,一个User拥有多个Phone,为了演示,User中有一个List和Array的Phone的集合
public class User {
private String userName;
private String address;
private List<Phone> phones;
private Phone[] phones2;
//省略GET和SET...
}
public class Phone {
private String brand;//手机品牌
}
//承接上面
@RequestMapping(value="user")
public void user(User user,ModelMap map){
map.addAttribute("user", user);
}
/**
* 这里可以接受List,Array,Set等,写法是一样的,注意前端写法<br>
* 另外这个必须要使用MappingJacksonHttpMessageConverter这个消息转换器
* 请看我上面的配置
* @param user
* @return
* ,仅支持post,但是必须要在消息转换器中配置JSON解析器
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
并注册到RequestMappingHandlerAdapter的messageConverters中。
*/
@ResponseBody
@RequestMapping("user2")
public String user2(@RequestBody List<User> user){
System.out.println(user.size());
return "";
}
var userList= new Array();
userList.push({userName:"xx",address:"fff"});
userList.push({userName:"zzzz",address:"ggggg"});
$.ajax({
url:"request/user2",
type:"post",
data:JSON.stringify(userList),
dataType:"json",
contentType:"application/json",
success:function(data){
},error:function(data){
}
});
该方法仅支持POST的方式,会使用到json2.js这个类库,注意设置contentType:"application/json"这个属性,否则会报415未知的类型异常。
/**
* 这个方法只支持POST
* @param s
* @return
*/
var array=new Array();
array.push(1);
array.push(2);
array.push(3);
array.push(4);
array.push(5);
$.ajax({
url:"request/array",
type:"post",
dataType:"json",
data:JSON.stringify(array),
dataType:"json",
contentType:"application/json",
success:function(data){
},error:function(data){
}
});
和上面的方法类似,注意contentType:"application/json",否则同样的415错误
@ResponseBody
@RequestMapping("array")
public String array(@RequestBody String[] s){
System.out.println(s.length);
return "";
}
/**
* 这个比较奇葩,来自一位朋友的写法,即.xxx/5,4这样的请求,SpringMVC竟然也是支持的
* @param id
* @return
*/
@ResponseBody
@RequestMapping(value="array/{id}",method=RequestMethod.GET)
public String array2(@PathVariable("id")Long[] id){
System.out.println(id.length);
return "array length:"+id.length+"";
}
/**
* 一个表单对应多个Bean对象,这些Bean中有相同的属性,那么需要在分装他们的一个整体的对象
* 使之支持object.property的表达式
* @param c
*/
@ResponseBody
@RequestMapping("complex")
public void complexObject(C c){
System.out.println(c.getA().getX());
System.out.println(c.getB().getX());
}
/**
* 读取Cookie的值
* @param x
* @return
*/
@ResponseBody
@RequestMapping("cookie")
public String cookie(@CookieValue("x")String x){
return x;
}
}