title: POST method without refreshing page. Spring MVC. JAVA. JSP
body: <p>How to do that when submiting my form at JSP page it will not reload? Here is user sending message and AJAX script (its in my jsp) get back all messages for user. But when i send message (do POST method) it refreshing my page that take some time to loading all messages with AJAX.
  Help pls!!!
  This is my JSP page</p>
  
  <pre><code>    &lt;%@ page language="java" contentType="text/html; charset=utf-8"
      pageEncoding="utf-8"%&gt;
  &lt;%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%&gt;
  &lt;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%&gt;
  &lt;%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%&gt;
  &lt;%@ taglib uri="http://www.springframework.org/security/tags"
      prefix="security"%&gt;
      &lt;%@taglib uri="http://www.springframework.org/tags" prefix="spring"%&gt;
  &lt;%
      request.setCharacterEncoding("UTF-8");
      response.setCharacterEncoding("UTF-8");
  %&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
  &lt;title&gt;Сообщения | Haine and Vold&lt;/title&gt;
  &lt;link rel="shortcut icon" href="&lt;c:url value="/resources/faviicon/iconHV.ico"/&gt;" type="image/x-icon"&gt;
  &lt;link href="&lt;c:url value="/resources/bootstrap/bootstrap.css"/&gt;"
      rel="stylesheet" type="text/css"&gt;
  &lt;link href="&lt;c:url value="/resources/CSS/styles.css"/&gt;" rel="stylesheet"
      type="text/css"&gt;
      &lt;link href="&lt;c:url value="/resources/CSS/tableMessFixedHeader.css"/&gt;" rel="stylesheet"
      type="text/css"&gt;
      &lt;script src="&lt;c:url value="/resources/Angular/angular.min.js"/&gt;"&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
      &lt;nav class="navbar navbar-default"&gt;
          &lt;div class="container-fluid"&gt;
              &lt;div class="navbar-header"&gt;
                  &lt;button type="button" class="navbar-toggle collapsed"
                      data-toggle="collapse" data-target="#bs-example-navbar-collapse-1"&gt;
                      &lt;span class="sr-only"&gt;Toggle navigation&lt;/span&gt; &lt;span
                          class="icon-bar"&gt;&lt;/span&gt; &lt;span class="icon-bar"&gt;&lt;/span&gt; &lt;span
                          class="icon-bar"&gt;&lt;/span&gt;
                  &lt;/button&gt;
                  &lt;a class="navbar-brand" href="#"&gt;Haine and Vold&lt;/a&gt;
              &lt;/div&gt;
  
              &lt;div class="collapse navbar-collapse"
                  id="bs-example-navbar-collapse-1"&gt;
                  &lt;ul class="nav navbar-nav"&gt;
  
                      &lt;li class="dropdown"&gt;&lt;a href="#" class="dropdown-toggle"
                          data-toggle="dropdown" role="button" aria-expanded="false"&gt;&lt;spring:message
                                  code="label.menuprofile" /&gt;&lt;span class="caret"&gt;&lt;/span&gt;&lt;/a&gt;
                          &lt;ul class="dropdown-menu" role="menu"&gt;
                              &lt;li&gt;&lt;a href="/haine/user"&gt;&lt;spring:message
                                          code="label.menuprofile" /&gt;&lt;/a&gt;&lt;/li&gt;
                              &lt;li&gt;&lt;a href="/haine/user/updateinfo"&gt;&lt;spring:message
                                          code="label.menusettinginfo" /&gt;&lt;/a&gt;&lt;/li&gt;
                          &lt;/ul&gt;&lt;/li&gt;
                      &lt;li&gt;&lt;a href="/haine/user/messages"&gt;&lt;spring:message
                                  code="label.menumessages" /&gt; (${allUserMess})&lt;/a&gt;&lt;/li&gt;
                      &lt;li&gt;&lt;a href="/haine/user/search"&gt;&lt;spring:message
                                  code="label.menusearch" /&gt;&lt;/a&gt;&lt;/li&gt;
                      &lt;li class="dropdown"&gt;&lt;a href="#" class="dropdown-toggle"
                          data-toggle="dropdown" role="button" aria-expanded="false"&gt;&lt;spring:message
                                  code="label.menuinformation" /&gt;&lt;span class="caret"&gt;&lt;/span&gt;&lt;/a&gt;
                          &lt;ul class="dropdown-menu" role="menu"&gt;
                              &lt;li&gt;&lt;a href="/haine/user/rules"&gt;&lt;spring:message
                                          code="label.menurules" /&gt;&lt;/a&gt;&lt;/li&gt;
                              &lt;li class="divider"&gt;&lt;/li&gt;
                              &lt;li&gt;&lt;a href="/haine/user/feedback"&gt;&lt;spring:message
                                          code="label.menusendfeedback" /&gt;&lt;/a&gt;&lt;/li&gt;
                          &lt;/ul&gt;&lt;/li&gt;
  
                      &lt;security:authorize ifAnyGranted="ROLE_ADMIN"&gt;
                          &lt;li class="dropdown"&gt;&lt;a href="#" class="dropdown-toggle"
                              data-toggle="dropdown" role="button" aria-expanded="false"&gt;&lt;spring:message
                                      code="label.menuadministration" /&gt;&lt;span class="caret"&gt;&lt;/span&gt;&lt;/a&gt;
                              &lt;ul class="dropdown-menu" role="menu"&gt;
                                  &lt;li&gt;&lt;a href="/haine/admin"&gt;&lt;spring:message
                                              code="label.menuadminpanel" /&gt;&lt;/a&gt;&lt;/li&gt;
                                  &lt;li&gt;&lt;a href="/haine/admin/feedbacklist"&gt;&lt;spring:message
                                              code="label.menufeedbacks" /&gt;(${countOfFeedbacks})&lt;/a&gt;&lt;/li&gt;
                              &lt;li&gt;&lt;a href="/haine/admin/newusers"&gt;&lt;spring:message
                                              code="label.newusers" /&gt;(${countOfNewUsers})&lt;/a&gt;&lt;/li&gt;
                              &lt;/ul&gt;&lt;/li&gt;
                      &lt;/security:authorize&gt;
  
                  &lt;/ul&gt;
                  &lt;ul class="nav navbar-nav navbar-right"&gt;
                      &lt;li&gt;&lt;a href="?locale=ru"&gt;&lt;img
                              src="&lt;c:url value="/resources/languageicons/rus.png"/&gt;"
                              alt="Russian Language" title="Сменить язык интерфейса на русский"&gt;&lt;/a&gt;
                      &lt;/li&gt;
                      &lt;li&gt;&lt;a href="?locale=en"&gt;&lt;img
                              src="&lt;c:url value="/resources/languageicons/usa.png"/&gt;"
                              alt="USA Language" title="Change interface language to american"&gt;&lt;/a&gt;
                      &lt;/li&gt;
                  &lt;/ul&gt;
                  &lt;c:url var="logoutUrl" value="/j_spring_security_logout" /&gt;
                  &lt;form class="navbar-form navbar-right" action="${logoutUrl}"
                      method="post"&gt;
                      &lt;button class="btn btn-default" type="submit"&gt;
                          &lt;spring:message code="label.logoutbutton" /&gt;
                      &lt;/button&gt;
                      &lt;input type="hidden" name="${_csrf.parameterName}"
                          value="${_csrf.token}" /&gt;
                  &lt;/form&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/nav&gt;
  &lt;div class="container"&gt;
      &lt;img class="img-responsive logo" src="/haine/resources/logo.jpg"&gt;
  &lt;/div&gt;
  &lt;br&gt;
  &lt;div class="scrollTableMessages" id="mess"&gt;
  &lt;/div&gt;
  &lt;form:form method="POST" modelAttribute="message"
                          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" 
                              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" type="submit"&gt;&lt;spring:message
                                  code="label.sendmessage"/&gt;&lt;/button&gt;
                          &lt;/div&gt;
  
  &lt;/form:form&gt;
  
  &lt;script&gt;
          var app = angular.module('vandh', []);
          app.controller('validateCtrl', function($scope) {
              $scope.message = '';
          });
      &lt;/script&gt;
  &lt;/body&gt;
  &lt;script src="&lt;c:url value="/resources/Jquery/jquery-2.1.4.min.js"/&gt;"
      type="text/javascript"&gt;&lt;/script&gt;
  &lt;script src="&lt;c:url value="/resources/bootstrap/bootstrap.js"/&gt;"
      type="text/javascript"&gt;&lt;/script&gt;
      &lt;script&gt;
  
      &lt;/script&gt;
  
  
  &lt;script&gt;
  var oldC = 0, newC = -1;
      function getMessages() {
          $.ajax({
          type : 'GET',
          url : '/haine/user/countofmess/${iddialog}',
          success : function(rs) 
          {
              newC = rs;
              if(oldC != newC)
              {
                  $.ajax({
                  type : 'GET',
                  url : '/haine/user/mess/${iddialog}',
                  success : function(r) 
                  {
                      $('#mess').html(r);
                      document.getElementById("mess").scrollTop = 9999;
                      console.log(r);
  
                  },
                  error : function(r) {
                      alert(r);
                  }
                  });
                  oldC = newC;
              }
          },
          error : function(rs) {
              alert(rs);
          }
          });
      }
      setInterval(getMessages, 1000);
  &lt;/script&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>And this is my controller</p>
  
  <pre><code>    @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) {
          if(checkingMessage(text)!=true)
          {
          sendMessage(iddialog, text, principal.getName());
  
          System.out.println("message sent!");
              return "messagesWithUser";
          }
          else
          {
              return "redirect:/user/messages";
          }
      }
  </code></pre>