我解释我的问题:
我正在使用 Liferay 6.1 并试图了解文档和媒体控制面板的功能。
在我的 portlet 视图中,我有一个表格,其中显示了一个文档列表,其中一个字段是修改日期。默认情况下,此日期是文件修改日期。
在java代码中,我需要检查这个日期字段(可选),如果它是空的,我保留旧代码(显示文档的修改日期),如果它不为空,那么我想显示它的值(可选日期)。
我创建了一个文档类型,并在文档类型中添加了一个名为 date(optional) 的字段来手动添加日期。
我不明白的一件事是,当尝试添加字段 ddm-date 类型时,日期字段应该有一个默认值,它不能为空,我错了吗?
我的第一个问题,如何将 ddm-date 类型设置为空?
在创建字段时,当我将 None 放入首选值时,CMS 会自动将其更改为默认值,并且在表单中(添加新文件时),可选日期的选择数据不包含空值,如我所见(默认情况下,它包含今天的日期)。
所以我使用文本字段作为类型。
我的主要问题:
我显示了可选日期,但它会覆盖所有创建日期。在显示中我只得到可选日期和最后一个。我解释了当我从文档和媒体控制面板添加一个带有可选日期字段的文件时,例如值“2012/08/01”,表中的所有值都被这个值替换。
我使用 java 代码发送了我的变量,renderRequest.setAttribute
并使用 JSTL - Core<fmt: formatDate value = "$ {optionalDate}" pattern = "MMM yyyy" />
标记在我的视图中显示了它。我的 portlet 也是从 MVCPortlet 扩展而来的。
为什么 doView 是渲染renderRequest.setAttribute
返回最后一个变量?
在我的java代码中:
for(DLFileEntry file : listFiles){
try {
Map<String, Fields> fieldsMap = file.getFieldsMap(file.getFileVersion().getFileVersionId());
if(fieldsMap.values().size() <= 0)
listContextFiles.remove(file);
for (Fields fields : fieldsMap.values()) {
if(...){
if(...){
}
else{
if(fields.get("optionaldate") != null ) {
DateFormat dateFormat1 = new SimpleDateFormat("yyyy/MM/dd");
String _optionalDate = (String) fields.get("optionaldate").getValue();
Date optionalDate = dateFormat1.parse(_optionalDate);
file.setModifiedDate(optionalDate);
renderRequest.setAttribute("optionalDate", optionalDate);
System.out.println(file.getModifiedDate());
listDate.add(dateFormat.format(file.getModifiedDate()));
}
else{
renderRequest.setAttribute("optionalDate", file.getModifiedDate());
if(!listDate.contains(dateFormat.format(file.getModifiedDate()))){
listDate.add(dateFormat.format(file.getModifiedDate()));
}
}
//other conditions
}
...
在我看来.jsp:
<liferay-ui:search-container iteratorURL="<%=actionURL%>" delta="10"
emptyResultsMessage="no-documents">
<liferay-ui:search-container-results total="<%=list.size()%>"
results="<%=ListUtil.subList(list,
searchContainer.getStart(),
searchContainer.getEnd())%>" />
<liferay-ui:search-container-row modelVar="file"
className="DLFileEntry">
<!--other code-->
<liferay-ui:search-container-column-text name='date'
cssClass="txt-capitalize width-10">
<fmt:formatDate value="${optionalDate}" pattern="MMM yyyy" />
</liferay-ui:search-container-column-text>
<!--other code-->
</liferay-ui:search-container-row>
</liferay-ui:search-container>
有没有一种干净的方法来做到这一切?
有人可以告诉我我的代码有什么问题吗?
如果您展开所有循环,首先在 Java 中,然后在 JSP 中,您基本上是在执行这些命令(按此顺序),给出 3 个要显示的对象:
renderRequest.setAttribute("optionalDate", someDate);
renderRequest.setAttribute("optionalDate", someOtherDate);
renderRequest.setAttribute("optionalDate", yetSomeOtherDate);
然后读取 JSP 中的值:
renderRequest.getAttribute("optionalDate");
renderRequest.getAttribute("optionalDate");
renderRequest.getAttribute("optionalDate");
既然setAttribute
不是pushToQueue
,也不getAttribute
是nextFromQueue
(形象地说)yetSomeOtherDate
,自然而然,您只会获得JSP 循环的所有迭代。
你可以
"optionalDate-" + file.getId()
而不仅仅是泛型"optionalDate"
。然后在 JSP 中使用相同的键构造来读取正确的值。(恕我直言,这很不雅,但可能对您的用例很实用)本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句