浏览 1337 次
锁定老帖子 主题:页面在多系统应用中同步
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2011-12-12
业务场景: 用户去银行营业厅办理的业务时 需要在预处理机上面填写代办业务信息或者之前在 网上预约系统填写过就可以到柜员直接办理业务。 涉及到预处理机 和 网上预约系统 两个系统,如何在开发的时候同步它们共同的业务数据也就是页面可以提供开发效率。 以及在每个系统中提取公用页面比如 formEdit.jsp (填写页面)与formConfirm.jsp (显示页面)
以fromEdit.jsp为例: 在公用页面fromEdit.jsp页面加入jsp定义的标签<mct:form ,formConfig.template 没不同的页面模板,formObject不同模板的业务数据
<div class="centerdd" style="width:780px"> <div id="formContent1"> <c:if test="${!empty(formObject)}"> <mct:form formObject="${formObject}" template="${formConfig.template}"/> </c:if> </div> </div>
<mct:form 定义:
/** * 表单模板输入标签。该标签输出基于 FreeMarker 的模板。 * * */ public class FormTemplateTag extends TagSupport { /** * 表单对象 */ Object formObject; /** * 模板名称 */ String template; /** * */ private static final long serialVersionUID = 7318973001234606567L; public FormTemplateTag() { } public int doStartTag() { Configuration conf = new Configuration(); conf.setClassForTemplateLoading(FormTemplateTag.class, "/templates/"); try { Template t = conf.getTemplate(template, "UTF-8"); Map<String, Object> root = new HashMap<String, Object>(); Object tempFormObject = formObject; if (formObject instanceof String) { Object realObj = pageContext.getAttribute((String) formObject); tempFormObject = realObj; } if (!(tempFormObject instanceof FormContentObject)) throw new RuntimeException("Invalid form object: A FormContentObject is expected."); root.put("formObject", tempFormObject); root.put("environment", getEnvironment()); t.process(root, this.pageContext.getOut()); } catch (IOException e) { e.printStackTrace(); } catch (TemplateException e) { e.printStackTrace(); } return Tag.SKIP_BODY; } //字典数据 private Object getEnvironment() { Map<String, Object> environment = new HashMap<String, Object>(); environment.put("currencyTypes", Constants.CURRENCY.entrySet()); environment.put("corporationTypes", Constants.CORPACCOUNTTYPE.entrySet()); environment.put("credentialTypes", Constants.CREDENTIALTYPE.entrySet()); environment.put("industries", Constants.INDUSTRYCLASSIFICATION.entrySet()); environment.put("corpAccountTypes", Constants.CORPACCOUNTTYPE.entrySet()); environment.put("nationality", Constants.NATIONALITY.entrySet()); return environment; } public Object getFormObject() { return formObject; } public void setFormObject(Object formObject) { this.formObject = formObject; } public String getTemplate() { return template; } public void setTemplate(String template) { this.template = template; } }
业务数据配置文件forms.xml:
<?xml version="1.0" encoding="UTF-8"?> <forms> <!-- ===================================== --> <!-- 对公业务表单定义 --> <!-- ===================================== --> <form name="corporationOpenAccount" productId="1" productType="对公"> <template state="edit" id="corporationOpenAccountEdit" name="对公结算账户开户" description="请输入您的对公账户开户信息" templatePath="corporation/openAccount/corporationOpenAccountEdit.html" formObjectCreator="com.ibm.banking.mct.form.ext.CorporationOpenAccountFormBuilder" /> <template state="confirm" id="corporationOpenAccountConfirm" name="对公结算账户开户" description="请确认您的对公账户开户信息" templatePath="corporation/openAccount/corporationOpenAccountConfirm.html" formObjectCreator="com.ibm.banking.mct.form.ext.CorporationOpenAccountFormBuilder" /> </form> <form name="corporationOnlineBankingApp" productId="111" productType="对公"> <template state="edit" id="corporationOnlineBankingAppEdit" name="对公网银开户申请单" description="请输入您的对公网银开户申请信息" templatePath="corporation/onlineBankingApp/corporationOnlineBankingAppEdit.html" formObjectCreator="com.ibm.banking.mct.form.ext.CorporationOnlineBankingAppFormBuilder" /> <template state="confirm" id="corporationOnlineBankingAppConfirm" name="对公网银开户申请单" description="请确认您的对公账户开户信息" templatePath="corporation/onlineBankingApp/corporationOnlineBankingAppConfirm.html" formObjectCreator="com.ibm.banking.mct.form.ext.CorporationOnlineBankingAppFormBuilder" /> </form> <!-- ===================================== --> <!-- 对私业务表单定义 --> <!-- ===================================== --> <form name="largeCashWithdrawal" productId="18" productType="对私"> <template state="edit" id="largeCashWithdrawalEdit" name="大额取现" description="请输入您的大额取现信息" templatePath="personal/largeCashWithdraw/largeCashWithdrawalEdit.html" formObjectCreator="com.ibm.banking.mct.form.ext.DefaultFormObjectBuilder" /> <template state="confirm" id="largeCashWithdrawalConfirm" name="大额取现" description="请确认您的大额取现信息" templatePath="personal/largeCashWithdraw/largeCashWithdrawalConfirm.html" formObjectCreator="com.ibm.banking.mct.form.ext.DefaultFormObjectBuilder" /> </form> <form name="onsiteTransfer" productId="177" productType="对私"> <template state="edit" id="onsiteTransferEdit" name="行内转账" description="请输入行内转账信息" templatePath="personal/onsiteTransfer/onsiteTransferEdit.html" formObjectCreator="com.ibm.banking.mct.form.ext.DefaultFormObjectBuilder" /> <template state="confirm" id="onsiteTransferConfirm" name="行内转账" description="请确认您行内转账信息" templatePath="personal/onsiteTransfer/onsiteTransferConfirm.html" formObjectCreator="com.ibm.banking.mct.form.ext.DefaultFormObjectBuilder" /> </form> <form name="personalOpenAccount" productId="137" productType="对私"> <template state="edit" id="personalOpenAccountEdit" name="个人账户开户及综合服务申请表" description="请输入您的开户信息" templatePath="personal/openAccount/personalOpenAccountEdit.html" formObjectCreator="com.ibm.banking.mct.form.ext.PersonalAccountOpenFormBuilder" /> <template state="confirm" id="personalOpenAccountConfirm" name="个人账户开户及综合服务申请表" description="请输入您的开户信息" templatePath="personal/openAccount/personalOpenAccountConfirm.html" formObjectCreator="com.ibm.banking.mct.form.ext.PersonalAccountOpenFormBuilder" /> </form> </forms>
业务构造设计:
/** * FormObjectBuilder 创建器,用于创建 FormContentObject * * * */ public interface FormObjectBuilder { /** * 创建出 FormContentObject * @return */ public FormContentObject create(); /** * 创建出 FormContentObject 并提供 Context * @param context * @return */ public FormContentObject create(Map<String, Object> context); }
public class DefaultFormObjectBuilder implements FormObjectBuilder { @Override public FormContentObject create() { FormContentObject object = new FormContentObject(); Field[] fields = FormContentObject.class.getDeclaredFields(); for (Field f : fields) { try { if (Modifier.isFinal(f.getModifiers())) continue; Class<?> fieldClass = f.getType(); Object value = fieldClass.newInstance(); f.setAccessible(true); f.set(object, value); } catch (Exception e) { System.err.println("Error when setting field: " + f.getName()); continue; } } return object; } @Override public FormContentObject create(Map<String, Object> context) { return create(); } }
public class CorporationOnlineBankingAppFormBuilder implements FormObjectBuilder { @Override public FormContentObject create() { FormContentObject object = new FormContentObject(); CorporationOnlineBankingAppForm form = new CorporationOnlineBankingAppForm(); object.setCorporationOnlineBankingAppForm(form); return object; } @Override public FormContentObject create(Map<String, Object> context) { CorporationCustomerInfo corporation = (CorporationCustomerInfo) context.get(ActionConstants.SESSION_CORPORATION_INFO); if (corporation != null) { FormContentObject object = new FormContentObject(); CorporationOnlineBankingAppForm form = BeanUtility.createAndMapping(CorporationOnlineBankingAppForm.class, corporation); object.setCorporationOnlineBankingAppForm(form); return object; } return create(); }
public class CorporationOpenAccountFormBuilder implements FormObjectBuilder { @Override public FormContentObject create() { FormContentObject object = new FormContentObject(); CorporateAccountOpenForm form = new CorporateAccountOpenForm(); object.setCorporateAccountOpenForm(form); return object; } @Override public FormContentObject create(Map<String, Object> context) { CorporationCustomerInfo corporation = (CorporationCustomerInfo) context.get(ActionConstants.SESSION_CORPORATION_INFO); if (corporation != null) { CorporateAccountOpenForm form = BeanUtility.createAndMapping(CorporateAccountOpenForm.class, corporation); FormContentObject object = new FormContentObject(); object.setCorporateAccountOpenForm(form); return object; } return create(); } }
初始化数据:
/** * 表单模板配置,规则如下: * * 每个表单被分配一个名字,如:corporationOpenAccount * 每个表单模板也有一个名字,如:corporationOpenAccountEdit * * 表单目前有两种状态,一种是编辑 (edit) 状态,一种是确认状态 (confirm) * 提取表单那模板时,需要提供表单名和状态作为参数,然后可以获取到某个具体的模板。 * * */ public class FormTemplateConfig { public static final String STATE_EDIT = "edit"; public static final String STATE_CONFIRM = "confirm"; String formId; String name; String productId; String productType; String description; String template; FormObjectBuilder formObjectBuilder; static Map<String, FormTemplateConfig> configs = new HashMap<String, FormTemplateConfig>(); static Map<String, Form> formConfigs = new HashMap<String, Form>(); static { InputStream is = FormTemplateConfig.class.getResourceAsStream("/forms.xml"); FormList formList = JAXBHelper.unmarshal(is, FormList.class); for (Form form: formList.getFormList()) { formConfigs.put(form.getProductId(), form); List<Template> templates = form.getTemplateList(); for (Template template: templates) { String templateKey = form.getName() + "_" + template.getState(); FormObjectBuilder builder = createFormObjectBuilder(template.getFormObjectCreator()); FormTemplateConfig config = new FormTemplateConfig( template.getId(), template.getName(), form.getProductId(), form.getProductType(), template.getDescription(), template.getTemplatePath(), builder ); configs.put(templateKey, config); } } } static { // //======================================================================== // // 对公业务表单定义 // //======================================================================== // // // // // 对公结算账户开户 // // // configs.put("corporationOpenAccount_edit", // new FormTemplateConfig( // "corporationOpenAccountEdit", // "对公结算账户开户", // "111", // "对公", // "请输入您的对公账户开户信息", // "corporation/openAccount/corporationOpenAccountEdit.html", // new CorporationOpenAccountFormBuilder())); // configs.put("corporationOpenAccount_confirm", // new FormTemplateConfig( // "corporationOpenAccountConfirm", // "对公结算账户开户", // "111", // "对公", // "请确认您的对公账户开户信息", // "corporation/openAccount/corporationOpenAccountConfirm.html", // new CorporationOpenAccountFormBuilder())); // // // // // 对公网银开户 // // // configs.put("corporationOnlineBankingApp_edit", // new FormTemplateConfig( // "corporationOnlineBankingAppEdit", // "对公网银开户申请单", // "111", // "对公", // "请输入您的对公网银开户申请信息", // "corporation/onlineBankingApp/corporationOnlineBankingAppEdit.html", // new CorporationOnlineBankingAppFormBuilder())); // configs.put("corporationOnlineBankingApp_confirm", // new FormTemplateConfig( // "corporationOnlineBankingAppConfirm", // "对公网银开户申请单", // "111", // "对公", // "请确认您的对公网银开户申请信息", // "corporation/onlineBankingApp/corporationOnlineBankingAppConfirm.html", // new CorporationOnlineBankingAppFormBuilder())); // // //======================================================================== // // 对私业务表单定义 // //======================================================================== // // // // // 大额取现 // // // configs.put("largeCashWithdrawal_confirm", // new FormTemplateConfig( // "largeCashWithdrawalConfirm", // "大额取现", // "111", // "对私", // "请确认您的大额取现信息", // "personal/largeCashWithdraw/largeCashWithdrawalConfirm.html", // new DefaultFormObjectBuilder())); // configs.put("largeCashWithdrawal_edit", // new FormTemplateConfig( // "largeCashWithdrawalEdit", // "大额取现", // "111", // "对私", // "请输入您的大额取现信息", // "personal/largeCashWithdraw/largeCashWithdrawalEdit.html", // new DefaultFormObjectBuilder())); // // // // // 行内转账 // // // configs.put("onsiteTransfer_confirm", // new FormTemplateConfig( // "onsiteTransferConfirm", // "行内转账", // "111", // "对私", // "请确认您行内转账信息", // "personal/onsiteTransfer/onsiteTransferConfirm.html", // new DefaultFormObjectBuilder())); // configs.put("onsiteTransfer_edit", // new FormTemplateConfig( // "onsiteTransferEdit", // "行内转账", // "111", // "对私", // "请输入您的行内转账信息", // "personal/onsiteTransfer/onsiteTransferEdit.html", // new DefaultFormObjectBuilder() )); // // // // // 个人账户开户及综合服务申请表 // // // configs.put("personalOpenAccount_edit", // new FormTemplateConfig( // "personalOpenAccountEdit", // "个人账户开户及综合服务申请表", // "111", // "对私", // "请输入您的开户信息", // "personal/openAccount/personalOpenAccountEdit.html", // new PersonalAccountOpenFormBuilder() )); // configs.put("personalOpenAccount_confirm", // new FormTemplateConfig( // "personalOpenAccountConfirm", // "个人账户开户及综合服务申请表", // "111", // "对私", // "请输入您的开户信息", // "personal/openAccount/personalOpenAccountConfirm.html", // new PersonalAccountOpenFormBuilder() )); } /** * 根据表单ID和表单状态获取表单模板配置 * @param formId * @param formState * @return */ public static FormTemplateConfig getConfig(String formId, String formState) { return configs.get(formId + "_" + formState); } /** * 根据产品 ID 获取 Form 配置 * @param productId * @return */ public static Form getForm(String productId) { return formConfigs.get(productId); } /** * 根据产品ID 获取 Form 名 * @param productId * @return */ public static String getFormName(String productId) { Form form = getForm(productId); if (form == null) throw new RuntimeException("No form is found for product id: " + productId); return form.getName(); } @SuppressWarnings("unchecked") private static FormObjectBuilder createFormObjectBuilder(String formObjectCreatorClassName) { try { Class<FormObjectBuilder> builderClass = (Class<FormObjectBuilder>) Class.forName(formObjectCreatorClassName); return builderClass.newInstance(); } catch (Throwable e) { e.printStackTrace(); return new DefaultFormObjectBuilder(); } } FormTemplateConfig(String formId, String name, String productId, String productType, String description, String template, FormObjectBuilder builder) { this.formId = formId; this.name = name; this.productId = productId; this.productType = productType; this.description = description; this.template = template; this.formObjectBuilder = builder; } public String getFormId() { return formId; } public void setFormId(String formId) { this.formId = formId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getTemplate() { return template; } public void setTemplate(String template) { this.template = template; } public String getProductId() { return productId; } public void setProductId(String productId) { this.productId = productId; } public FormObjectBuilder getFormObjectBuilder() { return formObjectBuilder; } public void setFormObjectBuilder(FormObjectBuilder formObjectBuilder) { this.formObjectBuilder = formObjectBuilder; } public String getProductType() { return productType; } public void setProductType(String productType) { this.productType = productType; } public static void main(String[] args) throws IOException { InputStream is = FormTemplateConfig.class.getResourceAsStream("/forms.xml"); System.out.println(is); if (is == null) throw new RuntimeException("No resource is found."); // ByteArrayOutputStream out = new ByteArrayOutputStream(); // int count = 0; // byte[] buf = new byte[1024]; // while (count >=0) // { // count = is.read(buf); // if (count > 0) out.write(buf, 0, count); // } // System.out.println(out.toString()); // // is.reset(); FormList forms = JAXBHelper.unmarshal(is, FormList.class); System.out.println(forms.getFormList().size()); } }
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |