【问题标题】:Is it possible to have freemarker's <@spring.showErrors to display errors in a div instead of span是否可以让 freemarker 的 <@spring.showErrors 在 div 而不是 span 中显示错误
【发布时间】:2011-09-10 05:35:21
【问题描述】:

代码:

<@spring.formInput 'myForm.spouseEmail' 'id="spouseEmail" class="text"'/>
<@spring.showErrors ', ' 'error'/>

输出:

<span class="error">not a well-formed email address</span>

我想要什么:

<div class="error">not a well-formed email address</div>

【问题讨论】:

    标签: validation spring-mvc freemarker


    【解决方案1】:

    @Mike:您似乎很难理解宏的本质。它们是已经编写好的 freemarker 脚本,让您的生活更轻松。你可以随时write a customed one

    有些人认为很明显,但我自己发现要知道如何查看spring-freemarker宏源代码并不容易。您可以导航到 Eclipse 的“引用库”中的包 org/springframework/spring-webmvc-3.0.5.jar/org/springframework/web/servlet/view/freemarker/spring.ftl

    这是从“spring.ftl”得到的宏“showErrors”:

    <#macro showErrors separator classOrStyle="">
        <#list status.errorMessages as error>
        <#if classOrStyle == "">
            <b>${error}</b>
        <#else>
            <#if classOrStyle?index_of(":") == -1><#assign attr="class"><#else><#assign attr="style"></#if>
            <span ${attr}="${classOrStyle}">${error}</span>
        </#if>
        <#if error_has_next>${separator}</#if>
        </#list>
    </#macro>
    

    要实现你的目标,很简单:只需编写一个与上面代码完全相同的自定义宏,将span替换为div

    【讨论】:

      【解决方案2】:

      不,但您可以轻松编写自己的宏来做任何您想做的事情。从spring.showErrors 本身获得灵感。

      【讨论】:

        猜你喜欢
        • 2018-01-28
        • 2012-07-30
        • 2011-08-02
        • 2021-04-18
        • 2019-10-23
        • 2018-11-15
        • 1970-01-01
        • 2018-07-08
        • 1970-01-01
        相关资源
        最近更新 更多