2

I'm Using Spring Boot and there is a point that I don't understand. Suppose I have a model, created in a Controller's method, annotated with @ResponseBody annotation. I must put, inside this model, 2 variables, a String and an int, that I have to use to set 2 global variables in a js file; this setting is done in case of ajax function success.

So, for semplicity, let's assume that my global variables are global1 and global2; and that the variables to put inside model are String a and int b; in this scenario, what must happend is:

$.ajax({
        ...<url, method and other actions here>...
        success: function(){
                 global1 = String a;
                 global2 = int b;
        }

My doubt are 3:

  1. How I may put the variables String a and int b inside the model? Should I use model.addObject() or it's better create a Modelmap object, fill this one with a and b and then put him inside the model? Which difference there is between this 2 approaches?

  2. In the ajax function, when I have success: function(), which parameters should I give in input to function()?

  3. Inside the success function, how can I use the model data to set the global variables? What syntax may I use to perform this? In other words, when I have my model, how can I access its variables and use to perform global1 = String a and global2 = int b?

2 Answers 2

1

You can use Model object and use addAttribute method for every object you need to have in ajax success function. In every Spring method you can add Model object, for example:

@RequestMapping(value="value", method = RequestMethod.POST)
    public @ResponseBody String yourMethod(@RequestParam(value="yourParam") String yourParam,
                       Model model){
    String a = "Your String";
    int b = 456;

    model.addAttribute("myString", a);
    model.addAttribute("myInt", b);

    return "";
}

The success function must be declared with one parameter, that is the response. Inside this parameter you will find the parameters you added in model object (myString and myInt):

success: function(response){   
            // response.myString will be "Your String"
            // response.myInt will be 456
        }
Sign up to request clarification or add additional context in comments.

1 Comment

this works perfectly. Thanks a lot, also for editing!
1

General : The clean way is to use $("form").serialize(); in data of AJAX it makes param1=param1value&param2=param2value and take back using @ModelAttribute className className , however in edit you may have many problems such checkBox or date or radio buttons automatic value detecting , so I recommend you to use spring form back-end and pass back end object and drop Ajax if you can! Also using @ResponseBody you should make json object in client side and get in server such @RequestBody className className , and I think json approach taking too time in client side for development , spring in client side specially in Ajax has not any ability , so for Ajax I prefer to use thymleaf fragment load instead of jquery AJAX , in this method you can even use Thymleaf object back command topography . But Your Questions 1-if you sending json use @RequestBody and get model such:

@RequestMapping(value = "/", method = RequestMethod.POST)
public ResponseEntity<Car> update(@RequestBody Car car) {
    ...
}

@ResponseBody just supporting jackson automatic backing object so you should just send back object , however you can send back status of ajax method using Response Entity it is wrapper that the data and status of html are in int such:

return new ResponseEntity<Car>(car, HttpStatus.OK);

or

 return new ResponseEntity("done", HttpStatus.OK);

2-if using jquery done function will got 200 201 and other success values and done will acitvated you can get data by

.done(function(data) {
  alert( "$.get succeeded" ); });

3- In general come back data is list of json you should foreach and get each item as json object and open by point such item.prop

Comments

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.