Can I use Ajax with Spring MVC with out <mvc:annotation-driven/>
? I yes the how would I map my controller in app-servlet.xml ? I have seen many examples with annotation but nothing without annotation :(
thanks.
here it is a good example. http://loianegroner.com/2010/02/spring-mvc-and-ajax-with-json/
in addition to the above...
public ModelAndView getColumnsJson(HttpServletRequest request,
HttpServletResponse response) throws Exception {
String nvalue = request.getParameter("value");
//do something with nvalue and send back the result....
Map<String,Object> modelMap = new HashMap<String,Object>(2);
modelMap.put("result", "DONE");
return new ModelAndView("jsonView", modelMap);
}
Actually am also a beginner to spring mvc.. currently pursuing bachelors degree.. I was also using spring mvc without annotation. suddenly i realize that i would need ajax for my project. I searched a lot but could not find satisfactorily result. The answer might look weird but it worked for me... You can use ajax call in interceptors. Interceptors have reference to both request and response object. You can extend HandlerInterceptorAdapter class.. Below is the code that i tried.. Hope so it will help you...
package mypack.Interceptors;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
public class AjaxInterceptor extends HandlerInterceptorAdapter {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)throws ServletException, IOException{
String name=null;
name = "Hello "+request.getParameter("user");
if(request.getParameter("user").toString().equals("")){
name="Hello User";
}
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(name);
System.out.println("The ajax method is working");
return false;}