【问题标题】:Why portlet is renderRequest.setAttribute return the last variable?为什么 portlet 是 renderRequest.setAttribute 返回最后一个变量?
【发布时间】:2017-10-20 20:31:52
【问题描述】:

我解释我的问题:

我正在使用 Liferay 6.1,我正在尝试了解文档和媒体控制面板的功能。

  • 在我的 portlet 视图中,我有一个表格,其中显示了文档列表,其中一个字段是修改日期。默认情况下,此日期为文件修改日期。

  • 在java代码中我需要检查这个日期字段(可选),如果它为空我保留旧代码(显示文档的修改日期)如果它不为空那么我想显示它value 代替(可选日期的值)。

  • 我创建了一个文档类型,并在文档类型中添加了一个名为 date(optional) 的字段来手动添加日期。

  • 有一件事我不明白,当尝试添加字段ddm-date类型时,日期字段应该有一个默认值,不能为空 我错了吗?

我的第一个问题,如何将 ddm-date 类型设置为空?

  • 在创建字段时,当我将 None 放入首选值时,CMS 会自动将其更改为默认值,并且在表单中(添加新文件时)可选日期的选择数据不包含我看到的空值(默认情况下它包含今天的日期)。

  • 所以我使用文本字段作为类型。

我的主要问题:

我显示了可选日期,但它会覆盖所有创建日期。在显示中我只得到可选的日期和最后一个。我解释说,当我从“文档和媒体”控制面板添加一个带有可选日期字段的文件时,例如值“2012/08/01”,表中的所有值都被这个值替换。

我使用renderRequest.setAttribute从java代码发送了我的变量 我用 JSTL - Core <fmt: formatDate value = "$ {optionalDate}" pattern = "MMM yyyy" /> 标记在我的视图中显示它。我的 portlet 也是从 MVCPortlet 扩展而来的。

为什么View是渲染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 liferay liferay-6


【解决方案1】:

如果您展开所有循环,首先在 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(形象地说),所以自然而然,您只会在JSP 循环的所有迭代中获得yetSomeOtherDate

你可以

  • 计算 JSP 中的值,
  • 根据当前对象存储一个为您进行计算的对象,
  • 使用对象的 ID 作为其键的一部分来存储属性,例如"optionalDate-" + file.getId() 而不仅仅是通用的 "optionalDate"。然后在 JSP 中使用相同的键构造来读取正确的值。 (恕我直言,这很不优雅,但对于您的用例可能很实用)

【讨论】:

    猜你喜欢
    • 2016-05-07
    • 2021-12-27
    • 2020-02-07
    • 2016-08-12
    • 1970-01-01
    • 2014-10-21
    • 2014-12-25
    • 2013-02-08
    • 2015-08-30
    相关资源
    最近更新 更多