旧版Liferay自定义表单字段在JSP中展示方法咨询
在JSP中展示自定义DDM文件上传字段的步骤
1. 确认模块部署状态
先确保你用Blade CLI生成的自定义字段模块已经成功部署到Liferay,并且在控制面板的「数据定义」中能找到consignes-imageupload-form-field这个字段类型,说明Liferay已经识别到你的自定义字段。
2. 引入JSP依赖资源
在你的JSP页面头部添加DDM标签库引用,并加载自定义字段的JS模块:
<%@ taglib prefix="liferay-ddm" uri="http://liferay.com/tld/ddm" %> <%@ taglib prefix="liferay-frontend" uri="http://liferay.com/tld/frontend" %> <!-- 加载自定义字段的JS模块,对应config.js里的模块名 --> <liferay-frontend:require module="consignes-imageupload-form-field" />
3. 渲染自定义字段
你猜测的liferay-ddm:html标签是正确的方向,具体有两种渲染方式:
方式一:直接渲染单个字段
手动构造字段的属性参数,通过标签渲染:
<% // 构造字段的基础配置 Map<String, Object> fieldProps = new HashMap<>(); fieldProps.put("label", "图片上传"); fieldProps.put("name", "customImageUpload"); fieldProps.put("readOnly", false); fieldProps.put("required", true); fieldProps.put("showLabel", true); fieldProps.put("tip", "仅支持PNG/JPG格式图片"); fieldProps.put("value", ""); // 可设置已上传文件的路径作为初始值 fieldProps.put("type", "consignes-imageupload-form-field"); %> <!-- 使用ddm:html标签渲染,template对应soy文件里的模板名 --> <liferay-ddm:html context='<%= fieldProps %>' template="ddm.ImageUpload" />
方式二:结合DDM数据定义渲染完整表单
如果你的自定义字段属于某个DDM数据定义的一部分,可直接渲染整个结构:
<% // 替换为你的数据定义ID long structureId = 12345; DDMStructure structure = DDMStructureLocalServiceUtil.getDDMStructure(structureId); Map<String, Object> renderCtx = new HashMap<>(); renderCtx.put("ddmStructure", structure); %> <liferay-ddm:render-structure context='<%= renderCtx %>' />
4. 关键注意事项
- 确保表单设置了
enctype="multipart/form-data",否则无法接收上传的文件:
<form action="<portlet:actionURL />" method="post" enctype="multipart/form-data"> <!-- 自定义字段渲染代码 --> <button type="submit">提交</button> </form>
- 检查
config.js中的MODULE_PATH是否正确指向模块资源路径,确保Soy模板编译后的JS能被正确加载。 - 旧版Liferay(如6.x/7.0)的DDM标签属性可能略有差异,需对应版本调整,但核心逻辑一致。
内容的提问来源于stack exchange,提问作者Hector
相关产品推荐
相关产品推荐

