This forum is now a read-only archive. All commenting, posting, registration services have been turned off. Those needing community support and/or wanting to ask questions should refer to the Tag/Forum map, and to http://spring.io/questions for a curated list of stackoverflow tags that Pivotal engineers, and the community, monitor.

XT Ajax Framwork

Nov 15th, 2010, 09:47 AM

Hello,

Im working on a small Webapplication with Spring, Spring MVC, Spring Security and Hibernate. Today I wanted to integrate Ajax, for example to check asynchrounly if a username is already in use while the user is filling out a formular to register.

I began to write the ajax stuff, but I found it a little bit difficult to integrate it nicely in my existing project. I was reading about it and I found out that I should use tiles when I use ajax and spring mvc. Well, until now I didnt use tiles and I dont want to change my whole configuration.

As I went on researching, I read about the XT Ajax Framework. It sounds like a really nice solution.
But I was wondering why it was so hard to find out about the XT Ajax Framework. Is it maybe old and not recommended to use? Are there new solutions which are way better than this? I would like to hear your opinion!

another question: since really nobody is talking about this framework, maybe you could help me with my ajax problem.

A User wants to register. When he types in the username, an ajax request is send. The Request is mapped to a controller, which looks up the necessary information (is the username in use) and returns a value (string, boolean, whatever.) this value is returned as ....

at the moment I return it as a ModelAndView value. But then my page looks horrible.

the red marked area shows where the answer is shown. at this point the new modelandview is shown. in this moment it is the same page as I use for registering, so the page is shown twice.
Should I use another new jsp, which only contains the word? or what could I return??

Comment

If you are using Spring 3 configured with <mvc:annotation-driven/>, you could download Jackson JSON Processor jars and add them to your WEB-INF/lib. Then annotate your controller method with @ResponseBody. The model you return would be printed to the output as a json string which you can use with prototype or jquery to put it on the page. It's all throughly explained in this excellent article by Keith Donald :

Comment

I downloaded Jackson Json Processor jars (jackson src 1.6.2), which are a lot of jars and I decided to put in my web-inf/lib folder:

json-org.jar
json_simple-1.0.2.jar
jsontools-core-1.5.jar

are these the ones I need?

then I changed my controller, I added @ResponseBody as you said, but I still return a ModelAndView. I understood that I should do that when I read your answer. I give back the "register.jsp", which is the jsp where the request comes from and where the string shall be displayed, is that right?

I downloaded the spring example for mvc and ajax, but I dont really understand how to print out the string on my jsp.

. The model you return would be printed to the output as a json string which you can use with prototype or jquery to put it on the page.

I have never used prototype or jquery before. I dont understand how I have to change my jsp. Can I keep my ajax methods or do I have to use those from the example from spring?

here is my code: the green part is the code from the spring example, the red part is my old code. can I use my old code?

how can I print out the answer? I would be very(!) grateful, if you could give me just a line, a snippet, to show me how to print out, so that I can implement it and learn how to do it! Thank you for your help!

Comment

You shouldn't return a modelandview. If you mark the method with @ResponseBody, what you return is directly written to the response, so there is no view or model involved. So you should return directly the object you want to retrieve with ajax on the javascript side, in your case a Boolean object.

On client side, of course you can use normal javascript way to make an ajax call, but using jquery or prototype is really much easier (nobody that I know use normal javascript for ajax anymore).