2

It's like user click a link and in the Spring controller class a method will respond to the request with a JSON object and also a view name (meaning, it should return but not only a JSON object but also a HTTP view which hold that JSON object, so @ResponseBody may not enough)? Do we have to split it up into two methods (one for view and the other for JSON object)? Any ideas will be appreciated.

Normally we have

@RequestMapping(value="/someValue")
public @ResponseBody someMethod1(@RequestParam String param){
    .....
    return someJSONObject
}

To handle JSON object, and

@RequestMapping(value="/someValue")
public String someMethod2(@RequestParam String param){
    .....
    return someViewInString;
}

To return the view.

How can we combine them together?

1
  • 2
    Can't you save the json object as an attribute and reference in your view using ${name_of_json_attr}? (I guess this may only work if your view is jsp) Commented Apr 11, 2013 at 19:15

1 Answer 1

2

You should return the view with a placeholder for the JSON.

In the controller code, create the JSON programatically, convert it to String format and then put it in the model (let's call it json_string)

In the view there should be a placeholder for the JSON string, something like:

<!-- other view stuff -->
var v = ${json_string};
<!-- more view stuff -->
Sign up to request clarification or add additional context in comments.

1 Comment

That's good work around. Thank you. So then we still use JSON but it's like a Strin in JSON format from controller?

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.