3 Replies Latest reply: Dec 14, 2011 3:30 AM by gimbal2 RSS

    JSP does not call the right method in controller class

      Hi all,
      I have a jsp file which loads a page at this address: http://localhost:8080/dir/list/

      I've added two checkboxes to the page; user can check both checkboxes, only one or none. Following is code I have in jsp file:
      <form class="checkboxForm">
      <c:when test='${newItems==true}'>
      <input id="newItems" type="checkbox" value="#" name="newItems" class="checkbox2" checked="checked" /><strong> New Businesses   </strong>                                                                 
      <input id="newItems" type="checkbox" value="#" name="newItems" class="checkbox2"/><strong> New Businesses   </strong>
      <c:when test='${rejectedItems==true}'>
      <input id="rejectedItems" type="checkbox" value="#" name="rejectedItems" class="checkbox3" checked="checked"/><strong> Rejected Businesses   </strong>                                                                 
      <input id="rejectedItems" type="checkbox" value="#" name="rejectedItems" class="checkbox3"/><strong> Rejected Businesses   </strong>
      <a href="#" onclick="apply();">
      <button name="apply" class="btn-primary" value="Apply" tabindex="4">Apply</button>
      <script type="text/javascript">
      function apply(){
           var newItems = document.getElementById("newItems").checked;
           var rejectedItems = document.getElementById("rejectedItems").checked;
           alert("Inside apply() method.");
           alert("newItems= " + newItems + ", rejectedItems: " + rejectedItems);     
           window.location = "<c:url value='/list/'/>" + newItems + "/" + rejectedItems;
           alert("window.location= " + window.location);
           alert("Add extra delay!");
           return false;
      This is my Controller class:
      // Method1: this method gets called when the user loads the page for the first time.
      @RequestMapping(value="/list", method = RequestMethod.GET)
      public String list(Model model, HttpServletRequest request) {          
                System.out.println("Controller: method1: Passing through...");
                model.addAttribute("newItems", Boolean.TRUE);
                model.addAttribute("rejectedItems", Boolean.TRUE);
                // Does other things.
      // Method3: this method gets called when user checks/unchecks one of the checkboxes and clicks on Apply button.
      @RequestMapping(value="/list/{newItems}/{rejectedItems}", method = RequestMethod.GET)
      public String list(@PathVariable Boolean newItems, @PathVariable Boolean rejectedItems, Model model, HttpServletRequest request) {          
                System.out.println("Controller: method3: Passing through...");
                model.addAttribute("newItems", newItems);
                model.addAttribute("rejectedItems", rejectedItems);
      // Does other things.
      The way my jsp code is written now, it calls the correct method and displays the correct url. When page loads for the first time, it calls method1 and the url is:

      When the user checks/unchecks one or both checkboxes and clicks on apply button, method3 is called and this url is displayed:

      If both checkboxes are checked which both booleans are set to true:


      Everything is fine... however, if I comment these two lines at the end of JavaScript apply() function,
      alert("window.location= " + window.location);
      alert("Add extra delay!");
      Then method3 will never gets called; only method1 gets called and I see this url:

      I do not know why it gets confused bet. method1 and method3 and cannot pick the right method?

      I tried to use the POST method instead, but the page goes directly to our internal error page and it never reaches the method I wrote in my controller for POST:

      I don't really know what I'm doing wrong? Any help is greatly appraciated.

      Edited by: ronitt on Dec 13, 2011 2:35 PM
        • 1. Re: JSP does not call the right method in controller class
          I changed the <form> in the jsp to div and its working fine. I do not need to have comments in JavaScript funcion() anymore. I don't know why that made the difference though? According to:


          The <form> tag is used to create an HTML form for user input.

          The <form> element can contain one or more of the following form elements:

          An HTML form is used to pass data to a server.

          I do have <button> and also send the data - the value of checkboxes - to server. So I think it should also work with <form>.

          Please let me know if you have any idea. Thanks.
          • 2. Re: JSP does not call the right method in controller class
            It sounds like a JSP page already is a form, and you can't have nested forms.
            • 3. Re: JSP does not call the right method in controller class
              EJP wrote:
              It sounds like a JSP page already is a form, and you can't have nested forms.
              Thank you, I was really stumped by this one. But this is a very simple explanation.

              OP: in the future, don't let people make assumptions. Not once did you mention in your post that you use Spring, I had to do a Google search on the used annotations to figure that out. If you want help, provide proper information.