1SpringMVC快速的完成ajax功能?
1)返回数据是json就OK;
2)页面,$.ajax()
2原生ajaxWeb
1)导入GSON
2)返回的数据用GSON转成json
3)写出去
3SpringMVC-ajax:
1)导包
jackson-annotations-2.1.5.jar
jackson-core-2.1.5.jar
jackson-databind-2.1.5.jar
2)写配置
public class AjaxTestController {
@Autowired
EmployeeDao employeeDao;
/**
* @ResponseBody,响应体,将返回的 数据放在响应体中,如果是对象,自动转为json格式
* jackson包下的
* @return
*/
@ResponseBody
@RequestMapping("/getallajax")
public Collection<Employee> ajaxGetAll(){
Collection<Employee> all = employeeDao.getAll();
return all;
}
}
JsonFormat:设置json数据的格式
@JsonFormat(pattern="yyyy-MM-dd")
@Past
private Date birth = new Date();
JsonIgnore:设置json可以忽略的属性
@JsonIgnore
private Department department;
<a href="${ctp }/getallajax">ajax获取所有的员工</a><br/>
<div>
</div>
<script type="text/javascript">
$("a:first").click(function(){
//1发送ajax获取所有的员工
$.ajax({
url:"${ctp}/getallajax",
type:"GET",
success:function(data){
//console.log(data);
$.each(data,function(){
var empInfo=this.lastName+"-->"+this.birth+
"-->"+this.gender;
$("div").append(empInfo+"<br/>");
});
}
});
return false;
});
</script>
3)测试
@RequestBody&@ResponseBody
<!-- post才有请求体,get是把数据待在请求的uil地址上 -->
<!-- enctype="multipart/form-data" 文件上传 -->
<form action="${ctp}/test02" method="post"
enctype="multipart/form-data">
<input name="userName" value="tomcat" /> <input name="password"
value="123456" /> <input type="file" name="file" /> <input
type="submit">
</form>
/**
* 如果请求参数位置写HttpEntity<String>,还能拿到请求头
*
* @param str
* @return
*/
@RequestMapping("/test02")
public String test02(HttpEntity<String> str){
System.out.println("HttpEntity:"+str);
return "success";
}
<a href="${ctp}/testRequestBody">ajax发送json格式</a>
<script type="text/javascript">
$("a:first").click(function() {
//点击发送ajax请求,请求带的数据是json
var emp = {
lastName : "张三",
email : "123@qq.com",
gender : 0
};
//js对象
//alert(typeof emp);
var empStr = JSON.stringify(emp);
//alert(typeof empStr);
$.ajax({
url : '${ctp}/testRequestBody',
type : "POST",
data : empStr,
contentType : "application/json",
success : function(data) {
alert(data);
}
});
return false;
});
</script>
/**
* @RequestBody:请求体
* @RequestParam:
*
* @ResponseBody:可以把对象转为json格式返回给浏览器
* @ResquestBody:接受json格式,封装为对象
*
* 获取一个请求的请求体
* @return
*/
@RequestMapping("/testRequestBody")
public String testRequestBody(@RequestBody String body){
System.out.println("请求体:"+body);
return "success";
}
文件下载
/**
* SpringMVC文件下载
* 将返回的数据放在响应体中,和视图解析器毫无关系,return的内容直接返回
* ResponseEntity<String>响应体中内容的类型
* @return
* @throws IOException
*/
@ResponseBody
@RequestMapping("/download")
public ResponseEntity<byte[]> download(HttpServletRequest request) throws IOException{
// String body="<h1>success</h1>";
// HttpStatus statusCode;
// MultiValueMap<String, String> headers=new HttpHeaders();
// headers.add("Set-Cookie", "username=hahaa");
//
// return new ResponseEntity<>(body, headers, HttpStatus.OK);
//1.得到要下载的文件的流
//找到下载文件的真实路径
ServletContext servletContext = request.getServletContext();
String realPath = servletContext.getRealPath("/scripts/jquery-1.9.1.min.js");
FileInputStream is = new FileInputStream(realPath);
byte[] tmp = new byte[is.available()];
is.read(tmp);
is.close();
MultiValueMap<String, String> headers;
//2,将要下载的文件流返回
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.set("Content-Disposition", "attachment;filename="+"jquery-1.9.1.min.js");
return new ResponseEntity<byte[]>(tmp, httpHeaders, HttpStatus.OK);
}