Reputation: 3652
I'm using Spring MVC and jquery autocomplete, my spring controller looks like this
@RequestMapping(value ="/searchit.htm", method=RequestMethod.GET)
@ResponseBody
protected String testIt(Model model){
gsonutil = new GsonUtil<YBusiness>();
String result = MyManager.search();
model.addAttribute("result",result);
return "jsonNames";
}
My "jsonNames" bean is configured like this.
<bean name="jsonNames"
class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
Everything is working fine, my autocomplete is hitting the controller but doesn't returning result in response, I'm very much sure that there is something wrong in my spring MVC controller method because if I pass the source array(with some pre-filled values) it works.
Upvotes: 2
Views: 863
Reputation: 3198
Few things wrong / insufficient with the code snippet you provided:
The @ResponseBody annotations tells Spring MVC that the return value of the method should be populated as the response of the HTTP request. Given the above controller method, if you hit http://yourserver/context/searchit.htm you probably see an html with "jsonNames" written in it. So remove the annotation. (Hint before even hooking this url with your autosuggest, use your browser to check if the url is responding as expected)
If you plan to use the MappingJacksonJsonView, you should define a BeanNameViewResolver bean in your spring context bean container. This view resolver tells Spring MVC, that the String returned from the controller method should be used to match against a bean name defined in the context, in your case "jsonNames"
After you've got the above working i.e. Spring MVC beans are configured properly and your controller is returning JSON, you will have to check if the format returned is compatible with your front end autosuggest library, if not you have some more work ahead of you in the controller.
Refer the documentation, for the most part Spring MVC is pretty simple http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html Hope this helps.
Upvotes: 1