【问题标题】:Show all Spring form errors on Velocity template在 Velocity 模板上显示所有 Spring 表单错误
【发布时间】:2011-06-30 07:16:25
【问题描述】:

我有一个使用 Spring MVC 和 Velocity 的应用程序。在我的一个表单上,我想在页面顶部显示与表单相关的所有错误。我已经想出了如何显示与一个特定字段相关的错误(使用#springShowErrors 宏),但我真的希望在表单顶部有一大块错误,而不是在每个单独项目旁边列出错误.

我已经做了很多谷歌搜索,有些人提出了类似的建议

#if ($status && $status.errors.hasErrors())
#foreach( $error in $status.errorMessages )
<p>$error</p>
#end
#end

...但是当将我的命令对象附加到表单的初始#springBind 宏下方时,这不会给我任何输出。将#springShowErrors 放在每个字段的#springFormInput 宏之后可以正常工作,所以我知道我的验证器正在运行并产生错误。

有什么想法吗?我错过了什么非常愚蠢的事情吗?

这是完整的表格,我在第一次 #springBind 之后尝试不工作

<form name="standardForm" id="standardForm" method="post" action="#springUrl("/requestAccess")">
            #springBind("accessRequest")
#if ($status && $status.errors.hasErrors())
#foreach( $error in $status.errorMessages )
<p>$error</p>
#end
#end

            <fieldset>

                  <label for="name">Name</label>

                  #springFormInput("accessRequest.name" " ")


                  <label for="company">Company</label> 

                  #springFormInput("accessRequest.company" " ")

                  <label for="title">Title</label> 
                  #springFormInput("accessRequest.title" " ")

                  <label for="email">Email</label> 

                  #springFormInput("accessRequest.email" " ")

                  <button  type="submit" value="send">Send</button>

             </fieldset>
         </form>

感谢您的帮助或建议!

【问题讨论】:

    标签: spring validation forms spring-mvc velocity


    【解决方案1】:

    我认为你在正确的轨道上。我知道没有直接的方法可以在一个聚合列表中获取所有对象错误消息和字段错误消息,但是您可以这样做:

    #springBind("bindName")
    #if($status.errors.hasErrors())
        ## Global error messages
        #foreach($e in $status.errorMessages)
            <p>${e}</p>
        #end
        ## Field error messages
        #foreach($f in $status.errors.fieldErrors)
            #springBind("bindName.${f.field}")
            #foreach($e in $status.errorMessages)
                <p>${e}</p>
            #end
        #end
    #end
    

    不是很干净,但它可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-20
      • 1970-01-01
      • 1970-01-01
      • 2017-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多