1、前台js保存
<script type="text/javascript" src="/ecloud/js/js/jquery.min.js"></script> <script type="text/javascript"> $(function(){ //发送验证码 $("#saveButton").click(function(){ var templateName = $("#templateName").val(); if(templateName == ""){ alert("请输入模板名称"); return; } $.ajax({ url:"control/PrintTemplateSaveServlet", data:{ guid:$("#tId").val(), planId:$("#planId").val(), userId:$("#userId").val(), templateName:$("#templateName").val(), templateContent:$("#print_content").html(), width:$("#template_width").val(), height:$("#template_height").val() /* , templateStyle:$("#print_style").html() */ }, type:"post", async:false, dataType:"json", success : function(data) { if(data.result == 'success'){ alert("操作成功"); } else if(data.result == 'nameError') { alert("模板名称已存在"); } else { alert("操作失败"); } }, }); }); }); </script>
2、controller配置
<request-map uri="PrintTemplateSaveServlet"> <security https="false" auth="false"/> <event type="service" invoke="addPrintTemplateButton"/> <response name="success" type="request" value="json"/> <response name="error" type="request" value="json"/> <response name="nameError" type="request" value="json"/> </request-map>
3、service配置
<service name="addPrintTemplateButton" engine="java" location="org.eheluo.ecloud.view.certificate.certificatePrint.PrintTemplateServlet" invoke="addPrintTemplateButton"> <attribute name="guid" mode="IN" type="String" optional="true"/> <attribute name="planId" mode="IN" type="String" optional="true"/> <attribute name="userId" mode="IN" type="String" optional="true"/> <attribute name="templateName" mode="IN" type="String" optional="true"/> <attribute name="templateContent" mode="IN" type="String" allow-html="any" optional="true"/> <attribute name="width" mode="IN" type="Integer" optional="true"/> <attribute name="height" mode="IN" type="Integer" optional="true"/> <!-- <attribute name="templateStyle" mode="IN" type="String" allow-html="any" optional="true"/> --> <attribute name="result" mode="OUT" type="String"/> </service>
3、service保存方法
/** * 保存模板 * @param ctx * @param context * @return */ public Map<String, Object> addPrintTemplateButton(DispatchContext ctx, Map<String, ?> context) { String guid = (String) context.get("guid"); String planId = (String) context.get("planId"); String userId = (String) context.get("userId"); String templateName = (String) context.get("templateName"); String templateContent = (String) context.get("templateContent"); Integer width = (Integer) context.get("width"); Integer height = (Integer) context.get("height"); // String templateStyle = (String) context.get("templateStyle"); List<EntityCondition> ec = FastList.newInstance(); ec.add(EntityCondition.makeCondition("planGuid", EntityOperator.EQUALS, planId)); // ec.add(EntityCondition.makeCondition("userId", EntityOperator.EQUALS, planId)); ec.add(EntityCondition.makeCondition("templateName", EntityOperator.EQUALS, templateName)); // Map createNewsCtx = UtilMisc.toMap("planGuid", planId); // createNewsCtx.put("userId", userId); // createNewsCtx.put("templateName", templateName); Map<String, Object> resultMap = new HashMap<String, Object>(); try { // long count = base.getDelegator().findCountByCondition("CertificatePrintTemplate", ec.size() > 0 ? EntityCondition.makeCondition(ec, EntityOperator.AND) : null, null, null); List<GenericValue> gvList = base.getDelegator().findList("CertificatePrintTemplate", ec.size() > 0 ? EntityCondition.makeCondition(ec, EntityOperator.AND) : null, null, null, null, false); if (gvList != null && gvList.size() > 0 && !gvList.get(0).getString("guid").equals(guid)) { resultMap.put("result", "nameError"); } else { Map createNewsCtx = UtilMisc.toMap("userLoginId", userId); GenericValue userLogin = base.getDelegator().findOne("UserLogin", createNewsCtx, false); String name = userLogin == null ? userId : userLogin.getString("name"); GenericValue certificatePrintTemplate = null; if (guid == null || "".equals(guid) || "null".equals(guid)) { certificatePrintTemplate = base.getDelegator().makeValue("CertificatePrintTemplate"); certificatePrintTemplate.put("guid", UUID.randomUUID().toString().toUpperCase()); certificatePrintTemplate.put("userId", userId); certificatePrintTemplate.put("userName", name); certificatePrintTemplate.put("templateName", templateName); certificatePrintTemplate.put("templateContent", templateContent); certificatePrintTemplate.put("width", width); certificatePrintTemplate.put("height", height); // certificatePrintTemplate.put("templateStyle", templateStyle); certificatePrintTemplate.put("planGuid", planId); certificatePrintTemplate.put("createdTime", new java.sql.Timestamp(new Date().getTime())); base.getDelegator().create(certificatePrintTemplate); } else { certificatePrintTemplate = base.getDelegator().findByPrimaryKey("CertificatePrintTemplate", UtilMisc.toMap("guid", guid)); certificatePrintTemplate.put("templateName", templateName); certificatePrintTemplate.put("templateContent", templateContent); certificatePrintTemplate.put("width", width); certificatePrintTemplate.put("height", height); // certificatePrintTemplate.put("templateStyle", templateStyle); base.getDelegator().store(certificatePrintTemplate); } resultMap.put("result", "success"); } } catch (GenericEntityException e) { e.printStackTrace(); resultMap.put("result", "error"); } return resultMap; }
注意:因为保存模板是html代码,所以要在service配置时,在模板内容的入参加一个参数: allow-html="any"
<attribute name="templateContent" mode="IN" type="String" allow-html="any" optional="true"/>
否则会报错:In field [templateContent] less-than (<) and greater-than (>) symbols are not allowed
意思是:"<",">"不能出现在这个域内