是否有可能让freemarker的<@ spring.showErrors在div而不是span中显示错误

Mik*_*ike 2 validation freemarker spring-mvc

码:

<@spring.formInput 'myForm.spouseEmail' 'id="spouseEmail" class="text"'/>
<@spring.showErrors ', ' 'error'/>
Run Code Online (Sandbox Code Playgroud)

输出:

<span class="error">not a well-formed email address</span>
Run Code Online (Sandbox Code Playgroud)

我想要的是:

<div class="error">not a well-formed email address</div>
Run Code Online (Sandbox Code Playgroud)

Hoà*_*ong 6

@Mike:你似乎很难理解宏的本质.它们是已经编写的freemarker脚本,可以让您的生活更轻松.你总是可以写一个定制的.

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

这是从"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>
Run Code Online (Sandbox Code Playgroud)

为了实现您的目标,它非常简单:只需编写一个与上面的代码完全相同的自定义宏,替换spandiv