title: How to change controller method that JSP page will not refresh after POST method. Spring MVC, AJAX
body: <blockquote>
    <p>Here i have got AJAX script and i need to correct my controller</p>
  </blockquote>
  
  <p>I need to do that when I'll send data to my controller from the form it will not refresh my page.
  I have got a <code>&lt;form&gt;</code> at my JSP page and AJAX <code>&lt;script&gt;</code> that transfer my data from <code>&lt;textarea&gt;</code> to controller method, but it refreshing my page. How to do that my POST method will work without refreshing page? Thanx!</p>
  
  <pre><code>&lt;form:form method="POST" modelAttribute="message" action="/app/user/messages/${iddialog}"
                          accept-charset="utf-8" ng-app="vandh" ng-controller="validateCtrl"
                          name="messageForm" novalidation="true"&gt;
  
  &lt;form:textarea path="text" class="form-control" rows="1" name="message"
                              id="message" ng-model="message" required="true"&gt;&lt;/form:textarea&gt;
                          &lt;div style="color: black"
                              ng-show="messageForm.message.$dirty &amp;&amp; messageForm.message.$invalid"&gt;
                              &lt;span ng-show="messageForm.message.$error.required"&gt;&lt;spring:message
                                  code="label.entermessage" /&gt;&lt;/span&gt;
                          &lt;/div&gt;
                          &lt;br&gt;
                          &lt;div class="text-center"&gt;
                          &lt;button class="btn btn-success" id="addMessage" name="addMessage"&gt;&lt;spring:message
                                  code="label.sendmessage"/&gt;&lt;/button&gt;
                          &lt;/div&gt;
  
  &lt;/form:form&gt;
  
  &lt;script&gt;
  $("#addMessage").click(function() { 
      var text = $('#message').val();
      $.ajax({ 
      type : "POST", 
      url: "/app/user/messages/${iddialog}" , 
      async : false, 
      data : { 
      text : text 
      } 
      }); 
  
      });
  &lt;/script&gt;   
  </code></pre>
  
  <p>Here is my controller method for POST method. Here i'm checking my message and adding it to database.</p>
  
  <pre><code>public void sendMessage(int iddialog, String text, String messender) {
      DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
      // get current date time with Date()
      Date date = new Date();
      System.out.println(dateFormat.format(date));
      Dialog dialog = new Dialog();
      dialog.setIddialog(iddialog);
      Message mess = new Message();
      mess.setText(text);
      mess.setDialog(dialog);
      mess.setDate(dateFormat.format(date));
      mess.setMessender(messender);
      this.messageService.addMessage(mess);
      this.dialogService.updateUnreadMessInfo(iddialog, messender);
  }
  
  public boolean checkingMessage(String text) {
      if (text.contains("&lt;") || text.contains("&gt;") || text.contains("script") || text.contains("$")) {
          return true;
      } else
          return false;
  }
  
  @RequestMapping(value = "/user/messages/{iddialog}", method = RequestMethod.POST)
  public String messages(@ModelAttribute("message") Message message, @RequestParam(value = "text") String text,
          @PathVariable(value = "iddialog") int iddialog, Model model, Principal principal) {
      System.out.println("ITS HERE");
      if (checkingMessage(text) != true) {
          sendMessage(iddialog, text, principal.getName());
  
          System.out.println("message sent!");
          // return "redirect:/user/messages/"+iddialog;
          return "messagesWithUser";
      } else {
          return "redirect:/user/messages";
      }
  }
  </code></pre>