前台:
//生成代码 function genCode() { var rowsSel = datagrid.datagrid("getSelections"); if(rowsSel.length != 1) { showWarnMsg("请选择一条数据!"); return; } var id = rowsSel[0].id; $.post("genCode", {headId: id}, function(data) { log(data); showMsg(data.msg); }); }
后台:请求调用方法,传入headId com.tspace.controller.TspaceHeadController#genCode
$.post("genCode", {headId: id}, function(data) {
/代码生成 public void genCode() throws IOException, TemplateException { int headId = getParaToInt("headId");//获取headId TspaceService tspaceService = Duang.duang(TspaceService.class);//做增强 TspaceMeta metaMap = tspaceService.getMetaData(headId);//根据headId获取集合 TspaceHead head = metaMap.getHead();//获取head String tableName = head.getTableName();//获取表名 String className = tableName.substring(0, 1).toUpperCase() + tableName.substring(1);//获取生成calss int index = className.indexOf("_"); while(index > 0) { String s = className.substring(index + 1, index + 2); className = className.replace("_" + s, s.toUpperCase()); index = className.indexOf("_"); } String lowerClassName = className.substring(0, 1).toLowerCase() + className.substring(1); Map<String, Object> mateDate = metaMap.toMap();//封装className和queryPara mateDate.put("className", className); mateDate.put("queryPara", new HashMap<>()); //复制模板 copyTemp("listPage.html"); //列表页面 copyTemp("addPage.html"); //增加页面 copyTemp("updatePage.html"); //编辑页面 copyTemp("detailPage.html"); //详情页面
//从配置中读取定义好的生成文件的路径 String genCodePath = PropKit.get("genCodePath") + className + "/"; String genCodePagePath = genCodePath + lowerClassName + "/"; new File(genCodePath).mkdirs(); new File(genCodePagePath).mkdirs(); //定义生成的文件名和文件存放的路径 gen(mateDate, "/tspace/tspace/genCode/listPage.html", genCodePagePath + "list.html"); gen(mateDate, "/tspace/tspace/genCode/addPage.html", genCodePagePath + "add.html"); gen(mateDate, "/tspace/tspace/genCode/updatePage.html", genCodePagePath + "update.html"); gen(mateDate, "/tspace/tspace/genCode/detailPage.html", genCodePagePath + "detail.html"); gen(mateDate, "/tspace/tspace/genCode/controller.html", genCodePath + className + "Controller.java"); gen(mateDate, "/tspace/tspace/genCode/model.html", genCodePath + className + ".java"); //记录日志 addOpLog("[在线表单] 生成代码"); renderSuccess("代码生成成功!保存在" + genCodePath); }
把代码拷贝到指定位置和小说管理同级别的地方