$(function(){ $("#linksCommonGrid").datagrid({ url:appPath+'/page/pageIndexMrgAct/queryPageIndexLinks', striped: true, remoteSort:false, pagination:true, rownumbers:true, singleSelect:false, height : 600, queryParams:getQueryParam1(), idField:'id', frozenColumns:[[{ field : 'ck', title:'全选', checkbox : true }]], columns:[[ {field:'opt',title:'操作',width:55,align:'center', formatter:function(value,rowData,rowIndex){ return "<img class='op-enable' src='"+appPath+"/js/lib/jquery-easyui/themes/icons/pencil.png' onClick='modifyLinksComm(\""+rowData.id+"\")' title='编辑'/>" + " <img class='op-enable' src='"+appPath+"/js/lib/jquery-easyui/themes/icons/cancel.png' onClick='cancelLinksComm("+rowData.id+")' title='删除'/> " ; } }, {field:'url',title:'URL',width:200,align:'center',sortable:true}, {field:'keywords1',title:'关键词',width:100,align:'center',sortable:true }, {field:'linkUrl1',title:'对方链接',width:200,align:'center',sortable:true }, {field:'qq',title:'对方QQ',width:100,align:'center',sortable:true }, {field:'phone',title:'对方手机',width:100,align:'center',sortable:true }, {field:'keywords2',title:'我方关键词',width:100,align:'center',sortable:true }, {field:'linkUrl2',title:'我方URL',width:200,align:'center',sortable:true }, {field:'addTime',title:'添加时间',width:100,align:'center',sortable:true } ]] }); }); function getQueryParam1() { var queryObject = new Object(); queryObject.linkType =Number($.trim($("#typeParam").val())); return queryObject; } function doQuery(){ $("#linksCommonGrid").datagrid('unselectAll'); $("#linksCommonGrid").datagrid('load',getQueryParam()); } function getQueryParam() { var queryObject = new Object(); queryObject.url = $.trim($("#queryUrl").val()); queryObject.keywords1 = $.trim($("#queryKeywords1").val()); queryObject.linkUrl1 = $.trim($("#queryLinkUrl1").val()); queryObject.qq = $.trim($("#queryqq").val()); queryObject.keywords2 = $.trim($("#queryKeywords2").val()); queryObject.linkUrl2 = $.trim($("#queryLinkUrl2").val()); queryObject.linkType =Number($.trim($("#typeParam").val())); return queryObject; } //新增友情链接 function addLinks (){ var iconStr = "icon-add"; var title = "添加友情链接"; initData(); $('#addDlg').window({ title : title, iconCls : iconStr, width : 580, height : 300, left : 200, modal : true, shadow : true, collapsible : false, minimizable : false, maximizable : false }); $('#addDlg').window('move', { top : 100 }); $('#addDlg').window('open'); } function closeWin(id) { if(id='addDlg'){ if($("#addUrl").val()=='' && $("#addKeywords1").val()=='' && $("#addLinkUrl1").val()==''){ $('#'+id).window('close'); }else{ $.messager.defaults = { ok: "是", cancel: "否,继续填写" }; $.messager.confirm('提示信息','将丢失已经填写的内容,是否继续关闭?',function(r){ if(r){ $('#'+id).window('close'); } }); } }else{ $('#'+id).window('close'); } } function initData(){ $('#addUrl').css("border","1px solid #A4BED4"); $('#addKeywords1').css("border","1px solid #A4BED4"); $('#addLinkUrl1').css("border","1px solid #A4BED4"); $("#addId").val(""); $("#addUrl").val(""); $("#addKeywords1").val(""); $("#addKeywords2").val(""); $("#addLinkUrl1").val(""); $("#addLinkUrl2").val(""); $("#addqq").val(""); $("#addPhone").val(""); } function save(){ if ($("#addUrl").val() == null || $("#addUrl").val() == '' || $("#addKeywords1").val() == null || $("#addKeywords1").val() == '' || $("#addLinkUrl1").val() == null || $("#addLinkUrl1").val() == '') { $.messager.alert('提示信息', '信息没有填写完整,请补充!', 'info'); return; } if($.trim($("#addUrl").val()).substr(0,7)!='http://'){ $.messager.alert('提示信息', 'URL格式错误,请以http://开头!', 'info'); $("#addUrl").css('border-color','red'); return; } var k1 = getByteLen($("#addKeywords1").val()) if(k1>40){ $.messager.alert('提示信息', '关键词最多20个中文字符', 'info'); $("#addKeywords1").css('border-color','red'); return; } if($.trim($("#addLinkUrl1").val()).substr(0,7)!='http://'){ $.messager.alert('提示信息', '对方链接格式错误,请以http://开头!', 'info'); $("#addLinkUrl1").css('border-color','red'); return; } if($("#addKeywords2").val() !=null && $("#addKeywords2").val() != ''){ var k2 = getByteLen($("#addKeywords2").val()); if(k2>40){ $.messager.alert('提示信息', '我方关键词最多20个中文字符', 'info'); return; } } if($("#addLinkUrl2").val() !=null && $("#addLinkUrl2").val() != ''){ if($.trim($("#addLinkUrl2").val()).substr(0,7)!='http://'){ $.messager.alert('提示信息', '我方URL格式错误,请以http://开头!', 'info'); $("#addUrl").css('border-color','red'); return; } } var modifyid =0; var pageIndexLinks = new Object(); if($("#addId").val()!=null && $("#addId").val()!=''){ modifyid=$("#addId").val(); pageIndexLinks.id=$("#addId").val(); } pageIndexLinks.linkType=Number($("#typeParam").val()); pageIndexLinks.url= $.trim($('#addUrl').val()); pageIndexLinks.keywords1=$.trim($("#addKeywords1").val()); pageIndexLinks.linkUrl1=$.trim($("#addLinkUrl1").val()); pageIndexLinks.qq=$.trim($("#addqq").val()); pageIndexLinks.phone=$.trim($("#addPhone").val()); pageIndexLinks.keywords2=$.trim($("#addKeywords2").val()); pageIndexLinks.linkUrl2=$.trim($("#addLinkUrl2").val()); doAjax({ url : appPath + '/page/pageIndexMrgAct/opreatePageIndexLinks', type : 'post', data : pageIndexLinks, success : function(data) { if (data.code == 0) { $.messager.alert('提示信息', '保存成功', 'success'); $('#addDlg').window('close'); doQuery(); }else if (data.code == -2){ var returnid=data.msg; if (returnid*1 != modifyid*1 ){ $('#addUrl').css("border","1px solid red"); $('#addKeywords1').css("border","1px solid red"); $('#addLinkUrl1').css("border","1px solid red"); $.messager.alert('提示信息', "该数据已经存在" + ',操作失败', 'error'); } }else if (data.code == -1){ $('#addUrl').css("border","1px solid red"); $('#addKeywords1').css("border","1px solid red"); $('#addLinkUrl1').css("border","1px solid red"); $.messager.alert('提示信息', "该数据已经存在" + ',操作失败', 'error'); }else { $.messager.alert('提示信息', data.msg + ',操作失败', 'error'); } }, error : function(XMLHttpRequest, textStatus, errorThrown) { $.messager.alert('提示信息', '操作未能完成' + textStatus, 'error'); } }); } //编辑 function modifyLinksComm (id){ initData(); var iconStr = "icon-edit"; var title = "编辑友情链接"; if (id != null) { doAjax({ url : appPath + '/page/pageIndexMrgAct/getPageIndexLinksId', type : 'post', data : { id : id }, dataType : 'json', success : function(data) { $("#addId").val(data.data.id); $("#addKeywords1").val(data.data.keywords1); $("#addLinkUrl1").val(data.data.linkUrl1); $("#addqq").val(data.data.qq); $("#addPhone").val(data.data.phone); $("#addKeywords2").val( data.data.keywords2); $("#addLinkUrl2").val(data.data.linkUrl2); $("#addUrl").val(data.data.url); }, error : function(XMLHttpRequest, textStatus, errorThrown) { $.messager.alert('提示信息', '操作未能完成' + textStatus, 'error'); } }); } $('#addDlg').window({ title : title, iconCls : iconStr, width : 580, height : 300, left : 200, modal : true, shadow : true, collapsible : false, minimizable : false, maximizable : false }); $('#addDlg').window('move', { top : 100 }); $('#addDlg').window('open'); } //单个删除 function cancelLinksComm (id){ var pageIndexLinks = new Object(); pageIndexLinks.id=id; pageIndexLinks.isDelete='Y'; $.messager.defaults = { ok: "确定", cancel: "取消" }; $.messager.confirm('提示信息','您确定要删除?',function(r){ if(r){ doAjax({ url : appPath + '/page/pageIndexMrgAct/deleteLinks', type : 'post', data : pageIndexLinks, success : function(data) { if (data.code == 0) { $.messager.alert('提示信息', '删除成功', 'success'); doQuery(); } else { $.messager.alert('提示信息', data.msg + ',操作失败', 'error'); } }, error : function(XMLHttpRequest, textStatus, errorThrown) { $.messager.alert('提示信息', '操作未能完成' + textStatus, 'error'); } }); } }); } //批量删除 function batchDeleteGoods(){ $.messager.defaults = { ok: "确定", cancel: "取消" }; var selections = $("#linksCommonGrid").datagrid('getSelections'); if (selections == null|| selections == '' || selections.length==0) { $.messager.alert('提示信息','请选择要删除的数据', 'info'); return; } $.messager.confirm('提示信息','您确定要删除这些记录?', function(r) { if (r) { var idStr = ''; for ( var i = 0; i < selections.length; i++) { if( i == (selections.length - 1)){ idStr += selections[i].id; }else{ idStr += selections[i].id + ","; } } doAjax({ url:appPath+'/page/pageIndexMrgAct/batchDeleteLinks', type:'post', data:{idStr:idStr}, success:function(data){ if(data='ok'){ $.messager.alert('提示信息','删除成功!','info'); } doQuery(); }, error:function(XMLHttpRequest, textStatus, errorThrown){ $.messager.alert('提示信息','删除失败!','info'); doQuery(); } }); } }); } //导出 function exportExcel(){ $.messager.confirm('提示信息','您确定要导出到Excel?',function(r){ if(r){ var columns = $("#linksCommonGrid").datagrid("options").columns[0]; //--------把标题grid标题和grid的field,拼接成字符串----------- var stringTitle = ""; var stringFields = ""; for(var i = 1; i < columns.length; i++){ stringTitle = stringTitle + columns[i].title + ","; stringFields = stringFields + columns[i].field + ","; } if(stringTitle.lastIndexOf(",") == (stringTitle.length-1)){ stringTitle = stringTitle.substring(0, stringTitle.lastIndexOf(",")); } if(stringFields.lastIndexOf(",") == (stringFields.length-1)){ stringFields = stringFields.substring(0, stringFields.lastIndexOf(",")); } var queryParams = $("#linksCommonGrid").datagrid("options").queryParams; queryParams.gridTitle = stringTitle; queryParams.gridField = stringFields; queryParams.moduleName = "友情链接.xls"; var formObj = $("<form></form>").attr("method","post").attr("action",appPath+"/page/pageIndexMrgAct/exportLinksList"); formObj.append("<input type='text' name='gridTitle'>") .append("<input type='text' name='gridField'>") .append("<input type='text' name='moduleName'>") .append("<input type='text' name='linkType'>") .append("<input type='text' name='url'>") .append("<input type='text' name='keywords1'>") .append("<input type='text' name='linkUrl1'>") .append("<input type='text' name='qq'>") .append("<input type='text' name='keywords2'>") .append("<input type='text' name='linkUrl2'>") .css('display','none') .appendTo("body"); formObj.form("load",queryParams); formObj.submit(); formObj.remove(); } }); } //批量导入新增 function batchaddLinks(){ var linkType=$.trim($("#typeParam").val()); var args1 = "height=350," + "width=650," + "toolbar =no," + "top =150," + "left =250," + "menubar=no," + "scrollbars=no," + "resizable=no," + "location=no," + "status=no"; window.open(appPath+"/jsp/homepage/links_import.jsp?linkType="+linkType,"",args1); } //返回val的字节长度 function getByteLen(val) { var len = 0; for (var i = 0; i < val.length; i++) { if (val[i].match(/[^\x00-\xff]/ig) != null) //全角 len += 2; else len += 1; } return len; }
=================================================================
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <c:set var="ctx" value="${pageContext.request.contextPath}" /> <html> <head> <jsp:include page="/jsp/common/header.jsp"></jsp:include> <title>友情链接</title> <script type="text/javascript" src="${ctx }/js/homepage/home_page_linkFriendly.js"></script> </head> <body> <input type="hidden" id="typeParam" value="<%=request.getParameter("typeParam")%>"> <table class="queryTable" > <tr> <td class="queryTitle" width="80px">URL:</td> <td class="queryContent"> <input class="inputText" id="queryUrl" /> </td> <td class="queryTitle" width="80px">关键词:</td> <td class="queryContent"> <input class="inputText" id="queryKeywords1" /> </td> <td class="queryTitle" width="80px">对方链接:</td> <td class="queryContent"> <input class="inputText" id="queryLinkUrl1" /> </td> <td class="queryTitle" width="80px">对方QQ:</td> <td class="queryContent"> <input class="inputText" id="queryqq" /> </td> </tr> <tr> <td class="queryTitle" width="80px">我方关键词:</td> <td class="queryContent"> <input class="inputText" id="queryKeywords2"/> </td> <td class="queryTitle" width="80px">我方URL:</td> <td class="queryContent"> <input class="inputText" id="queryLinkUrl2" /> </td> <td class="queryBtnTd" colspan="4"> <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-search" onclick="doQuery()">检 索</a> </td> </tr> </table> <div region="south" border="false" style="text-align:left;height:35px;line-height:30px;"> <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" onclick="addLinks()">新增友情链接</a> <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" onclick="batchaddLinks()">批量新增友情链接</a> <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="batchDeleteGoods()">批量删除</a> <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-exportExcel" onclick="exportExcel()">导出Excel</a> </div> <table id="linksCommonGrid" ></table> <div id="addDlg" class="easyui-window" title="" closed="true" iconCls="icon-save" style="width: 582px; height: 500px; text-align: center; background: #fafafa;"> <div class="easyui-layout" fit="true"> <div region="center" border="false" style="background: #fff; border: 1px solid #ccc; padding-left: 30px;"> <form id="addForm" method="POST" style="width: 580px; height: 210px; text-align: center;"> <table border="0" class="queryTable" width="450px" height="210px" style="margin-bottom: 0px"> <tr> <td class="queryTitle">*URL: <input id="addId" type="hidden" /> </td> <td class="queryContent"><input type="text" id="addUrl" name="addUrl" class="inputText easyui-validatebox" required="true" /><font color="red">(必须以http://开头)</font></td> </tr> <tr> <td class="queryTitle">*关键词:</td> <td class="queryContent"><input type="text" id="addKeywords1" name="addKeywords1" class="inputText easyui-validatebox" required="true" /><font color="red">(最多20个中文字符)</font></td> </tr> <tr> <td class="queryTitle">*对方链接:</td> <td class="queryContent"><input type="text" id="addLinkUrl1" name="addLinkUrl1" class="inputText easyui-validatebox" required="true" /> <font color="red">(必须以http://开头)</font></td> </tr> <tr> <td class="queryTitle">对方QQ:</td> <td class="queryContent"><input type="text" id="addqq" name="addqq" class="inputText easyui-validatebox" required="true" /></td> </tr> <tr> <td class="queryTitle" width="70px">对方手机:</td> <td class="queryContent"><input type="text" id="addPhone" class="inputText easyui-validatebox" required="true" /></td> </tr> <tr> <td class="queryTitle" width="70px">我方关键词:</td> <td class="queryContent"><input type="text" id="addKeywords2" class="inputText easyui-validatebox" required="true" /></td> </tr> <tr> <td class="queryTitle" width="70px">我方URL:</td> <td class="queryContent"><input type="text" id="addLinkUrl2" class="inputText easyui-validatebox" required="true" /> <font color="red">(必须以http://开头)</font></td> </tr> </table> </form> </div> <div region="south" border="false" style="text-align: center; height: 30px; line-height: 30px;"> <a class="easyui-linkbutton" iconCls="icon-ok" href="javascript:void(0)" onclick="save()">保存</a> <a class="easyui-linkbutton" iconCls="icon-cancel" href="javascript:void(0)" onclick="closeWin('addDlg')">关闭</a> </div> </div> </div> </body> </html>
=================================================
/** * 批量导入 * * @param request * @param response * @param uploadFile * @throws Exception */ @RequestMapping(value = "/linksImport", method = RequestMethod.POST) public void linksImport(HttpServletRequest request, HttpServletResponse response, @RequestParam("uploadFile") MultipartFile f) throws Exception { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); Integer linkType=Integer.parseInt(request.getParameter("linkType")); HashMap<String, Object> map = new HashMap<String, Object>(); try { InputStream is = f.getInputStream(); Workbook wb = Workbook.getWorkbook(is); Sheet sheet = wb.getSheet(0); map = this.doImportLinks(sheet,linkType); response.getWriter().write(org.json.simple.JSONObject.toJSONString(map)); } catch (Exception e) { response.getWriter().write("{code:-3,msg:'inner error'}"); logger.error(e.getMessage(), e); } } ============================================================================= private HashMap<String, Object> doImportLinks(Sheet sheet,Integer linkType) { HashMap<String, Object> rslt = new HashMap<String, Object>(); List<PageIndexLinks> pageIndexLinksList=new ArrayList<PageIndexLinks>(); if(linkType==null){ rslt.put("code", -1); rslt.put("msg", "业务类型传输有错"); return rslt; } int countColumn = sheet.getColumns(); int countRow = sheet.getRows(); if (countColumn !=7) { rslt.put("code", -2);// Excel表格应该等于7列 } else if (countRow <= 1) { rslt.put("code", -2);// Excel表格应该大于1行 } else { Cell cell = null; for (int i = 1; i < countRow; i++) { PageIndexLinks pageIndexLinks=new PageIndexLinks(); pageIndexLinks.setLinkType(linkType); pageIndexLinks.setIsDelete("N"); for (int j = 0; j < countColumn; j++) { cell = sheet.getCell(j, i); String content = (null == cell.getContents()) ? "" : cell .getContents().trim(); if(j==0||j==1|j==2){ if(StringUtils.isEmpty(content)){ rslt.put("code", -1); rslt.put("msg", "提供的Excel数据存在空值"); return rslt; } } if(j==0 || j==2 ||( j==6&& content !=null&&content.length()>0)){ if(!content.substring(0, 7).equals("http://")){ rslt.put("code", -1); rslt.put("msg", "提供的Excel数据存在不合规格的url"); return rslt; } } if(j==0 ){ pageIndexLinks.setUrl(content); }else if (j==1){ if (content.length()>40 ) { rslt.put("code", -1); rslt.put("msg", "提供的Excel数据为:"+content+",超出长度,最多20个中文字符"); return rslt; }else { pageIndexLinks.setKeywords1(content); } }else if(j==2){ pageIndexLinks.setLinkUrl1(content); }else if (j==3 && content !=null &&content.length()>0 ){ pageIndexLinks.setQq(content); }else if (j==4 && content !=null &&content.length()>0 ){ pageIndexLinks.setPhone(content); }else if (j==5 && content !=null&&content.length()>0 ){ if (content.length()>40 ) { rslt.put("code", -1); rslt.put("msg", "提供的Excel数据为:"+content+",超出长度,最多20个中文字符"); return rslt; }else { pageIndexLinks.setKeywords2(content); } }else if(j==6 && content !=null && content.length()>0){ pageIndexLinks.setLinkUrl2(content); } } if(pageIndexLinks!=null){ pageIndexLinksList.add(pageIndexLinks); } } JSONArray json1=JSONArray.fromObject(pageIndexLinksList); rslt.put("code", 0); rslt.put("pageIndexLinksList", json1.toString()); return rslt; } return rslt; }
=====================================