2012-02-28 33 views
0

我有一個春天的應用程序,我驗證定製春天驗證器類的所有驗證。如何設置焦點元素彈簧基礎的應用

我的形式有近15 fields.If沒有在最後一欄的錯誤,如何將焦點設置到該元素。

春天在什麼標籤支持。任何技術想法?目前,我在字段本身附近顯示我的錯誤消息。

回答

2

它可以使用spring:bind標記以確定一個字段或整個模型對象是否具有錯誤,並且對象的後處理(提交)的整體狀態。

<spring:bind path="user.username"> 
    <input id="inputId" type="text" name="${status.expression}" value="${status.value}"/> 
    <c:if test="${status.error}"> 
     <c:forEach items="${status.errorMessages}" var"error"> 
      <span class="field-error">${error}</span> 
     </c:forEach> 
     <script type="text/javascript"> 
      document.getElementById('inputId').focus(); 
     </script> 
    </c:if> 
</spring:bind> 

注:

  • status變量由spring:bind標籤露出。
  • JavaScript部分將集中的元件當特定字段 具有驗證錯誤。
1

您可以將autofocus設置爲所有有錯誤的字段,但瀏覽器只會自動對焦(並滾動)到第一個。

<spring:bind path="someFieldName"> 
    <input type="text" name="someFieldName" value="${status.value}" ${status.error?'autofocus="autofocus"':''} /> 
</spring:bind> 

這裏不能使用spring的<form:input />,因爲它不適用於條件屬性。

0

我用這個:

<spring:bind path="*"> 
     <c:if test="${status.error}"> 
      <script type="text/javascript"> 
       window.onload = function(){ 
        document.getElementById('${status.errors.fieldErrors[0].field}').focus(); 
       }; 
      </script> 
     </c:if> 
    </spring:bind> 

如果有錯誤,將找到的第一個fieldError和其ID,並設置焦點元素上

忘了說,美女是你不需要把這個代碼放在每個輸入的旁邊。一時間爲每一個JSP足夠

相關問題