一.分区添加功能
1.写在combox中
<input class="easyui-combobox" name="region.id"
data-options="valueField:'id',textField:'name',mode:'remote',
url:'regionAction_listajax.action'" />
2.编写Action
/**
* 查询所有未关联到定区的分区,返回json
*/
public String listajax(){
List<Subarea> list = subareaService.findListNotAssociation();
this.java2Json(list, new String[]{"decidedzone","region"});
return NONE;
}
3.编写Service接口和实现
import java.util.List;
import com.itheima.bos.domain.Subarea;
import com.itheima.bos.utils.PageBean;
public interface ISubareaService {
public void save(Subarea model);
public void pageQuery(PageBean pageBean);
public List<Subarea> findAll();
public List<Subarea> findListNotAssociation();
}
import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.itheima.bos.dao.ISubareaDao;
import com.itheima.bos.domain.Subarea;
import com.itheima.bos.service.ISubareaService;
import com.itheima.bos.utils.PageBean;
@Service
@Transactional
public class SubareaServiceImpl implements ISubareaService {
@Autowired
private ISubareaDao subareaDao;
public void save(Subarea model) {
subareaDao.save(model);
}
public void pageQuery(PageBean pageBean) {
subareaDao.pageQuery(pageBean);
}
public List<Subarea> findAll() {
return subareaDao.findAll();
}
/**
* 查询所有未关联到定区的分区
*/
public List<Subarea> findListNotAssociation() {
DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Subarea.class);
//添加过滤条件,分区对象中decidedzone属性为null
detachedCriteria.add(Restrictions.isNull("decidedzone"));
return subareaDao.findByCriteria(detachedCriteria);
}
}
4.编写dao接口和实现
import com.itheima.bos.dao.base.IBaseDao;
import com.itheima.bos.domain.Subarea;
public interface ISubareaDao extends IBaseDao<Subarea>{
}
import org.springframework.stereotype.Repository;
import com.itheima.bos.dao.ISubareaDao;
import com.itheima.bos.dao.base.impl.BaseDaoImpl;
import com.itheima.bos.domain.Subarea;
@Repository
public class SubareaDaoImpl extends BaseDaoImpl<Subarea> implements ISubareaDao {
}
二.数据导出功能
1.按钮对应的函数
//导出按钮对应的处理函数
function doExport(){
//发送请求,请求Action,进行文件下载
window.location.href = "subareaAction_exportXls.action";
}
2.编写Action
/**
* 分区数据导出功能
* @throws IOException
*/
public String exportXls() throws IOException{
//第一步:查询所有的分区数据
List<Subarea> list = subareaService.findAll();
//第二步:使用POI将数据写到Excel文件中
//在内存中创建一个Excel文件
HSSFWorkbook workbook = new HSSFWorkbook();
//创建一个标签页
HSSFSheet sheet = workbook.createSheet("分区数据");
//创建标题行
HSSFRow headRow = sheet.createRow(0);
headRow.createCell(0).setCellValue("分区编号");
headRow.createCell(1).setCellValue("开始编号");
headRow.createCell(2).setCellValue("结束编号");
headRow.createCell(3).setCellValue("位置信息");
headRow.createCell(4).setCellValue("省市区");
for (Subarea subarea : list) {
HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
dataRow.createCell(0).setCellValue(subarea.getId());
dataRow.createCell(1).setCellValue(subarea.getStartnum());
dataRow.createCell(2).setCellValue(subarea.getEndnum());
dataRow.createCell(3).setCellValue(subarea.getPosition());
dataRow.createCell(4).setCellValue(subarea.getRegion().getName());
}
//第三步:使用输出流进行文件下载(一个流、两个头)
String filename = "分区数据.xls";
String contentType = ServletActionContext.getServletContext().getMimeType(filename);
ServletOutputStream out = ServletActionContext.getResponse().getOutputStream();
ServletActionContext.getResponse().setContentType(contentType);
//获取客户端浏览器类型
String agent = ServletActionContext.getRequest().getHeader("User-Agent");
filename = FileUtils.encodeDownloadFilename(filename, agent);
ServletActionContext.getResponse().setHeader("content-disposition", "attachment;filename="+filename);
workbook.write(out);
return NONE;
}
所用到的工具类
import java.io.IOException;
import java.net.URLEncoder;
import sun.misc.BASE64Encoder;
public class FileUtils {
/**
* 下载文件时,针对不同浏览器,进行附件名的编码
*
* @param filename
* 下载文件名
* @param agent
* 客户端浏览器
* @return 编码后的下载附件名
* @throws IOException
*/
public static String encodeDownloadFilename(String filename, String agent)
throws IOException {
if (agent.contains("Firefox")) { // 火狐浏览器
filename = "=?UTF-8?B?"
+ new BASE64Encoder().encode(filename.getBytes("utf-8"))
+ "?=";
filename = filename.replaceAll("\r\n", "");
} else { // IE及其他浏览器
filename = URLEncoder.encode(filename, "utf-8");
filename = filename.replace("+"," ");
}
return filename;
}
}
三.分区条件组合查询功能
1.用在数据表格中
// 收派标准数据表格
$('#grid').datagrid( {
iconCls : 'icon-forward',
fit : true,
border : true,
rownumbers : true,
striped : true,
pageList: [1],
pagination : true,
toolbar : toolbar,
url : "subareaAction_pageQuery.action",
idField : 'id',
columns : columns,
onDblClickRow : doDblClickRow
});
2.编写Action
/**
* 分页查询
*/
public String pageQuery(){
DetachedCriteria dc = pageBean.getDetachedCriteria();
//动态添加过滤条件
String addresskey = model.getAddresskey();
if(StringUtils.isNotBlank(addresskey)){
//添加过滤条件,根据地址关键字模糊查询
dc.add(Restrictions.like("addresskey", "%"+addresskey+"%"));
}
Region region = model.getRegion();
if(region != null){
String province = region.getProvince();
String city = region.getCity();
String district = region.getDistrict();
dc.createAlias("region", "r");
if(StringUtils.isNotBlank(province)){
//添加过滤条件,根据省份模糊查询-----多表关联查询,使用别名方式实现
//参数一:分区对象中关联的区域对象属性名称
//参数二:别名,可以任意
dc.add(Restrictions.like("r.province", "%"+province+"%"));
}
if(StringUtils.isNotBlank(city)){
//添加过滤条件,根据市模糊查询-----多表关联查询,使用别名方式实现
//参数一:分区对象中关联的区域对象属性名称
//参数二:别名,可以任意
dc.add(Restrictions.like("r.city", "%"+city+"%"));
}
if(StringUtils.isNotBlank(district)){
//添加过滤条件,根据区模糊查询-----多表关联查询,使用别名方式实现
//参数一:分区对象中关联的区域对象属性名称
//参数二:别名,可以任意
dc.add(Restrictions.like("r.district", "%"+district+"%"));
}
}
subareaService.pageQuery(pageBean);
this.java2Json(pageBean, new String[]{"currentPage","detachedCriteria","pageSize",
"decidedzone","subareas"});
return NONE;
}
这样没有条件时就会全部分页显示,有条件再筛选
3.编写Service接口和实现
import java.util.List;
import com.itheima.bos.domain.Subarea;
import com.itheima.bos.utils.PageBean;
public interface ISubareaService {
public void save(Subarea model);
public void pageQuery(PageBean pageBean);
public List<Subarea> findAll();
public List<Subarea> findListNotAssociation();
}
import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.itheima.bos.dao.ISubareaDao;
import com.itheima.bos.domain.Subarea;
import com.itheima.bos.service.ISubareaService;
import com.itheima.bos.utils.PageBean;
@Service
@Transactional
public class SubareaServiceImpl implements ISubareaService {
@Autowired
private ISubareaDao subareaDao;
public void save(Subarea model) {
subareaDao.save(model);
}
public void pageQuery(PageBean pageBean) {
subareaDao.pageQuery(pageBean);
}
public List<Subarea> findAll() {
return subareaDao.findAll();
}
/**
* 查询所有未关联到定区的分区
*/
public List<Subarea> findListNotAssociation() {
DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Subarea.class);
//添加过滤条件,分区对象中decidedzone属性为null
detachedCriteria.add(Restrictions.isNull("decidedzone"));
return subareaDao.findByCriteria(detachedCriteria);
}
}