2017-06-06 79 views
-1

我有一個Struts2頁面,我需要顯示2個操作警告(實際上是一個標誌設置的錯誤):1每當頁面加載時,另一個取決於頁面的屬性。如何在Struts2中的多行顯示多個操作錯誤?

我使用添加行動消息的代碼:

addActionWarning(getText(MessageKeys.WARN_PROJECTSTREAM_NOT_SAFE_DELETE)); 
AddProjectStreamDependencyService addProjectStreamDependencyService = (AddProjectStreamDependencyService) ServiceFactory 
.getInstance().createService(AddProjectStreamDependencyService.class); 
isUsedByList = addProjectStreamDependencyService.getProjectStreamDependencies(oid, 
AddProjectStreamDependencyService.DEPENDENCY_TYPE_IS_USED_BY); 
if(!isUsedByList.isEmpty()){ 
    addActionWarning(getText(MessageKeys.WARN_PROJECTSTREAM_IS_USED_BY_DELETE)); 
} 

此頁面(注意,這是由每個頁面進口,所以我不能輕易改變這一點)JSP代碼:

<c:choose> 
    <c:when test='${! empty actionErrors || ! empty actionWarnings}'> 
     <s:actionerror id="globalError" cssClass="globalError" escape="false"/> 
    </c:when> 
    <c:when test='${! empty actionMessages}'> 
     <s:actionmessage id="globalError" cssClass="globalInfo" escape="false"/> 
    </c:when> 
    <c:otherwise> 
    <span id="globalError" class="globalError">&nbsp;</span> 
    </c:otherwise> 
</c:choose> 

這導致顯示以下消息中:

.globalWarning, .globalWarningDiff { 
 
    color: #ef4121; 
 
} 
 
.globalInfo, .globalWarning, .globalError, .globalInfoDiff, .globalWarningDiff, .globalErrorDiff { 
 
    font-size: 11px !important; 
 
    font-weight: bold; 
 
    text-align: left; 
 
    text-transform: uppercase; 
 
}
<span id="globalError" class="globalWarning"> 
 
\t \t \t \t warning: <span>This will also delete historical Project Stream information <br>like Level Requests, Builds, Deploys, etc...</span> \t \t \t \t warning: <span>This Project Stream is still used as a Dependency by other Project Streams.</span> \t </span>

正如您所看到的,第二條消息與第一條消息位於同一行,這看起來有點草率。我試圖解決這個使用裝飾方法:

public Collection<String> getActionErrors() { 
    return decorateToStringMethod(super.getActionErrors()); 
} 

protected static Collection<String> decorateToStringMethod(Collection<String> c) { 
    return new ArrayList<String>(c) { 
     /* (non-Javadoc) 
     * @see java.util.AbstractCollection#toString() 
     */ 
     @Override 
     public String toString() { 
      return StringUtils.join(this, "<br />"); 
     } 
    };  
} 

然而,這並不解決問題:它仍然顯示在同一行。

有沒有辦法在同一行顯示多個操作錯誤?

+0

將返回標籤添加到JSP。 –

+0

@RomanC請你澄清你的意思嗎?我不知道「將返回標籤添加回JSP」是什麼意思。 – Nzall

+0

這只是線索爲什麼你的代碼不工作,你不知道代碼是如何寫入響應的,所以你必須檢查模板。 –

回答

0

經過我的項目源代碼搜索後,我發現了一個Freemarker模板,其中<#list>被用來迭代元素。我編輯此模板以包含<#sep><br /></#sep>,因此它使用<br />作爲分隔符。不幸的是,這不適用於我的項目使用的Freemarker版本(我們使用2.3.19,在2.3.23中添加了list builtins)。

作爲替代,我最終使用<#if item_has_next><br /></#if>,這在我的版本中可用。這已經解決了這個問題。感謝羅馬C建議檢查模板。