11

I have an ajax on a jsp page which calls a spring controller through URL /check .

$.ajax({
    type : "GET",
    url : "${pageContext.request.contextPath}/check",
    data : {
    "id" : ${articleCount}
    },
    success: function(data){
    //response from controller
    }
});

Now, the controller looks like,

@RequestMapping("/check")
public String check(@RequestParam Integer id, HttpServletRequest request,
        HttpServletResponse response, Model model) {
    boolean a = getSomeResult();
    if (a == true) {
        model.addAttribute("alreadySaved", true);
        return view;
    } else
        model.addAttribute("alreadySaved", false);

    return view;
}

I sent data using model and tried to access it in success: function(data) as "${alreadySaved}"but it shows blank.

Is there any way I can receive that true/false data on the view page?

4 Answers 4

9

You have to add the @ResponseBody annotation for spring ajax calls example

@RequestMapping("/check")     
@ResponseBody
public String check(@RequestParam Integer id, HttpServletRequest request, HttpServletResponse response, Model model) {
    boolean a = getSomeResult();
    if (a == true) {
        model.addAttribute("alreadySaved", true);
        return view;
    } else {
        model.addAttribute("alreadySaved", false);
        return view;
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Okay. How can I get that response on the view side?
that means view is empty.please return the model object as response.
No, the view is not empty. The view is injected by spring from springmvc servlet. It is a string that resolves to load a JSP.
I had some misconception. I got it right now. Thanks :)
4

use @ResponseBody

Spring will bind the return value to outgoing HTTP response body when you add @ResponseBody annotation.

@ResponseBody
public String check(@RequestParam Integer id, HttpServletRequest request, HttpServletResponse response, Model model) {
    boolean a = getSomeResult();
    if (a == true) {
        return "already saved";
    } 
    return "error exist";
}

Spring will use HTTP Message converters to convert the return value to HTTP response body [serialize the object to response body], based on Content-Type used in request HTTP header.

for more info:

http://websystique.com/springmvc/spring-mvc-requestbody-responsebody-example/

Comments

1

Controller part

You have to add the @ResponseBody annotation for spring ajax calls example

View Part

$.ajax({
    type : "GET",
    url : "${pageContext.request.contextPath}/check",
    data : {
        "id" : ${articleCount}
    },
    success: function(data){
        $('#input_field').val(data);
    }
});

Comments

1

When you are trying to return the values from the ajax request , you should use the @ResponseBody annotation . As your method return type is String make sure you return the string value and not your view to the jsp.

Since it will render the jsp again the response

@ResponseBody
public String check(@RequestParam Integer id, HttpServletRequest request, HttpServletResponse response, Model model) {
    boolean a = getSomeResult();
    if (a == true) {
        return "already saved";
    } 
    return "error exist";
}

And in your jsp,

success: function(data){
    alert(data);
}

2 Comments

But I need to return view from that method because without that, spring will not be able to load the jsp.
@najus No the concept of ajax is to refresh the part of the page so it is enough to the send the return value alone . As you are loading the same page

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.