Results 1 to 7 of 7

Thread: Controller called twice in one request when changed the View

Hybrid View

  1. #1
    Join Date
    May 2008
    Posts
    8

    Unhappy Controller called twice in one request when changed the View

    Hey guys!

    I am facing a problem with a multiple calls to controller (within one request) when wanting to change to a different view than default. Specifically, in class:

    Code:
    @Controller
    public class DisplayRecording {
    
           //....
    
    	private static int callNumber = 1;
    
    	@RequestMapping("/displayRecording.do")
    	public ModelAndView displayRecording(@RequestParam(value = "recordingId", required = false)
    	Long recordingId, ModelMap modelMap) {
    		
    		System.out.println("callNumber:"+callNumber++);
    
                   //.... some omitted code 
    
    		if (....some condition....) { // display presentation
    
    			ModelAndView mav = new ModelAndView("displayPresentation");  //results in calling this controller twice
    
    //			ModelAndView mav = new ModelAndView("displayRecording"); // using this does not result in two calls
    			mav.addAllObjects(modelMap);
    			
    			return mav;
    		}
    
    		// display recording in the normal way:
    		return new ModelAndView().addAllObjects(modelMap);
    	}
    
    }
    where when the condition is false, the default displayRecording.jsp view is used. However, when the view is changed to "displayPresentation" (the displayPresentation.jsp), the controller (the displayRecording() method) is called twice for some to me unknown reasons. When uncommenting the ModelAndView("displayRecording") line in the if-block, the controller's method again is called only once.

    Is there any explanation for such a behaviour?

    My viewResolver configuration in springmvc-servlet.xml is
    Code:
    <bean id="viewResolver"
    		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    		<property name="prefix" value="/jsp/" />
    		<property name="suffix" value=".jsp" />
    	</bean>
    and normally works just fine. And the method is not called from anywhere within my code.

    Many thanks for any ideas in advance!

    Ibisek

  2. #2
    Join Date
    May 2008
    Posts
    8

    Default Problem solved

    Hi everyone,

    the problem was solved. The double call was surprisingly caused by an empty src field in a img tag in the views:

    <img src="" alt="some text" width="640" height="480">

    Filling the src with something else than an empty string fixes the described problem. The question still stands - why - but it works now.

    So be careful and don't do such a silly mistake as I did!

    Ibisek

  3. #3
    Join Date
    Mar 2009
    Posts
    1

    Default

    ibisek, the problem with this code is that the browser makes another request to get the image file, using the url in the src field. If the field is filled with a "" (or, in my case, a "#") string, the request is made to the current page, calling the controller twice. Thank you to report your solution, helped me a lot!

  4. #4
    Join Date
    May 2009
    Location
    POLAND
    Posts
    52

    Default

    I had same problem.
    I know that this is Browser issue but I didn't know where is error.

    I had <script src=""></script> temporary line
    grrrrrrrrrrrrrrr

  5. #5
    Join Date
    Dec 2009
    Posts
    3

    Default That helped

    Your tip helped me a lot.

    In my case, instead of an empty img src tag, I had an empty href for a css import and an empty src for a javascript import. What a mess!

    But strange that this would cause the controller to be called repeatedly - up to six times in my case.

    Best,
    Abe

  6. #6

    Red face

    Hi ibisek ,

    I am also facing the same problem. Can you identify , why?

    Search for "Request Mapping calling two times for one submit"

    How did you used for more than one button?

    i have used like that

    HTML Code:
    <td><input type="submit" value="Add User" onclick="this.form.action='addUser.htm'; this.form.submit()"></td>
    			<td><input type="submit" value="Cancel" onclick="this.form.action='cancelViewUser.htm'; this.form.submit()"></td>
    is any other way to do like that...

    Please let me know..

    thank
    rahul

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •