代码之家  ›  专栏  ›  技术社区  ›  Dark Magic

为什么portlet是renderRequest。setAttribute是否返回最后一个变量?

  •  0
  • Dark Magic  · 技术社区  · 7 年前

    我解释我的问题:

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

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

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

    • 我不明白的一件事是,当试图添加字段ddm date type时,日期字段应该有一个默认值,它不能为空 我错了吗?

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

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

    我的主要问题:

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

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

    为什么渲染doView 渲染请求。集合属性 返回最后一个变量?

    在我的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>
    

    有没有一个干净的方法来做这一切?

    有人能告诉我我的代码出了什么问题吗?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Olaf Kock    7 年前

    如果您首先在Java中展开所有循环,然后在JSP中展开,那么您基本上是在执行这些命令(按此顺序),给定3个要显示的对象:

    renderRequest.setAttribute("optionalDate", someDate);
    renderRequest.setAttribute("optionalDate", someOtherDate);
    renderRequest.setAttribute("optionalDate", yetSomeOtherDate);
    

    renderRequest.getAttribute("optionalDate");
    renderRequest.getAttribute("optionalDate");
    renderRequest.getAttribute("optionalDate");
    

    setAttribute 不是 pushToQueue getAttribute 不是 nextFromQueue (比喻地说),你只会 yetSomeOtherDate 当然,对于JSP循环的所有迭代。

    你也可以

    • 计算JSP中的值,
    • 根据当前对象存储一个为您计算的对象,
    • 使用对象的ID作为其键的一部分来存储属性,例如:。 "optionalDate-" + file.getId() 而不仅仅是泛型 "optionalDate" . 然后在JSP中使用相同的键构造来读取适当的值。(我认为这很不雅观,但对于您的用例来说可能是务实的)