2011-06-29 43 views
0

我有一個使用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> 

感謝任何幫助或建議!

回答

5

我認爲你是在正確的軌道上。還有我所知道的獲得一個彙總列表中的所有對象的錯誤信息和現場的錯誤消息沒有直接的方法,但你可以這樣做:

#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 

不那麼幹淨,但它的作品。

+0

這工作完美!非常感謝你的幫助! – KMitchell

+0

甜!感謝Rob:)... –