2010-03-17 141 views
3

我在服務器端使用Spring MVC,但在其中一個頁面中,我決定使用jQuery而不是默認的Spring驗證創建AJAX驗證。 一切工作很好,除非我必須做一個遠程驗證,以檢查數據庫中是否存在「標題」。 對於JavaScript的我有以下幾點:Spring MVC jQuery遠程驗證

var validator = $("form").validate({ 
    rules: { 
     title: { 
      minlength: 6, 
      required: true, 
      remote: { 
       url: location.href.substring(0,location.href.lastIndexOf('/'))+"/checkLocalArticleTitle.do", 
       type: "GET" 
      } 
     }, 
     html: { 
      minlength: 50, 
      required: true 
     } 
    }, 
    messages: { 
     title: { 
      required: "A title is required.", 
      remote: "This title already exists." 
     } 
    } 

}); 

然後,我用Spring的JSON來使此驗證,並給予迴應:

@RequestMapping("/checkLocalArticleTitle.do") 
public ModelAndView checkLocalArticleTitle(HttpServletRequest request, 
    HttpServletResponse response) throws ServletException, IOException { 
    Map model = new HashMap(); 
    String result = "FALSE"; 
    try{ 
     String title = request.getParameter("title"); 
     if(!EJBRequester.articleExists(title)){ 
      result = "TRUE"; 
     } 
    }catch(Exception e){ 
     System.err.println("Exception: " + e.getMessage()); 
    } 
    model.put("result",result); 
    return new ModelAndView("jsonView", model); 
} 

但是,這並不工作,並現場「稱號「從未被驗證。 我想這樣做的原因是我的方式返回一個答案:

{result:"TRUE"} 

其實時,答案應該是:

{"TRUE"} 

我不知道怎麼回事像這樣使用ModelAndView答案的單個響應。

不工作的另一件事情是「遠程」驗證定製的消息:

messages: { 
     title: { 
      required: "A title is required.", 
      remote: "This title already exists." 
     } 
    }, 

所需的信息工作,而不是遠程消息。 我環顧四周,但我沒有看到許多人同時使用Spring和jQuery。至少,不要混用jQuery遠程值和Spring-Json。 我很感謝這裏的一些幫助。

+0

沒有人知道答案嗎? – raulsan

回答

3

我遇到了同樣的問題。

,現在我做

@RequestMapping(value = "/brand/check/exists", method = RequestMethod.GET) 
public void isBrandNameExists(HttpServletResponse response, 
     @RequestParam(value = "name", required = false) String name) 
     throws IOException { 
    response.getWriter().write(
      String.valueOf(Brand.findBrandsByName(name).getResultList() 
        .isEmpty())); 
} 

可能不是一個很好的解決方案。但無論如何,這是成功的。

如果有更好的辦法,請讓我知道:)

更新:

在Spring 3.0,我們可以使用到@ResponseBody做到這一點

@RequestMapping(value = "/brand/exists/name", method = RequestMethod.GET) 
@ResponseBody 
public boolean isBrandNameExists(HttpServletResponse response, 
     @RequestParam String name) throws IOException { 
    return Brand.findBrandsByName(name).getResultList().isEmpty(); 
} 
1

這裏是另一個Spring MVC jQuery遠程驗證示例,用於檢查用戶唯一性。 參數作爲json和服務器返回布爾值傳遞。

JS:

$('#user-form').validate({ // initialize the plugin 
    rules: { 
     username: { 
      required: true, 
      remote: function() { 
       var r = { 
        url: 'service/validateUsernameUnique', 
        type: "POST", 
        contentType: "application/json; charset=utf-8", 
        dataType: "json", 
        data: '{"username": "' + $("#username").val() + '"}' 
        } 
       return r; 
       } 
     }, 
    }, 
    messages: { 
     username: { 
      remote: jQuery.format("User {0} already exists") 
     } 
    } 
}); 

春控制器:

@RequestMapping(value="/AdminUserService/validateUsernameUnique", method = RequestMethod.POST) 
public @ResponseBody boolean validateUsernameUnique(@RequestBody Object username) { 

    @SuppressWarnings("unchecked") 
    String name = ((LinkedHashMap<String, String>)username).get("username"); 
    return userService.validateUsernameUnique(name); 
} 
+0

我想象很多事情在4年內發生了變化。我不再使用Spring了,但是感謝你的回覆@ tetchen9。 – raulsan