样例代码

String formDefId = httpServletRequest.getParameter("formDefId");
String appId = httpServletRequest.getParameter("appId");
String appVersion = httpServletRequest.getParameter("appVersion");

Map<String, Object> properties = getFormPropertiesByFormDefId(formDefId, appId, appVersion);


/**
 * 根据表单ID获取配置项
 * @param formDefId
 * @param appId
 * @param appVersion
 * @return
 */
private Map<String, Object> getFormPropertiesByFormDefId( String formDefId, String appId, String appVersion) {
    AppService appService = (AppService) AppUtil.getApplicationContext().getBean("appService");
    AppDefinition appDef = appService.loadAppDefinition(appId, appVersion);
    FormDefinitionDao formDefinitionDao = (FormDefinitionDao) AppUtil.getApplicationContext().getBean("formDefinitionDao");
    FormDefinition formDefinition = formDefinitionDao.loadById(formDefId, appDef);
    if(formDefinition!=null){
        String json = formDefinition.getJson();
        // render json get properties
        LogUtil.info("json",json);
        Map<String, Object> properties = new HashMap<>();
        try {
            properties = parseChildElementsFromJsonObject(new JSONObject(json),properties);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return properties;
    }
    return  null;
}

/**
 * Parse child elements from element json object
 * @param obj
 * @throws Exception
 */
public Map<String,Object> parseChildElementsFromJsonObject(JSONObject obj,Map<String,Object> props ) throws Exception {
    if (!obj.isNull(FormUtil.PROPERTY_ELEMENTS)) {
        JSONArray elements = obj.getJSONArray(FormUtil.PROPERTY_ELEMENTS);
        if (elements != null && elements.length() > 0) {
            for (int i = 0; i < elements.length(); i++) {
                JSONObject childObj = (JSONObject) elements.get(i);
                Map<String, Object> map = FormUtil.parsePropertyFromJsonObject(childObj);
                props.putAll(map);
                parseChildElementsFromJsonObject(childObj,props);
            }
        }
    }
    return props;
}


  • No labels