如何在 Struts2 中的多行显示多个操作错误?
How can I display multiple action errors on multiple lines in 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"> </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">
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>
如您所见,第二条消息与第一条消息在同一行,看起来有些草率。我尝试使用 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 />");
}
};
}
但是,这并不能解决问题:它仍然显示在同一行上。
有没有办法在同一行显示多个动作错误?
在对我的项目源代码进行一些搜索后,我找到了一个 Freemarker 模板,其中 <#list>
用于迭代元素。我编辑了此模板以包含 <#sep><br /></#sep>
,因此它使用 <br />
作为分隔符。不幸的是,这不适用于我的项目使用的 Freemarker 版本(我们使用 2.3.19,列表内置函数是在 2.3.23 中添加的)。
作为替代方案,我最终使用了 <#if item_has_next><br /></#if>
,它在我的版本中可用。这已经解决了这个问题。感谢 Roman C 建议检查模板。
我有一个 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"> </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">
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>
如您所见,第二条消息与第一条消息在同一行,看起来有些草率。我尝试使用 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 />");
}
};
}
但是,这并不能解决问题:它仍然显示在同一行上。
有没有办法在同一行显示多个动作错误?
在对我的项目源代码进行一些搜索后,我找到了一个 Freemarker 模板,其中 <#list>
用于迭代元素。我编辑了此模板以包含 <#sep><br /></#sep>
,因此它使用 <br />
作为分隔符。不幸的是,这不适用于我的项目使用的 Freemarker 版本(我们使用 2.3.19,列表内置函数是在 2.3.23 中添加的)。
作为替代方案,我最终使用了 <#if item_has_next><br /></#if>
,它在我的版本中可用。这已经解决了这个问题。感谢 Roman C 建议检查模板。