1、jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(function(){
$("#queryByName").click(function(){
var name=$(this).prev().val();
location.href="${pageContext.request.contextPath}/employee/queryByName?name="+name;
});
$("#import").click(function(){
var array=$("input[type=checkbox]:checked");
if(array.length==0){
alert("请选择您要导出的数据...");
}else{
var ids=new Array();
array.each(function(){
ids.push($(this).parent().next().text());
});
$.post("${pageContext.request.contextPath}/employee/queryByIds","ids="+ids,function(d){
if(d=="ok"){
alert("数据导入成功!");
}else{
alert("数据导入失败!");
}
location.reload();
},"json");
}
});
});
</script>
</head>
<body>
<div>
姓名:<input type="text" name="name"/><input type="button" value="查询" id="queryByName"/>
<input type="button" value="导出" id="import"/>
</div>
<table border="1" width="80%">
<tr>
<th>选择</th>
<th>序号</th>
<th>姓名</th>
<th>职位</th>
<th>薪资</th>
<th>部门</th>
</tr>
<c:forEach items="${requestScope.list }" var="e">
<tr>
<td><input type="checkbox"></td>
<td>${e.id }</td>
<td>${e.name }</td>
<td>${e.position }</td>
<td>${e.salary }</td>
<td>${e.department.name }</td>
</tr>
</c:forEach>
</table>
</body>
</html>
2、controllerl类
@RequestMapping("queryByIds")
@ResponseBody
public String queryByName(Model model,String[] ids) throws Exception{
FileOutputStream outputStream =null;
OutputStreamWriter writer=null;
BufferedWriter writer2=null;
try {
int[] idss=new int[ids.length];
for(int i=0;i<ids.length;i++){
idss[i]=Integer.parseInt(ids[i]);
}
List<Employee> list = es.queryByIds(idss);
outputStream = new FileOutputStream(new File("D://employees.txt"));
writer=new OutputStreamWriter(outputStream, "utf-8");
writer2 = new BufferedWriter(writer);
for (Employee employee : list) {
writer2.write(employee.getId()+"#"+employee.getName()+"#"+employee.getPosition()+"#"+employee.getSalary()+"#"+employee.getDepartment().getName());
writer2.newLine();
}
return "ok";
} catch (Exception e) {
return "error";
}finally{
writer2.close();
writer.close();
outputStream.close();
}
}
}