title: JSP - How to submit form when clicking a link?
body: <p>I want to submit a form when I click on a link but when I click the element it doesn't prevent the default action. And I also get this message error: <code>HTTP Status 405 - Request method 'POST' not supported</code>. This is what I tried so far: </p>
  
  <p>users.jsp</p>
  
  <pre><code>&lt;%@ include file="/WEB-INF/template/taglibs.jsp"%&gt;    
  &lt;div class="container"&gt;
      &lt;%@ include file="menu.jsp" %&gt;
      &lt;a class="btn btn-warning addbtn buttons" href="http://localhost:8080/Catering/index/addUser"&gt;Add&lt;/a&gt;
      &lt;div class="panel panel-default"&gt;
          &lt;table class="table table-hover"&gt;
              &lt;thead&gt;
                  &lt;tr&gt;
                      &lt;td class="hidden"&gt;Id&lt;/td&gt;
                      &lt;td&gt;First name&lt;/td&gt;
                      &lt;td&gt;Last name&lt;/td&gt;
                      &lt;td&gt;User name&lt;/td&gt;
                      &lt;td class="table-row-option"&gt;&lt;/td&gt;
                      &lt;td class="table-row-option"&gt;&lt;/td&gt;
                  &lt;/tr&gt;
              &lt;/thead&gt;
              &lt;tbody&gt;
                  &lt;c:forEach items="${list}" var="item"&gt;
                      &lt;tr&gt;
                          &lt;td class="hidden"&gt;${item.getUserid()}&lt;/td&gt;
                          &lt;td&gt;${item.getFname()}&lt;/td&gt;
                          &lt;td&gt;${item.getLname()}&lt;/td&gt;
                          &lt;td&gt;${item.getUsername()}&lt;/td&gt;
                          &lt;td class="table-row-option"&gt;                               
                              &lt;a class="btn btn-warning buttons col-width" href="http://localhost:8080/Catering/index/editUser?id=${item.getUserid()}"&gt;Edit&lt;/a&gt;
                          &lt;/td&gt;
                          &lt;td class="table-row-option"&gt;
                              &lt;form:form method="post" action="#" commandName="delete"&gt;
                                  &lt;input type="hidden" value="${item.getUserid()}" name="id"/&gt;
                                  &lt;a class="btn btn-warning buttons col-width lnk-delete" href="#"&gt;Delete&lt;/a&gt;                             
                              &lt;/form:form&gt;                    
                          &lt;/td&gt;
                      &lt;/tr&gt;
                  &lt;/c:forEach&gt;        
              &lt;/tbody&gt;
          &lt;/table&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;script type="text/javascript"&gt;
  $(document).ready(function(){
           $("a.lnk-delete").click(function(event){
              event.stopImmediatePropagation();
              var frm = $(this).closest('form');
              frm.submit();
          }); 
  
      });
  &lt;/script&gt;
  </code></pre>
  
  <p>UsersController.java</p>
  
  <pre><code>package catering.web.controller;
  
  import java.util.ArrayList;
  import java.util.List;
  
  import org.apache.log4j.Logger;
  import org.springframework.security.core.Authentication;
  import org.springframework.stereotype.Controller;
  import org.springframework.ui.Model;
  import org.springframework.web.bind.annotation.ModelAttribute;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  import org.springframework.web.bind.annotation.RequestParam;
  
  import catering.web.data_access.UserDataAccess;
  import catering.web.data_access.UserSummaryDataAccess;
  import catering.web.mapper.UserMapper;
  import catering.web.model.UserModel;
  import catering.web.model.UserModelSummary;
  import catering.web.view_model.UserViewModel;
  
  
  @Controller
  public class UsersController {
  
      protected static Logger logger = Logger.getLogger("controller");
      static final String guid = "1390e9e7-7792-4e24-b66f-6da90b8ebefb";
  
      @RequestMapping(value = "users", method = RequestMethod.GET)
      public String indexUsers(Model model, Authentication authentication){
  
          logger.debug("Received request to users page(GET)");
  
          List&lt;UserModelSummary&gt; users = UserSummaryDataAccess.getUsersSummary();
  
          model.addAttribute("username", "You are logged in as " + authentication.getPrincipal());
          model.addAttribute("list", users);
  
          return "users";
      }
  
      @RequestMapping(value = "/addUser", method = RequestMethod.GET)
      public String addUserPage(Model model,Authentication authentication){
  
          logger.debug("Received request to show add user page(GET)");    
  
          populateDrop(model);
          model.addAttribute("username", "You are logged in as " + authentication.getPrincipal());
          model.addAttribute("addForm", new UserViewModel());
  
  
          return "adduser";
      }
  
      @RequestMapping(value = "/addUser", method = RequestMethod.POST)
      public String addUser(@ModelAttribute("addForm") UserViewModel model, Model viewmodel){
  
          logger.debug("Received request to show add user page(POST)");   
  
          int isValid = 0;
  
  
  
          if(model.getConfirmedpassword().equals(model.getPass())){
  
              //map the view model to database model
              UserModel user = UserMapper.mapUVMtoUM(model);          
  
              isValid = UserDataAccess.insertUser(user);
              if(isValid != -1){
                  return "redirect:users";
              } else {
                  viewmodel.addAttribute("error", "The username already exists!");
                  return "adduser";
              }
  
          } else{         
              viewmodel.addAttribute("passeorr", "Password unmatch!");
              return "adduser";
          }       
      }
  
      @RequestMapping(value = "/editUser", method = RequestMethod.GET)
      public String getEditUserPage(@RequestParam int id, Model model, Authentication authentication) {
  
  
  
          logger.debug("Received request to show edit user page(GET)");   
  
          model.addAttribute("username", "You are logged in as " + authentication.getPrincipal());
          populateDrop(model);
  
          UserModel user = UserDataAccess.getUserById(id);
  
          UserViewModel userVM = UserMapper.mapUMtoUVM(user);
          userVM.setPass(guid);
          userVM.setConfirmedpassword(guid);  
          model.addAttribute("id", userVM.getId());
          model.addAttribute("firstName", userVM.getFirstName());
          model.addAttribute("lastName", userVM.getLastName());
          model.addAttribute("role", userVM.getRole());
          model.addAttribute("user_name", userVM.getUser_name());
          model.addAttribute("pass", userVM.getPass());
  
  
  
  
          model.addAttribute("editForm", userVM);
  
          return "edituser";
      }
  
      @RequestMapping(value = "/editUser", method = RequestMethod.POST)
      public String editUser(@ModelAttribute("editForm") UserViewModel model, Model viewmodel){
  
          logger.debug("Received request to show edit user page(POST)");  
  
          if(model.getRole().equals("NONE")){
              viewmodel.addAttribute("error", "You need to select a role!");
              return "edituser";
          }       
  
          if(model.getPass().equals(model.getConfirmedpassword()) &amp;&amp; model.getPass().equals(guid)){
  
              UserModel user = UserMapper.mapUVMtoUM(model);
              int inserted = UserDataAccess.updateWithoutPassword(user);  
              if(inserted != -1){
                  return "redirect:users";
              } else{
                  viewmodel.addAttribute("error", "The username already exists!");
                  return "edituser";
              }
          } else {
              UserModel user = UserMapper.mapUVMtoUM(model);
              int inserted = UserDataAccess.updateWithPassword(user);
              if(inserted != -1){
                  return "redirect:users";
              } else{
                  viewmodel.addAttribute("error", "The username already exists!");
                  return "edituser";
              }
          }       
  
      }
  
  
      @RequestMapping(value = "#", method = RequestMethod.POST)
      public String deleteUser(@ModelAttribute("delete") @RequestParam int id){
  
          logger.debug("Received request to delete user page(POST)");
  
  
          UserDataAccess.deleteUser(id);
          return "redirect:users";
      }   
  
  
      private void populateDrop(Model model){
          List&lt;String&gt; roles = new ArrayList&lt;String&gt;();
          roles.add("ADMIN");
          roles.add("USER");
          model.addAttribute("roles",roles);
      }
  }
  </code></pre>