Spring Remote Check MVC jQuery

I use Spring MVC on the server side, but on one of the pages I decided to create an AJAX check using jQuery, and not the default Spring. Everything works fine, except when I have to do a remote check to see if the title already exists. For javascript, I have the following:

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."
        }
    }

});

Then I use Spring -Json to do this check and give an answer:

@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);
}

However, this does not work, and the "title" field is never checked. I think the reason is because I am returning the answer this way:

{result:"TRUE"}

when in fact the answer should be:

{"TRUE"}

I don't know how to return a single response like this using a ModelAndView answer.

Another thing that doesn't work is the configured message for checking "remote":

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

, . , , Spring jQuery . , jQuery valdations Spring -Json. .

+3
2

.

@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()));
}

. .

, :)

Update:

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();
}
+3

MVC jQuery Spring, . json, boolean.

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")
        }
    }
});

Spring :

@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);
}
+1

Source: https://habr.com/ru/post/1737351/


All Articles