bootstap后台管理增删改

    xiaoxiao2021-03-25  127

    说明:后台管理的增删改很常用,采用bootstrap方便快捷,本文主要用了ajax和bootstrap结合来处理数据交互

    1.前端jsp:

    <!DOCTYPE html> <jsp:directive.page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" /> <%@ taglib prefix="ur" uri="ur-tags"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%> <!-- document type --> <c:import url="/inc/doctype-frag.jsp" /> <fmt:setBundle basename="messages" /> <html> <head> <title>爬虫网址配置管理</title> <!-- Medatadata fragment for page cache --> <c:import url="/inc/meta-frag.jsp" /> <c:import url="/inc/css-frag.jsp" /> <ur:styleSheet href="page-resources/css/main_menu.css" /> <ur:styleSheet href="page-resources/css/global.css" /> <ur:styleSheet href="page-resources/css/tables.css" /> <ur:styleSheet href="page-resources/css/blue_theme.css" /> <ur:styleSheet href="page-resources/bootstrap-table/bootstrap-table.css" /> <ur:styleSheet href="page-resources/bootstrap-table/bootstrap-editable.css" /> <c:import url="/inc/js-frag.jsp" /> <!-- Source File --> <ur:js src="page-resources/js/menu/main_menu.js" /> <ur:js src="pages/js/base_path.js" /> <ur:js src="page-resources/bootstrap-table/bootstrap-table.js" /> <ur:js src="page-resources/bootstrap-table/bootstrap-table-export.js" /> <ur:js src="page-resources/extends/tableExport/jquery.base64.js" /> <ur:js src="page-resources/extends/tableExport/tableExport.js" /> <ur:js src="page-resources/bootstrap-table/bootstrap-table-field-search.js" /> <ur:js src="page-resources/bootstrap-table/bootstrap-editable.js" /> <ur:js src="page-resources/bootstrap-table/bootstrap-table-editable.js" /> <ur:js src="page-resources/js/crawlerconfigure/crawlerconfigure.js" /> <ur:js src="page-resources/js/util/automatic-bootstrap-table.js"/> </head> <body> <c:import url="/inc/header.jsp" /> <div class="container"> <!-- <div id="workspace-tabs" class="yui-navset"> <ul class="nav nav-tabs"> <li class="active"><a href="#manageCrawlerConfigure" data-toggle="tab"><em>模板管理</em></a></li> <li><a href="#manageMetadata" data-toggle="tab"><em>元数据管理</em></a></li> <li class="active"><a href="#manageOrganization" data-toggle="tab"><em>机构管理</em></a></li> </ul> </div> --> <!-- 加载初始化数据 --> <div class="tab-content"> <div id="manageCrawlerConfigure" class="tab-pane fade in active" style="padding-bottom: 50px;"> <div class="text-right" style="padding-right:20px;margin-top: 10px;"> <a href="#" class="btn btn-system-function-tem"οnclick="crawlerConfigureDef()"><img src="${pageContext.request.contextPath}/page-resources/images/all-images/mobanguanli01.png"/>新增</a> </div> <table id="crawlerConfigureTable" data-toggle="table" data-show-columns="false" data-search="false" data-show-refresh="false" data-show-toggle="false" data-card-view="false" data-pagination="true" data-page-size="5" data-url="viewCrawlerConfigure.action"> </table> </div> </div> </div> <!-- 弹出新建或者修改的div对话框 star --> <div class="modal fade" id="crawlerConfigure_div"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" οnclick="cancel('crawlerConfigure');"> <span aria-hidden="true">×</span><span class="sr-only">Close</span> </button> <h4 class="modal-title">爬虫网址配置信息</h4> </div> <div class="modal-body form-horizontal"> <input type="hidden" id="configureId" /> <div class="form-group"> <label for="configureUrl" class="col-xs-3 col-sm-3 col-md-3 col-lg-3 control-label"><font color="red">*</font>网址:</label> <div class="col-sm-8 col-md-8 col-lg-8"> <input class="form-control input-sm" type="text" id="configureUrl" /> </div> </div> <div class="form-group"> <label for="configureCharset" class="col-xs-3 col-sm-3 col-md-3 col-lg-3 control-label"><font color="red">*</font>编码方式:</label> <div class="col-sm-8 col-md-8 col-lg-8"> <input class="form-control input-sm" type="text" id="configureCharset" /> </div> </div> <div class="form-group"> <label for="configureMc" class="col-xs-3 col-sm-3 col-md-3 col-lg-3 control-label"><font color="red">*</font>网址名称:</label> <div class="col-sm-8 col-md-8 col-lg-8"> <input class="form-control input-sm" type="text" id="configureMc" /> </div> </div> <div class="form-group"> <label for="configureNeedKeyword" class="col-xs-3 col-sm-3 col-md-3 col-lg-3 control-label"><font color="red">*</font>是否需要关键词:</label> <div class="col-sm-8 col-md-8 col-lg-8" id="configureNeedKeyword"> <input name="configureNeedKeyword" type="radio" checked="checked" name="1" value="需要"/>需要 <input name="configureNeedKeyword" type="radio" name="1" value="不需要"/>不需要 </div> </div> <div class="form-group"> <label for="configureMatchReg" class="col-xs-3 col-sm-3 col-md-3 col-lg-3 control-label"><font color="red">*</font>爬取网址规则:</label> <div class="col-sm-8 col-md-8 col-lg-8"> <input class="form-control input-sm" type="text" id="configureMatchReg" /> </div> </div> <div class="form-group"> <label for="configureUnmatchReg" class="col-xs-3 col-sm-3 col-md-3 col-lg-3 control-label"><font color="red">*</font>不匹配爬取网址规则:</label> <div class="col-sm-8 col-md-8 col-lg-8"> <input class="form-control input-sm" type="text" id="configureUnmatchReg" /> </div> </div> <!-- 其他参数,形式是para1=value1,para2=value2 --> <div class="form-group"> <label for="configureParaKeyword" class="col-xs-3 col-sm-3 col-md-3 col-lg-3 control-label"><font color="red">*</font>关键词参数(逗号隔开):</label> <div class="col-sm-8 col-md-8 col-lg-8"> <input class="form-control input-sm" type="text" id="configureParaKeyword" /> </div> </div> <div class="form-group"> <label for="c" class="col-xs-3 col-sm-3 col-md-3 col-lg-3 control-label"><font color="red">*</font>其他参数:</label> <div class="col-sm-8 col-md-8 col-lg-8"> <input class="form-control input-sm" type="text" id="configureParaOther" /> </div> </div> <div class="form-group"> <label for="collectionId" class="col-xs-3 col-sm-3 col-md-3 col-lg-3 control-label"><font color="red">*</font>专题:</label> <div class="col-sm-8 col-md-8 col-lg-8"> <select id="collectionId"><option>请选择</option></select> </div> </div> </div> <div class="modal-footer"> <button type="button" class="btn btn-primary" id="addOrUpdate" οnclick="saveCrawlerConfigure();">确定</button> <button type="button" class="btn btn-default" id="cancel" οnclick="cancel();">取消</button> </div> </div> </div> </div> <!-- 弹出新建或者修改的div对话框 end --> <div class="modal fade" id="loading" data-keyboard="false" data-backdrop="static" οncοntextmenu="return false" onselectstart="return false"> <div class="modal-dialog"> <c:import url="/inc/loading.jsp" /> </div> </div> </body> </html>

    2.js代码:

    //定义全局变量 var $crawlerConfigureTable; //最开始加载界面时,该js代码就会被执行 $(function() { //table的标题栏 var crawlerConfigureColumns = [ { field : "configureId", title : "编号", width : "1%", sortable : "true" }, { field : "configureUrl", title : "网址", align : "left", valign : "middle", sortable : "true", width : "2%" }, { field : "configureMc", title : "网址名", align : "center", valign : "middle", width : "5%" }, { field : "collectionName", title : "专题", align : "center", valign : "middle", width : "5%" }, { field : 'operate', title : '操作', align : 'center', width : "3%", formatter : operateFormatter } ]; // '<a href="#" class="btn btn-modelManageOne" style="background:#f8f8f8;color:black;" οnclick="editMetadatas(' + row.ID + ')"><span class="glyphicon glyphicon-edit" style="color: rgb(131, 183, 187);" ></span> 查看</a>', //最右边操作栏的格式化 function operateFormatter(value, row, index) { // bootsrtap会将每一行的数据值,放在row中 // console.log("value:"+value); // console.log("row:"+row); // console.log("index:"+index); return [ '<a href="#" class="btn btn-modelManageOne" style="background:#f8f8f8;color:black;" οnclick="editCrawlerConfigure(' + row.configureId + ')"><span class="glyphicon glyphicon-edit" style="color: rgb(131, 183, 187);" ></span> 编辑</a>', '<a href="#" class="btn btn-modelManageOne" style="background:#f8f8f8;color:black;" οnclick="deleteCrawlerConfigure(' + row.configureId + ')"><span class="glyphicon glyphicon-trash" style="color: rgb(131, 183, 187);" ></span> 删除</a>' ].join(''); } //页面加载时,bootstrap会首先执行这行代码 $crawlerConfigureTable=$('#crawlerConfigureTable').bootstrapTable('destroy').bootstrapTable({ columns : crawlerConfigureColumns, pageList : [10,30,50, 100, 200, 500, 1000 ], uniqueId: "configureId",//设置唯一值,可以用bootstrapTable('getRowByUniqueId', id);取出该行的数据 showFieldSearch:false, firstLoadPic: true }); }); //获取系统中所有的专题 function getAllCollection(){ //1.$.ajax带json数据的异步请求 var aj = $.ajax( { url:'getAllCollectionCrawlerConfigure.action',// 跳转到 action type:'post', dataType:'json', success:function(data) { console.log("专题的json数据:"+data.data); setSelectValue(data); }, error : function() { alert("异常!"); } }); } //为专题下拉选择框设置值 function setSelectValue(data){ var collection = $("#collectionId"); //删除结点 removeOption(); //转成json格式 var result = eval(data.data); //添加默认结点 collection.append("<option value='-1'>--请选择--</option>"); // 循环遍历 下拉框绑定 $(result).each(function (key) { var opt = $("<option></option>").text(result[key].collectionName).val(result[key].collectionId); collection.append(opt); }); } //清空下拉选择框 function removeOption() { $("#collectionId option").remove(); } //编辑爬虫网址配置 function editCrawlerConfigure(id){ var row=$crawlerConfigureTable.bootstrapTable('getRowByUniqueId', id); //填充数据 var configureId = row.configureId; //根据id查询出数据,然后后台存到值栈中,界面上用EL表达式取出来 //1.$.ajax带json数据的异步请求 var aj = $.ajax( { url:'getConfigureByIdCrawlerConfigure.action',// 跳转到 action data:{ configureId:configureId }, type:'post', dataType:'json', success:function(data) { console.log("editCrawlerConfigure:"+data); $("#configureId").val(data.configureId); $("#configureUrl").val(data.configureUrl); $("#configureCharset").val(data.configureCharset); $("#configureMc").val(data.configureMc); $("#configureMatchReg").val(data.configureMatchReg); $("#configureUnmatchReg").val(data.configureUnmatchReg); $("#configureParaKeyword").val(data.configureParaKeyword); $("#configureParaOther").val(data.configureParaOther); $("#collectionId").val(data.collectionId); }, error : function() { } }); crawlerConfigureDef(); } //弹出新建爬虫网址配置的div对话框 function crawlerConfigureDef(){ //初始化专题数据 getAllCollection(); $("#crawlerConfigure_div").modal("show"); } //保存修改或者新建的爬虫网址配置信息 function saveCrawlerConfigure(){ //确定按钮不可用用 jQuery("#addOrUpdate").attr('disabled',true); var configureNeedKeyword = null; //获取单选框中选中的值 var obj = document.getElementsByName("configureNeedKeyword"); for(var i=0; i<obj.length; i ++){ // alert(obj[i].value); if("需要"==obj[i].value){ configureNeedKeyword = 1; }else{//不需要 configureNeedKeyword = 0; } } //验证参数 var configureId = jQuery("#configureId").val(); var configureUrl = jQuery("#configureUrl").val(); var configureCharset = jQuery("#configureCharset").val(); var configureMc = jQuery("#configureMc").val(); var configureMatchReg = jQuery("#configureMatchReg").val(); var configureUnmatchReg = jQuery("#configureUnmatchReg").val(); var configureParaKeyword = jQuery("#configureParaKeyword").val(); var configureParaOther = jQuery("#configureParaOther").val(); var collectionId = jQuery("#collectionId").val(); var oper_type = ""; if(configureUrl==""){ sweetAlert("", "网址不能为空!", "warning"); document.getElementById("configureUrl").focus(); jQuery("#addOrUpdate").attr('disabled',false); return false; } if(configureCharset=="" || configureCharset.trim().length>10){ sweetAlert("", "编码方式不能为空", "warning"); document.getElementById("configureCharset").focus(); jQuery("#addOrUpdate").attr('disabled',false); return false; } if(collectionId=="" || collectionId==-1){ sweetAlert("", "请选择专题", "warning"); document.getElementById("collectionId").focus(); jQuery("#addOrUpdate").attr('disabled',false); return false; } //添加 if(configureId.trim()==""){ oper_type = "add"; }else{ //修改 oper_type = "update"; } jQuery.ajax({ url:"saveCrawlerConfigure.action", data:{ configureId:configureId, configureUrl:configureUrl, configureCharset:configureCharset, configureMc:configureMc, configureNeedKeyword:configureNeedKeyword, configureMatchReg:configureMatchReg, configureUnmatchReg:configureUnmatchReg, configureParaKeyword:configureParaKeyword, configureParaOther:configureParaOther, collectionId:collectionId, type:oper_type }, type:'POST', success:function(data){ if(data=="SUCCESS"){ sweetAlert("", "保存成功!", "success"); $("#addOrUpdate").attr('disabled',false); cancel(); $crawlerConfigureTable.bootstrapTable('refresh'); }else if(data=="accessDenied"){ //登陆超时 window.location.href = basePath+"home.action"; }else{ sweetAlert("", data, "error"); $("#addOrUpdate").attr('disabled',false); } }, error:function(){ sweetAlert("", "保存失败!", "error"); $("#addOrUpdate").attr('disabled',false); cancel(); } }); } //删除一行数据 function deleteCrawlerConfigure(id){ var row=$crawlerConfigureTable.bootstrapTable('getRowByUniqueId', id); var configureId = row.configureId; var configureMc = row.configureMc; //提示框,自定义的需要引入文件<script src="dist/sweetalert.min.js"></script> swal({ title : "", text : "删除【"+configureMc+"】?", type : "warning", showCancelButton : true, cancelButtonText : "取消", confirmButtonColor : "#DD6B55", confirmButtonText : "删除", closeOnConfirm : false }, function() { jQuery.ajax({ url:"deleteCrawlerConfigure.action", data:{configureId:configureId}, type:'POST', success:function(data){ if(data=="SUCCESS"){ sweetAlert("", "删除成功!", "success"); }else{ sweetAlert("", data, "error"); } $crawlerConfigureTable.bootstrapTable('refresh'); }, error:function(){ sweetAlert("", "删除失败!", "error"); } }); }); } //新建或者修改的取消按钮 function cancel(type){ $("#crawlerConfigure_div").modal("hide"); clear(type); } //清空数据值 function clear(type){ jQuery("#crawlerConfigureId").val(""); jQuery("#crawlerConfigureUrl").val(""); jQuery("#configureMc").val(""); jQuery("#collectionName").val(""); }

    3.后台action:

    /** * @类功能说明: * @作者: 一叶扁舟 (skiff) * @创建时间:2017年3月8日 上午10:53:38 * @版本:V1.0 */ public class CrawlerConfigureAction extends ActionSupport implements UserIdAware { private static final Logger log = Logger.getLogger(CrawlerConfigureAction.class); private UserService userService; private int pageSize = 5;//分页-每页数 private int curPage = 1;//当前页码 private Long userId;//当前操作用户ID private ICrawlerConfigureService crawlerConfigureService; private InstitutionalCollectionService InstitutionalCollectionService; /** * 方法功能说明: 进入爬虫网址配置管理 * @参数: @return * @参数: @throws Exception * @return String * @throws */ public String enter() throws Exception { // 验证用户是否管理员 IrUser user = userService.getUser(userId, false); if (!user.hasRole(IrRole.ADMIN_ROLE)) { return "accessDenied"; } return SUCCESS; } /** * 方法功能说明: * @参数: * @return void * @throws */ public void view() { /* * curPage 1 pageSize 5->startRow 0 * * curPage 1 pageSize 5->startRow 5 */ int startRow = (curPage - 1) * pageSize; Object rowCount; List<CrawlerConfigure> configures; //总记录数 rowCount = crawlerConfigureService.getCount(); //分页的查询数据 configures = crawlerConfigureService.getCrawlerConfigures(startRow, pageSize); HttpServletResponse response = ServletActionContext.getResponse(); response.setCharacterEncoding("utf-8"); String retStr = ""; if (configures != null && configures.size() > 0) { retStr = "{\"success\": true, \"totalRows\": " + rowCount + ", \"curPage\": " + curPage + ", \"data\": " + AjaxUtils.getStrFromCrawlerConfigureList(configures) + "}"; } else { retStr = "{\"success\": true, \"totalRows\": 0, \"curPage\": 1, \"data\": []}"; } AjaxUtils.ajaxReturn(retStr, response); } /** * 方法功能说明: 获取所有的专题(即信息题材),并转成json格式 * @参数: * @return void * @throws */ public void getAllCollection(){ List<InstitutionalCollection> institutionalCollections= InstitutionalCollectionService.getAll(); HttpServletResponse response = ServletActionContext.getResponse(); response.setCharacterEncoding("utf-8"); String retStr = ""; if (institutionalCollections != null && institutionalCollections.size() > 0) { retStr = "{\"success\": true, \"totalRows\": " + institutionalCollections.size() + ", \"curPage\": " + 1 + ", \"data\": " + AjaxUtils.getStrFrominstItutionalCollectionList(institutionalCollections) + "}"; } else { retStr = "{\"success\": true, \"totalRows\": 0, \"curPage\": 1, \"data\": []}"; } AjaxUtils.ajaxReturn(retStr, response); } /** * 方法功能说明: 新建或者修改时保存 * @参数: * @return void * @throws */ public void save(){ HttpServletRequest request = ServletActionContext.getRequest(); HttpServletResponse response = ServletActionContext.getResponse(); try{ //登陆超时 IrUser user = userService.getUser(userId, false); if (user==null||!user.hasRole(IrRole.ADMIN_ROLE)) { AjaxUtils.ajaxReturn("accessDenied", response); return; } String configureId = request.getParameter("configureId"); String configureUrl = request.getParameter("configureUrl"); String configureCharset = request.getParameter("configureCharset"); String configureMc = request.getParameter("configureMc"); String configureNeedKeyword = request.getParameter("configureNeedKeyword"); String configureMatchReg = request.getParameter("configureMatchReg"); String configureUnmatchReg = request.getParameter("configureUnmatchReg"); String configureParaKeyword = request.getParameter("configureParaKeyword"); String configureParaOther = request.getParameter("configureParaOther"); String collectionId = request.getParameter("collectionId"); String type = request.getParameter("type"); if(type==null||"".equals(type)){ AjaxUtils.ajaxReturn("保存失败!原因:【type为空】", response); log.debug("保存失败【type为空】"); return; } if(type.equals("update")&&(configureId==null||"".equals(type))){ AjaxUtils.ajaxReturn("保存失败!原因:【更新记录时ID为空】", response); log.debug("保存失败【更新记录时ID为空】"); return; } CrawlerConfigure crawlerConfigure = new CrawlerConfigure(); //新建 if("add".equals(type)){ Timestamp time = new java.sql.Timestamp((new java.util.Date()).getTime()); crawlerConfigure.setCreateTime(time); crawlerConfigure.setModifyTime(time); } //修改 if("update".equals(type)){ crawlerConfigure.setConfigureId(Long.parseLong(configureId)); Timestamp time = new java.sql.Timestamp((new java.util.Date()).getTime()); crawlerConfigure.setModifyTime(time); } crawlerConfigure.setConfigureCharset(configureCharset); crawlerConfigure.setConfigureMatchReg(configureMatchReg); crawlerConfigure.setConfigureMc(configureMc); crawlerConfigure.setConfigureNeedKeyword(Integer.parseInt(configureNeedKeyword)); crawlerConfigure.setConfigureParaOther(configureParaOther); crawlerConfigure.setConfigureParaKeyword(configureParaKeyword); crawlerConfigure.setConfigureUnmatchReg(configureUnmatchReg); crawlerConfigure.setConfigureUrl(configureUrl); InstitutionalCollection institutionalCollection = null; institutionalCollection = InstitutionalCollectionService.getCollection(Long.parseLong(collectionId), false); crawlerConfigure.setInstitutionalCollection(institutionalCollection); //保存到数据库中 crawlerConfigureService.makePersistent(crawlerConfigure); AjaxUtils.ajaxReturn("SUCCESS", response); }catch(DataAccessException e){ AjaxUtils.ajaxReturn("保存失败!原因:【系统抛出异常】", response); log.error("保存失败!未知错误:"+e.getMessage()); }catch(Exception e2){ AjaxUtils.ajaxReturn("保存失败!原因:【系统抛出异常】", response); log.error("保存失败!未知错误:"+e2.getMessage()); } } /** * 方法功能说明: 根据id获取CrawlerConfigure,用来回显数据 * @参数: * @return void * @throws */ public void getConfigureById(){ HttpServletRequest request = ServletActionContext.getRequest(); HttpServletResponse response = ServletActionContext.getResponse(); //登陆超时 IrUser user = userService.getUser(userId, false); if (user==null||!user.hasRole(IrRole.ADMIN_ROLE)) { AjaxUtils.ajaxReturn("accessDenied", response); return; } String configureId = request.getParameter("configureId"); CrawlerConfigure crawlerConfigure =crawlerConfigureService.getById(Long.parseLong(configureId), false); response.setCharacterEncoding("utf-8"); String retStr = ""; if (crawlerConfigure != null ) { retStr = "{"+ AjaxUtils.getStrFromCrawlerConfigure(crawlerConfigure) + "}"; } AjaxUtils.ajaxReturn(retStr, response); } /** * 方法功能说明: 根据id删除一个crawlerConfigure * @参数: * @return void * @throws */ public void delete(){ HttpServletRequest request = ServletActionContext.getRequest(); HttpServletResponse response = ServletActionContext.getResponse(); try{ String id = request.getParameter("configureId"); if(id==null||"".equals(id)){ AjaxUtils.ajaxReturn("操作失败!【参数ID不能为空】", response); return; } CrawlerConfigure crawlerConfigure = crawlerConfigureService.getById(Long.parseLong(id), false); crawlerConfigureService.makeTransient(crawlerConfigure); AjaxUtils.ajaxReturn("SUCCESS", response); }catch(Exception e){ log.error("系统抛出异常!"+e.getMessage()); AjaxUtils.ajaxReturn("操作失败!【系统抛出异常】", response); } } /** * @return the pageSize */ public int getPageSize() { return pageSize; } /** * @param pageSize the pageSize to set */ public void setPageSize(int pageSize) { this.pageSize = pageSize; } /** * @return the curPage */ public int getCurPage() { return curPage; } /** * @param curPage the curPage to set */ public void setCurPage(int curPage) { this.curPage = curPage; } /** * @return the crawlerConfigureService */ public ICrawlerConfigureService getCrawlerConfigureService() { return crawlerConfigureService; } /** * @param crawlerConfigureService the crawlerConfigureService to set */ public void setCrawlerConfigureService( ICrawlerConfigureService crawlerConfigureService) { this.crawlerConfigureService = crawlerConfigureService; } public Long getUserId() { return userId; } public void setUserId(Long userId) { this.userId = userId; } /** * @return the userService */ public UserService getUserService() { return userService; } /** * @param userService the userService to set */ public void setUserService(UserService userService) { this.userService = userService; } /** * @return the institutionalCollectionService */ public InstitutionalCollectionService getInstitutionalCollectionService() { return InstitutionalCollectionService; } /** * @param institutionalCollectionService the institutionalCollectionService to set */ public void setInstitutionalCollectionService( InstitutionalCollectionService institutionalCollectionService) { InstitutionalCollectionService = institutionalCollectionService; } }

    4.ajax封装的json数据格式:

    public class AjaxUtils { private static final Logger log = Logger.getLogger(AjaxUtils.class); /** * * @param msg * @param response */ public static void ajaxReturn(String msg, HttpServletResponse response) { try { response.setCharacterEncoding("utf-8"); PrintWriter writer = response.getWriter(); writer.print(msg); writer.flush(); writer.close(); } catch (Exception e) { log.debug("ajax return faild"); log.debug("exception message:" + e.getMessage()); e.printStackTrace(); } } /** * 方法功能说明: 组装机构信息的json格式数据 * @参数: @param list * @参数: @return * @return String * @author 一叶扁舟(skiff) * @throws */ public static String getStrFromOrganizationList(List<Organization> list) { StringBuffer jsonStr = new StringBuffer("["); for (Organization organization : list) { jsonStr.append("{"); //"ID":"1", jsonStr.append("\"ID\""); jsonStr.append(":"); jsonStr.append("\"" + organization.getOrganizationId() + "\","); jsonStr.append("\"NAME\""); jsonStr.append(":"); jsonStr.append("\"" + organization.getOrganizationName().trim() + "\","); jsonStr.append("\"URL\""); jsonStr.append(":"); jsonStr.append("\"" + organization.getOrganizationUrl().trim() + "\""); jsonStr.append("},"); } return jsonStr.substring(0, jsonStr.length() - 1) + "]"; } /** * 方法功能说明: 爬虫网站的配置展示的主界面 * @参数: @param configures * @参数: @return * @return String * @throws */ public static String getStrFromCrawlerConfigureList( List<CrawlerConfigure> configures) { StringBuffer jsonStr = new StringBuffer("["); for (CrawlerConfigure e : configures) { jsonStr.append("{"); jsonStr.append("\"configureId\""); jsonStr.append(":"); jsonStr.append("\"" + e.getConfigureId() + "\","); if (e.getConfigureUrl() == null) { jsonStr.append("\"configureUrl\""); jsonStr.append(":"); jsonStr.append("\"\","); } else { jsonStr.append("\"configureUrl\""); jsonStr.append(":"); jsonStr.append("\"" + e.getConfigureUrl() + "\","); } if (e.getConfigureMc() == null) { jsonStr.append("\"configureMc\""); jsonStr.append(":"); jsonStr.append("\"\","); } else { jsonStr.append("\"configureMc\""); jsonStr.append(":"); jsonStr.append("\"" + e.getConfigureMc() + "\","); } jsonStr.append("\"collectionName\""); jsonStr.append(":"); jsonStr.append("\"" + e.getInstitutionalCollection().getName() + "\""); jsonStr.append("},"); } return jsonStr.substring(0, jsonStr.length() - 1) + "]"; } /** * 方法功能说明: * @参数: @param institutionalCollections * @参数: @return * @return String * @throws */ public static String getStrFrominstItutionalCollectionList( List<InstitutionalCollection> institutionalCollections) { StringBuffer jsonStr = new StringBuffer("["); for (InstitutionalCollection e : institutionalCollections) { jsonStr.append("{"); //专题的id jsonStr.append("\"collectionId\""); jsonStr.append(":"); jsonStr.append("\"" + e.getId() + "\","); //专题的名 jsonStr.append("\"collectionName\""); jsonStr.append(":"); jsonStr.append("\"" + e.getName() + "\""); jsonStr.append("},"); } return jsonStr.substring(0, jsonStr.length() - 1) + "]"; } /** * 方法功能说明: 将单个的crawlerConfigure封装成json格式 * @参数: @param crawlerConfigure * @参数: @return * @return String * @throws */ public static String getStrFromCrawlerConfigure( CrawlerConfigure e) { StringBuffer jsonStr = new StringBuffer(""); jsonStr.append("\"configureId\""); jsonStr.append(":"); jsonStr.append("\"" + e.getConfigureId() + "\","); if (e.getConfigureUrl() == null) { jsonStr.append("\"configureUrl\""); jsonStr.append(":"); jsonStr.append("\"\","); } else { jsonStr.append("\"configureUrl\""); jsonStr.append(":"); jsonStr.append("\"" + e.getConfigureUrl() + "\","); } if (e.getConfigureMc() == null) { jsonStr.append("\"configureMc\""); jsonStr.append(":"); jsonStr.append("\"\","); } else { jsonStr.append("\"configureMc\""); jsonStr.append(":"); jsonStr.append("\"" + e.getConfigureMc() + "\","); } if (e.getConfigureCharset() == null) { jsonStr.append("\"configureCharset\""); jsonStr.append(":"); jsonStr.append("\"\","); } else { jsonStr.append("\"configureCharset\""); jsonStr.append(":"); jsonStr.append("\"" + e.getConfigureCharset() + "\","); } if (e.getConfigureMatchReg() == null) { jsonStr.append("\"configureMatchReg\""); jsonStr.append(":"); jsonStr.append("\"\","); } else { jsonStr.append("\"configureMatchReg\""); jsonStr.append(":"); jsonStr.append("\"" + e.getConfigureMatchReg() + "\","); } jsonStr.append("\"configureNeedKeyword\""); jsonStr.append(":"); jsonStr.append("\"" + e.getConfigureNeedKeyword() + "\","); if (e.getConfigureParaKeyword() == null) { jsonStr.append("\"configureParaKeyword\""); jsonStr.append(":"); jsonStr.append("\"\","); } else { jsonStr.append("\"configureParaKeyword\""); jsonStr.append(":"); jsonStr.append("\"" + e.getConfigureParaKeyword() + "\","); } if (e.getConfigureUnmatchReg() == null) { jsonStr.append("\"configureUnmatchReg\""); jsonStr.append(":"); jsonStr.append("\"\","); } else { jsonStr.append("\"configureUnmatchReg\""); jsonStr.append(":"); jsonStr.append("\"" + e.getConfigureUnmatchReg() + "\","); } if (e.getConfigureParaOther() == null) { jsonStr.append("\"configureParaOther\""); jsonStr.append(":"); jsonStr.append("\"\","); } else { jsonStr.append("\"configureParaOther\""); jsonStr.append(":"); jsonStr.append("\"" + e.getConfigureParaOther() + "\","); } jsonStr.append("\"collectionId\""); jsonStr.append(":"); jsonStr.append("\"" + e.getInstitutionalCollection().getId() + "\""); return jsonStr.toString(); } /** * 方法功能说明: 封装成json格式,提供外部接口使用 * @参数: @param crawlerConfigure * @return void * @throws */ public static String getStrFromCrawlerConfigureForWebservice( CrawlerConfigure e) { StringBuffer jsonStr = new StringBuffer(""); jsonStr.append("\"configureId\""); jsonStr.append(":"); jsonStr.append("\"" + e.getConfigureId() + "\","); if (e.getConfigureUrl() == null) { jsonStr.append("\"configureUrl\""); jsonStr.append(":"); jsonStr.append("\"\","); } else { jsonStr.append("\"configureUrl\""); jsonStr.append(":"); jsonStr.append("\"" + e.getConfigureUrl() + "\","); } if (e.getConfigureMc() == null) { jsonStr.append("\"configureMc\""); jsonStr.append(":"); jsonStr.append("\"\","); } else { jsonStr.append("\"configureMc\""); jsonStr.append(":"); jsonStr.append("\"" + e.getConfigureMc() + "\","); } if (e.getConfigureCharset() == null) { jsonStr.append("\"configureCharset\""); jsonStr.append(":"); jsonStr.append("\"\","); } else { jsonStr.append("\"configureCharset\""); jsonStr.append(":"); jsonStr.append("\"" + e.getConfigureCharset() + "\","); } if (e.getConfigureMatchReg() == null) { jsonStr.append("\"configureMatchReg\""); jsonStr.append(":"); jsonStr.append("\"\","); } else { jsonStr.append("\"configureMatchReg\""); jsonStr.append(":"); jsonStr.append("\"" + e.getConfigureMatchReg() + "\","); } jsonStr.append("\"configureNeedKeyword\""); jsonStr.append(":"); jsonStr.append("\"" + e.getConfigureNeedKeyword() + "\","); if (e.getConfigureParaKeyword() == null) { jsonStr.append("\"configureParaKeyword\""); jsonStr.append(":"); jsonStr.append("\"\","); } else { jsonStr.append("\"configureParaKeyword\""); jsonStr.append(":"); jsonStr.append("\"" + e.getConfigureParaKeyword() + "\","); } if (e.getConfigureUnmatchReg() == null) { jsonStr.append("\"configureUnmatchReg\""); jsonStr.append(":"); jsonStr.append("\"\","); } else { jsonStr.append("\"configureUnmatchReg\""); jsonStr.append(":"); jsonStr.append("\"" + e.getConfigureUnmatchReg() + "\","); } if (e.getConfigureParaOther() == null) { jsonStr.append("\"configureParaOther\""); jsonStr.append(":"); jsonStr.append("\"\","); } else { jsonStr.append("\"configureParaOther\""); jsonStr.append(":"); jsonStr.append("\"" + e.getConfigureParaOther() + "\","); } jsonStr.append("\"collectionName\""); jsonStr.append(":"); jsonStr.append("\"" + e.getInstitutionalCollection().getName() + "\""); return jsonStr.toString(); } /** * 方法功能说明: * @参数: @param configures * @参数: @return * @return String * @throws */ public static String getStrFromCrawlerConfigureListForWebservice( List<CrawlerConfigure> configures) { StringBuffer jsonStr = new StringBuffer("["); for (CrawlerConfigure e : configures) { jsonStr.append("{"); jsonStr.append("\"configureId\""); jsonStr.append(":"); jsonStr.append("\"" + e.getConfigureId() + "\","); if (e.getConfigureUrl() == null) { jsonStr.append("\"configureUrl\""); jsonStr.append(":"); jsonStr.append("\"\","); } else { jsonStr.append("\"configureUrl\""); jsonStr.append(":"); jsonStr.append("\"" + e.getConfigureUrl() + "\","); } if (e.getConfigureMc() == null) { jsonStr.append("\"configureMc\""); jsonStr.append(":"); jsonStr.append("\"\","); } else { jsonStr.append("\"configureMc\""); jsonStr.append(":"); jsonStr.append("\"" + e.getConfigureMc() + "\","); } if (e.getConfigureCharset() == null) { jsonStr.append("\"configureCharset\""); jsonStr.append(":"); jsonStr.append("\"\","); } else { jsonStr.append("\"configureCharset\""); jsonStr.append(":"); jsonStr.append("\"" + e.getConfigureCharset() + "\","); } if (e.getConfigureMatchReg() == null) { jsonStr.append("\"configureMatchReg\""); jsonStr.append(":"); jsonStr.append("\"\","); } else { jsonStr.append("\"configureMatchReg\""); jsonStr.append(":"); jsonStr.append("\"" + e.getConfigureMatchReg() + "\","); } jsonStr.append("\"configureNeedKeyword\""); jsonStr.append(":"); jsonStr.append("\"" + e.getConfigureNeedKeyword() + "\","); if (e.getConfigureParaKeyword() == null) { jsonStr.append("\"configureParaKeyword\""); jsonStr.append(":"); jsonStr.append("\"\","); } else { jsonStr.append("\"configureParaKeyword\""); jsonStr.append(":"); jsonStr.append("\"" + e.getConfigureParaKeyword() + "\","); } if (e.getConfigureUnmatchReg() == null) { jsonStr.append("\"configureUnmatchReg\""); jsonStr.append(":"); jsonStr.append("\"\","); } else { jsonStr.append("\"configureUnmatchReg\""); jsonStr.append(":"); jsonStr.append("\"" + e.getConfigureUnmatchReg() + "\","); } if (e.getConfigureParaOther() == null) { jsonStr.append("\"configureParaOther\""); jsonStr.append(":"); jsonStr.append("\"\","); } else { jsonStr.append("\"configureParaOther\""); jsonStr.append(":"); jsonStr.append("\"" + e.getConfigureParaOther() + "\","); } jsonStr.append("\"collectionName\""); jsonStr.append(":"); jsonStr.append("\"" + e.getInstitutionalCollection().getName() + "\""); jsonStr.append("},"); } return jsonStr.substring(0, jsonStr.length() - 1) + "]"; } }

    5.效果图:

    主界面: 编辑: 删除:
    转载请注明原文地址: https://ju.6miu.com/read-20096.html

    最新回复(0)