title: method = RequestMethod.POST and model.addAttribute
body: <p>I am relatively new to java world. 
  I am using Spring MVC (using 4.0.2) Maven...</p>
  
  <p>1) I have jsp file which sends json data [A] to controller (POST)</p>
  
  <p>2) then, Controller(method) calculates another value based on the data from jsp </p>
  
  <p>3) I need to return the another value (the result of calculation) [B] to jsp by model.addAtrribute. </p>
  
  <p>but It doesn't return by model. Here is my code. </p>
  
  <h2>    In JSP file (original trial)</h2>
  
  <pre><code> $.ajax({
                  url:'/mypage.html',
                      data: 'left=' + left +'&amp;right='+ right,  ----[A1]
                      type:"POST",
                      success: function(json){
                      //alert('succeed!');
                      }   
                    });
      .........
      .........
  &lt;p style="color:red; font-size: 12pt"&gt;Distance: ${distance}&lt;/p&gt;  --- [B1]
  </code></pre>
  
  <p>so now controller:</p>
  
  <h2>    In controller (original trial)</h2>
  
  <pre><code>@RequestMapping(value="/mypage", method = RequestMethod.POST)
  public @ResponseBody String update(@RequestBody String  response, Model model) {
  
      String left = … response parsing…...;   ---[A2] //works fine  
      String right = … response parsing...; -----[A2] //works fine
  
      String distance = getDistance(left, right);   // [B2] is calculated for distance based on [A2], works fine
  
      model.addAttribute("distance", distance);   --- [B2]
  
      Return "mypage";  // [B2] should be delivered to [B1] (in jsp) and display ${distance} value
  }
  </code></pre>
  
  <p>I have simplified my code because response, parsing parts are working fine. </p>
  
  <p>I think it's something wrong about <strong>(my suspicion part)</strong>:</p>
  
  <p>===> 'method=RequestMethod.post' and 'model.addAttribute' </p>
  
  <p>or </p>
  
  <p>===> return "mypage" and [B1] part in jsp</p>
  
  <p>If anyone can point out what I did wrong, really appreciated. </p>
  
  <p>====================== Solved !!!! =====================</p>
  
  <p>I add my solution here, just in case, 
  if anyone need to use this kind technique as well....</p>
  
  <p>Thanks to both @willysama and @misko321 for good answers &amp; pointing out what  can and can't and providing sample code.</p>
  
  <p>[Solution]  </p>
  
  <h2>    In controller (final solution)</h2>
  
  <pre><code>    @RequestMapping(value="/mypage", method = RequestMethod.POST)
      public @ResponseBody String update(@RequestBody String  response) {
          String left = … response parsing…...;      
          String right = … response parsing...;  
          String distance = getDistance(left, right);   //e.g. distance = 100
  
          return distance;    // simply return String value of distance
                              // don't need to have model.addAttribute
      }
  </code></pre>
  
  <h2>In JSP file (final solution)</h2>
  
  <pre><code> $.ajax({
              url:'/mypage.html',
              data: 'left=' + left +'&amp;right='+ right, 
              type:"POST",
              success: function(response){   // here should be 'response'
               //here gets from controller
              alert('what I get?response='+response);  //what I get? response=100
              $('#distance_result').text(response);  // to [1] for refreshing distance value 
  
              }   
          });
      ......
      ......
      &lt;p style="color:red; font-size: 12pt"&gt;Distance: &lt;span id="distance_result"&gt;${distance}&lt;/span&gt;&lt;/p&gt;   // [1] refresh the distance value which returned from controller
  </code></pre>
  
  <p>(simple comments for the solution)
  I don't need to deliver the distance value through model.addAttribute.</p>
  
  <p>The update method in controller when I post has String return value (public @ResponseBody <strong>String</strong> update....), 
  so I just need to return String (which is 'distance' here) to jsp</p>
  
  <p>then in jsp , 
  'ajax success:' part will receive this return 'distance' from controller in success which both @willysama and @misko321 pointed out. like below: </p>
  
  <pre><code>success: function(response){   
                  $('#distance_result').text(response);
  </code></pre>
  
  <p>so in the end, the place to refresh the distance value..  </p>
  
  <pre><code>&lt;p style="color:red; font-size: 12pt"&gt;Distance: &lt;span id="distance_result"&gt;${distance}&lt;/span&gt;&lt;/p&gt;
  </code></pre>
  
  <p>works well. </p>