2010-03-17 147 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