如何在Struts2中的多行显示多个操作错误?

时间:2017-06-06 09:47:30

标签: jsp struts2

我有一个Struts2页面,我需要显示2个动作警告(基本上是设置了标志的错误):每次页面加载时为1,另一个取决于页面的属性。

我用于添加操作消息的代码:

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>

这会显示以下消息:

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

正如您所看到的,第二条消息与第一条消息位于同一行,看起来有点草率。我尝试使用Decorator方法修复此问题:

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 />");
        }
    };      
}

但是,这并没有解决问题:它仍然显示在同一行。

有没有办法在同一行显示多个动作错误?

1 个答案:

答案 0 :(得分:0)

在搜索了我的项目源代码后,我发现了一个Freemarker模板,其中<#list>用于迭代元素。我修改了此模板以包含<#sep><br /></#sep>,因此它使用<br />作为分隔符。不幸的是,这对我的项目使用的Freemarker版本不起作用(我们使用2.3.19,在2.3.23中添加了内置列表)。

作为替代方案,我最终使用<#if item_has_next><br /></#if>,我的版本中提供了private KieServices kieServices = KieServices.Factory.get(); private KieFileSystem kieFileSystem = kieServices.newKieFileSystem(); public reinitialize() { kieServices = KieServices.Factory.get(); kieFileSystem = kieServices.newKieFileSystem(); } 。这解决了这个问题。感谢Roman C建议检查模板。