title: Ajax call implementation with Controller interface in Spring
body: <p>Currently I am on a legacy project of Spring mvc and annotations are not working on it.</p>
  
  <p>I have to create an controller-action which will respond to an ajax call with JSON response.</p>
  
  <p>I have created my controller by implementing <a href="http://docs.spring.io/spring/docs/1.2.9/api/org/springframework/web/servlet/mvc/Controller.html" rel="nofollow">Controller</a> interface.</p>
  
  <p>I am able to implement a jsp page with this but unable to tweek it for ajax call.</p>
  
  <p>Taking help from <a href="http://www.javablog.fr/javaspringjson-generate-json-withwithout-viewresolver-jsonview-with-json-lib-2-3-jdk15.html" rel="nofollow">link1</a>  <a href="http://loianegroner.com/2010/02/spring-mvc-and-ajax-with-json/" rel="nofollow">link2</a>  <a href="http://forum.spring.io/forum/spring-projects/web/76361-spring-modelandview-containing-json" rel="nofollow">link3</a></p>
  
  <p>Xml configuration in action-servlet.xml</p>
  
  <pre><code>&lt;bean id="jsonResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver"&gt;
      &lt;property name="order"&gt;&lt;value&gt;1&lt;/value&gt;&lt;/property&gt;
  &lt;/bean&gt;
  &lt;bean name="jsonView" class="org.springframework.web.servlet.view.json.JsonView"&gt;
      &lt;property name="contentType"&gt;
          &lt;value&gt;text/html&lt;/value&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id="myOwnController" class="com.myapp.webapp.action.MyOwnController" /&gt;
  
  
  &lt;bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"&gt;
      &lt;property name="interceptors"&gt;
          &lt;list&gt;
              &lt;!-- list of inteceptor urls --&gt;
          &lt;/list&gt;
      &lt;/property&gt;
          &lt;property name="mappings"&gt;
          &lt;props&gt;
                      &lt;prop key="/mytest"&gt;myOwnController&lt;/prop&gt;
              &lt;!-- mapping for other controller actions --&gt;
          &lt;/props&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>Controller class</p>
  
  <pre><code>package com.myapp.webapp.action
  public class myOwnController implements org.springframework.web.servlet.mvc.Controller
  {   
      @Override
      public ModelAndView handleRequest(HttpServletRequest request,HttpServletResponse response) throws Exception
      {
          Map&lt;String, Object&gt; model = new HashMap&lt;String, Object&gt;();
          model.put("success", "true");
          return new ModelAndView("jsonView", model);
      }
  }
  </code></pre>
  
  <p>ajax call js method (in Prototype.js)</p>
  
  <pre><code>function myAjaxMethod(){
      new Ajax.Request('${pageContext.request.contextPath}/jsonView', {
            method:'get',
            onSuccess: function(transport) {
              var response = transport.responseText || "no response text";
              alert("Success! \n\n" + response);
            },
            onFailure: function() { alert('Something went wrong...'); }
          });
  }
  </code></pre>
  
  <p>Also I am unable to understand that how the url in ajax call will get to know the this is the controller that should be called.Do I am missing any mapping?</p>
  
  <p>Please help.</p>