【问题标题】:Need to Combine Spring Binding Errors and Custom Exceptions into Same DIV需要将 Spring 绑定错误和自定义异常合并到同一个 DIV 中
【发布时间】:2015-12-28 18:54:18
【问题描述】:

我需要在我页面上的same DIV 中显示 SpringMVC 的表单错误,以及我存储在请求中的任何自定义“异常”消息。

现在它们位于不同的 DIV 中:

<!-- Validation Errors -->
<spring:hasBindErrors htmlEscape="true" name="model">                
    <c:if test="${errors.errorCount gt 0}">                 
        <div class="errors_div clsErrorTblBorder">
            <c:forEach items="${errors.allErrors}" var="error">
                <div class="errors clsWhiteBack blue">                      
                    <myForm:message messageLinkClass="errorLink"
                                error="${error}" />
                </div>
            </c:forEach>                
        </div>
    </c:if>   
</spring:hasBindErrors>

<!-- Exceptions -->
<c:if test="${fn:length(requestScope.exceptions) > 0}">
    <div class="errors_div clsMsgTblBorder">
        <c:forEach items="${requestScope.exceptions}" var="exception">
            <div class="messages clsWhiteBack">
                ${exception}
            </div>
        </c:forEach>
    </div>
</c:if>

错误 DIV 的共享条件应该是,

${fn:length(requestScope.exceptions) || errors.errorCount gt 0}

但是不可能测试这个条件,因为:

  • 你没有得到errors var。直到你做&lt;spring:hasBindErrors&gt;

  • &lt;spring:hasBindErrors&gt; 中的任何内容仅适用于您拥有
    绑定错误。如果您不这样做(例如,如果您只有一个自定义
    异常消息)它的逻辑不会执行,所以你不能移动
    自定义“异常”检查。

【问题讨论】:

    标签: spring spring-mvc


    【解决方案1】:

    我从这个帖子中得到了一些线索:How to access Spring 3 MVC validator results in JSP without using form taglib,这是工作结果:

        <c:if test="${fn:length(requestScope.exceptions) > 0 || 
    requestScope['org.springframework.validation.BindingResult.model'].hasFieldErrors()}">
    

    此条件的第二部分甚至在标签 Spring:hasBindErrors 之前检查绑定错误。

    【讨论】:

      猜你喜欢
      • 2021-01-19
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多