IP通用配置导出,只导出配置列表上展示的列,界面上的列表需要指定exportColumn,与后台注解ExcelField 的title一致

This commit is contained in:
wangxin
2018-07-13 19:58:40 +08:00
parent 3309725c9a
commit 59bf2c3dbe
11 changed files with 267 additions and 83 deletions

View File

@@ -146,7 +146,7 @@ public class BaseCfg<T> extends BaseEntity<T> implements Cloneable{
/**
* 分类
*/
@ExcelField(title="type",dictType="type")
@ExcelField(title="classification",dictType="type")
protected String classify;
/**
* 性质

View File

@@ -297,6 +297,113 @@ public class ExportExcel {
}
initialize(title, headerList);
}
/**
* 构造函数
* @param msgProp 国际化配置
* @param title 表格标题,传“空值”,表示无标题
* @param cls 实体对象通过annotation.ExportField获取标题
* @param type 导出类型1:导出数据2导出模板
* @param groups 导入分组
*/
public ExportExcel(String columns,Properties msgProp,String title, Class<?> cls, int type, int... groups){
String[] cloumnArray=columns.split(",");
List<Field> list=new ArrayList<Field>();
// Get annotation field
//递归获取cls实体对象及父级对象的属性
getFields(list, cls);
if(!StringUtil.isEmpty(list)){
for (Field f : list){
ExcelField ef = f.getAnnotation(ExcelField.class);
boolean export=false;
for(String column:cloumnArray) {
if(ef!=null&&column.equals(ef.title())) {
export=true;
break;
}
}
if(export) {
if (ef != null && (ef.type()==0 || ef.type()==type)){
if (groups!=null && groups.length>0){
boolean inGroup = false;
for (int g : groups){
if (inGroup){
break;
}
for (int efg : ef.groups()){
if (g == efg){
inGroup = true;
annotationList.add(new Object[]{ef, f});
break;
}
}
}
}else{
annotationList.add(new Object[]{ef, f});
}
}
}
}
}
List<Method> ms=new ArrayList<Method>();
// Get annotation method
//递归获取cls实体对象及父级对象的属性
getMethods(ms, cls);
if(!StringUtil.isEmpty(ms)){
for (Method m : ms){
ExcelField ef = m.getAnnotation(ExcelField.class);
boolean export=false;
for(String column:cloumnArray) {
if(ef!=null&&column.equals(ef.title())) {
export=true;
break;
}
}
if(export) {
if (ef != null && (ef.type()==0 || ef.type()==type)){
if (groups!=null && groups.length>0){
boolean inGroup = false;
for (int g : groups){
if (inGroup){
break;
}
for (int efg : ef.groups()){
if (g == efg){
inGroup = true;
annotationList.add(new Object[]{ef, m});
break;
}
}
}
}else{
annotationList.add(new Object[]{ef, m});
}
}
}
}
}
// Field sorting
Collections.sort(annotationList, new Comparator<Object[]>() {
public int compare(Object[] o1, Object[] o2) {
return new Integer(((ExcelField)o1[0]).sort()).compareTo(
new Integer(((ExcelField)o2[0]).sort()));
};
});
// Initialize
List<String> headerList = Lists.newArrayList();
for (Object[] os : annotationList){
String t = ((ExcelField)os[0]).title();
// 如果是导出,则去掉注释
if (type==1){
String[] ss = StringUtils.split(t, "**", 2);
if (ss.length==2){
t = ss[0];
}
}
headerList.add(msgProp.getProperty(t)==null?t:msgProp.getProperty(t));
}
initialize(title, headerList);
}
/**
* 构造函数
* @param title 表格标题,传“空值”,表示无标题
@@ -497,48 +604,116 @@ public class ExportExcel {
* @return list 数据列表
*/
public <E> ExportExcel setDataList(Properties msgProp,List<E> list,Map map){
for (E e : list){
int colunm = 0;
Row row = this.addRow();
StringBuilder sb = new StringBuilder();
for (E e : list){
int colunm = 0;
Row row = this.addRow();
StringBuilder sb = new StringBuilder();
for (Object[] os : annotationList){
ExcelField ef = (ExcelField)os[0];
Object val = null;
// Get entity value
try{
if (StringUtils.isNotBlank(ef.value())){
val = Reflections.invokeGetter(e, ef.value());
}else{
if (os[1] instanceof Field){
val = Reflections.invokeGetter(e, ((Field)os[1]).getName());
}else if (os[1] instanceof Method){
val = Reflections.invokeMethod(e, ((Method)os[1]).getName(), new Class[] {}, new Object[] {});
}
}
// If is dict, get dict label
if (StringUtils.isNotBlank(ef.dictType())){
String valStr=val==null?"":val.toString();
if("type".equals(ef.dictType()) || "attribute".equals(ef.dictType())
|| "label".equals(ef.dictType())){
// Get basic info
val = getBasicInfo(ef.dictType(),map,valStr);
}else{
//字典数据已做国际化处理
String dict=DictUtils.getDictLabel(ef.dictType(), valStr, "");
//如果找不到字典国际化值,把字典本身作为默认值放进去,不然导出就是空了
val = msgProp.getProperty(dict,dict);
}
}
}catch(Exception ex) {
// Failure to ignore
log.error("Get entity value failed",ex);
val = "";
}
this.addCell(row, colunm++, val, ef.align(), ef.fieldType());
sb.append(val + ", ");
}
log.debug("Write success: ["+row.getRowNum()+"] "+sb.toString());
}
return this;
}
/**
* 添加数据通过annotation.ExportField添加数据
* @return list 数据列表
*/
public <E> ExportExcel setDataList(String columns,Properties msgProp,List<E> list,Map map){
if(StringUtils.isBlank(columns)) {
return setDataList(msgProp,list,map);
}else {
Map<String,Boolean> exportMap=new HashMap<>();
String[] columnArray=columns.split(",");
for (Object[] os : annotationList){
ExcelField ef = (ExcelField)os[0];
Object val = null;
// Get entity value
try{
if (StringUtils.isNotBlank(ef.value())){
val = Reflections.invokeGetter(e, ef.value());
}else{
if (os[1] instanceof Field){
val = Reflections.invokeGetter(e, ((Field)os[1]).getName());
}else if (os[1] instanceof Method){
val = Reflections.invokeMethod(e, ((Method)os[1]).getName(), new Class[] {}, new Object[] {});
}
for(String column:columnArray) {
if(column.equals(ef.title())) {
exportMap.put(column, true);
break;
}
// If is dict, get dict label
if (StringUtils.isNotBlank(ef.dictType())){
String valStr=val==null?"":val.toString();
if("type".equals(ef.dictType()) || "attribute".equals(ef.dictType())
|| "label".equals(ef.dictType())){
// Get basic info
val = getBasicInfo(ef.dictType(),map,valStr);
}else{
//字典数据已做国际化处理
String dict=DictUtils.getDictLabel(ef.dictType(), valStr, "");
//如果找不到字典国际化值,把字典本身作为默认值放进去,不然导出就是空了
val = msgProp.getProperty(dict,dict);
}
}
}catch(Exception ex) {
// Failure to ignore
log.error("Get entity value failed",ex);
val = "";
}
this.addCell(row, colunm++, val, ef.align(), ef.fieldType());
sb.append(val + ", ");
}
log.debug("Write success: ["+row.getRowNum()+"] "+sb.toString());
for (E e : list){
int colunm = 0;
Row row = this.addRow();
StringBuilder sb = new StringBuilder();
for (Object[] os : annotationList){
ExcelField ef = (ExcelField)os[0];
if(exportMap.containsKey(ef.title())) {
Object val = null;
// Get entity value
try{
if (StringUtils.isNotBlank(ef.value())){
val = Reflections.invokeGetter(e, ef.value());
}else{
if (os[1] instanceof Field){
val = Reflections.invokeGetter(e, ((Field)os[1]).getName());
}else if (os[1] instanceof Method){
val = Reflections.invokeMethod(e, ((Method)os[1]).getName(), new Class[] {}, new Object[] {});
}
}
// If is dict, get dict label
if (StringUtils.isNotBlank(ef.dictType())){
String valStr=val==null?"":val.toString();
if("type".equals(ef.dictType()) || "attribute".equals(ef.dictType())
|| "label".equals(ef.dictType())){
// Get basic info
val = getBasicInfo(ef.dictType(),map,valStr);
}else{
//字典数据已做国际化处理
String dict=DictUtils.getDictLabel(ef.dictType(), valStr, "");
//如果找不到字典国际化值,把字典本身作为默认值放进去,不然导出就是空了
val = msgProp.getProperty(dict,dict);
}
}
}catch(Exception ex) {
// Failure to ignore
log.error("Get entity value failed",ex);
val = "";
}
this.addCell(row, colunm++, val, ef.align(), ef.fieldType());
sb.append(val + ", ");
}
}
log.debug("Write success: ["+row.getRowNum()+"] "+sb.toString());
}
}
return this;
}

View File

@@ -245,7 +245,7 @@ public class CommonController extends BaseController {
this.importCfgTemplate(request, response, redirectAttributes, functionId, cfgRegionCode);
}
//ip配置导出
public void _exportIp(Model model,HttpServletRequest request,HttpServletResponse response,
public void _exportIp(String columns,Model model,HttpServletRequest request,HttpServletResponse response,
IpPortCfg entity,String ids,RedirectAttributes redirectAttributes){
try {
//获取国际化配置
@@ -267,7 +267,7 @@ public class CommonController extends BaseController {
Long.parseLong(id);
}
List<BaseIpCfg> list=ipCfgService.getListByCfgIdWithName(IpPortCfg.getTablename(), entity.getFunctionId(), ids);
new ExportExcel(msgProp,null, IpPortCfg.class,1).setDataList(msgProp,list,map).write(response, fileName).dispose();
new ExportExcel(columns,msgProp,null, IpPortCfg.class,1).setDataList(columns,msgProp,list,map).write(response, fileName).dispose();
}else{
//条件导出数据大于最大导出数,只导出最大导出条数
entity.setTableName(IpPortCfg.getTablename());
@@ -280,7 +280,7 @@ public class CommonController extends BaseController {
pageInfo.setPageSize(-1);
}
Page<BaseIpCfg> page = ipCfgService.findPage(pageInfo, entity);
new ExportExcel(msgProp,null, IpPortCfg.class,1).setDataList(msgProp,page.getList(),map).write(response, fileName).dispose();
new ExportExcel(columns,msgProp,null, IpPortCfg.class,1).setDataList(columns,msgProp,page.getList(),map).write(response, fileName).dispose();
}
} catch (Exception e) {
e.printStackTrace();

View File

@@ -106,8 +106,8 @@ public class IpMultiplexController extends CommonController {
}
//ip配置导出
@RequestMapping(value = "export")
public void exportIp(Model model,HttpServletRequest request,HttpServletResponse response,
public void exportIp(String columns,Model model,HttpServletRequest request,HttpServletResponse response,
@ModelAttribute("cfg")IpPortCfg entity,String ids,RedirectAttributes redirectAttributes){
this._exportIp(model, request, response, entity, ids, redirectAttributes);
this._exportIp(columns,model, request, response, entity, ids, redirectAttributes);
}
}

View File

@@ -126,8 +126,8 @@ public class RatelimitController extends CommonController {
}
//ip配置导出
@RequestMapping(value = "/ip/export")
public void exportIp(Model model,HttpServletRequest request,HttpServletResponse response,
public void exportIp(String columns,Model model,HttpServletRequest request,HttpServletResponse response,
@ModelAttribute("cfg")IpPortCfg entity,String ids,RedirectAttributes redirectAttributes){
this._exportIp(model, request, response, entity, ids, redirectAttributes);
this._exportIp(columns,model, request, response, entity, ids, redirectAttributes);
}
}

View File

@@ -79,8 +79,8 @@ public class IpController extends CommonController{
}
//ip配置导出
@RequestMapping(value = "export")
public void exportIp(Model model,HttpServletRequest request,HttpServletResponse response,
public void exportIp(String columns,Model model,HttpServletRequest request,HttpServletResponse response,
@ModelAttribute("cfg")IpPortCfg entity,String ids,RedirectAttributes redirectAttributes){
this._exportIp(model, request, response, entity, ids, redirectAttributes);
this._exportIp(columns,model, request, response, entity, ids, redirectAttributes);
}
}

View File

@@ -233,8 +233,8 @@ public class WhiteListController extends CommonController{
}
//ip配置导出
@RequestMapping(value = "ip/export")
public void exportIp(Model model,HttpServletRequest request,HttpServletResponse response,
public void exportIp(String columns,Model model,HttpServletRequest request,HttpServletResponse response,
@ModelAttribute("cfg")IpPortCfg entity,String ids,RedirectAttributes redirectAttributes){
this._exportIp(model, request, response, entity, ids, redirectAttributes);
this._exportIp(columns,model, request, response, entity, ids, redirectAttributes);
}
}

View File

@@ -126,8 +126,8 @@ public class ControlController extends CommonController {
}
//ip配置导出
@RequestMapping(value = "/ip/export")
public void exportIp(Model model,HttpServletRequest request,HttpServletResponse response,
public void exportIp(String columns,Model model,HttpServletRequest request,HttpServletResponse response,
@ModelAttribute("cfg")IpPortCfg entity,String ids,RedirectAttributes redirectAttributes){
this._exportIp(model, request, response, entity, ids, redirectAttributes);
this._exportIp(columns,model, request, response, entity, ids, redirectAttributes);
}
}

View File

@@ -116,8 +116,8 @@ public class InterceptController extends CommonController{
}
//ip配置导出
@RequestMapping(value = "/ip/export")
public void exportIp(Model model,HttpServletRequest request,HttpServletResponse response,
public void exportIp(String columns,Model model,HttpServletRequest request,HttpServletResponse response,
@ModelAttribute("cfg")IpPortCfg entity,String ids,RedirectAttributes redirectAttributes){
this._exportIp(model, request, response, entity, ids, redirectAttributes);
this._exportIp(columns,model, request, response, entity, ids, redirectAttributes);
}
}

View File

@@ -214,6 +214,15 @@ var checkboxes=$("#${id} ${value} tbody tr td input.i-checks:checkbox");
}
//删除
function exportData(url,maxRow){
var column=[];
$("#${id} ${value} thead tr th").each(function(){
if($(this).attr("exportColumn")){
column.push($(this).attr("exportColumn"));
}
});
if(column){
url+="&columns="+column.toString();
}
var checkboxes=$("#${id} ${value} tbody tr td input.i-checks:checkbox");
//导出选中数据
if($(checkboxes).filter(":checked").length>0){

View File

@@ -277,46 +277,46 @@
<tr>
<th><input type="checkbox" class="i-checks" id="checkAll"></th>
<%-- <th><spring:message code="seq"/></th> --%>
<th><spring:message code="config_describe"/></th>
<th>IP<spring:message code="type"/></th>
<th exportColumn="config_describe" ><spring:message code="config_describe"/></th>
<th exportColumn="ip_type"><spring:message code="ip_type"/></th>
<c:if test="${specialFunctionId ne 'ipmulitiplex'}">
<th><spring:message code="client_ip"/></th>
<th><spring:message code="client_port"/></th>
<th><spring:message code="server_ip"/></th>
<th><spring:message code="server_port"/></th>
<th exportColumn="client_ip"><spring:message code="client_ip"/></th>
<th exportColumn="client_port"><spring:message code="client_port"/></th>
<th exportColumn="server_ip"><spring:message code="server_ip"/></th>
<th exportColumn="server_port"><spring:message code="server_port"/></th>
</c:if>
<c:if test="${specialFunctionId eq 'ipmulitiplex'}">
<th><spring:message code="IP"/></th>
<th><spring:message code="port"/></th>
<th exportColumn="client_ip"><spring:message code="IP"/></th>
<th exportColumn="client_port"><spring:message code="port"/></th>
</c:if>
<th><spring:message code="block_type"/></th>
<th><spring:message code="direction"/></th>
<th><spring:message code="protocol"/></th>
<th><spring:message code="whether_area_block"/></th>
<th exportColumn="block_type"><spring:message code="block_type"/></th>
<th exportColumn="direction"><spring:message code="direction"/></th>
<th exportColumn="protocol"><spring:message code="protocol"/></th>
<th exportColumn="whether_area_block"><spring:message code="whether_area_block"/></th>
<c:if test="${specialFunctionId ne null and specialFunctionId eq 'ipmulitiplex'}">
<th><spring:message code="group_name"/></th>
<th><spring:message code="ir_type"/></th>
<th exportColumn="group_name"><spring:message code="group_name"/></th>
<th exportColumn="ir_type"><spring:message code="ir_type"/></th>
</c:if>
<c:if test="${specialFunctionId ne null and specialFunctionId eq 'ipratelimit'}">
<th><spring:message code="ratelimit"/></th>
<th exportColumn="ratelimit"><spring:message code="ratelimit"/></th>
</c:if>
<c:if test="${specialFunctionId ne null and specialFunctionId eq 'pxyIpControl'}">
<th><spring:message code="REDIRECT_RESPONSE_CODE"/></th>
<th><spring:message code="redirect_content"/></th>
<th exportColumn="userRegion1"><spring:message code="REDIRECT_RESPONSE_CODE"/></th>
<th exportColumn="userRegion2"><spring:message code="redirect_content"/></th>
</c:if>
<th><spring:message code="letter"/></th>
<th><spring:message code="classification"/></th>
<th><spring:message code="attribute"/></th>
<th><spring:message code="label"/></th>
<th><spring:message code="valid_identifier"/></th>
<th><spring:message code="is_audit"/></th>
<th><spring:message code="log_total"/></th>
<th><spring:message code="creator"/></th>
<th class="sort-column r.create_time"><spring:message code="config_time"/></th>
<th><spring:message code="editor"/></th>
<th class="sort-column r.edit_time"><spring:message code="edit_time"/></th>
<th><spring:message code="auditor"/></th>
<th class="sort-column r.audit_time"><spring:message code="audit_time"/></th>
<th exportColumn="letter"><spring:message code="letter"/></th>
<th exportColumn="classification"><spring:message code="classification"/></th>
<th exportColumn="attribute"><spring:message code="attribute"/></th>
<th exportColumn="label"><spring:message code="label"/></th>
<th exportColumn="valid_identifier"><spring:message code="valid_identifier"/></th>
<th exportColumn="is_audit"><spring:message code="is_audit"/></th>
<th exportColumn="log_total"><spring:message code="log_total"/></th>
<th exportColumn="creator"><spring:message code="creator"/></th>
<th exportColumn="config_time" class="sort-column r.create_time"><spring:message code="config_time"/></th>
<th exportColumn="editor"><spring:message code="editor"/></th>
<th exportColumn="edit_time" class="sort-column r.edit_time"><spring:message code="edit_time"/></th>
<th exportColumn="auditor"><spring:message code="auditor"/></th>
<th exportColumn="audit_time" class="sort-column r.audit_time"><spring:message code="audit_time"/></th>
<%-- <th><spring:message code="operation"></spring:message></th> --%>
</tr>
</thead>