0

i simplified my code in my project and left only web.xml ,webcontext configuration and simplest controller with the call counter but the problem remains

In result i have 3 calls one after another, why ?

Web.xml

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <display-name>Spring MVC Application</display-name>
    <welcome-file-list>
        <welcome-file>main.jsp</welcome-file>

    </welcome-file-list>

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

WebContext

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:beans="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
    <context:component-scan base-package="com.dubovskiy.movc"/>
    <mvc:annotation-driven />
    <context:annotation-config/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

Controller

@Controller
public class HelloController {
    public static int count;

    @RequestMapping(value = {"/", "/main"}, method = RequestMethod.GET)
    public String printWelcome(ModelMap model) {

        model.addAttribute("M"," Have many times "+ ++count);
        return "main";
    }
}

View

<%@ page contentType="text/html; charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<!DOCTYPE html>
<html>
<head>
    <title></title>

</head>
<body>

<h2>${M}</h2>

</body>
</html>
Mikhail
  • 2,690
  • 4
  • 28
  • 43
  • **Never** store some kind of state in spring MVC controllers, what do you expect, how do you fetch the jsp (if with a browser, wich one && did you refresh the page) –  Oct 23 '14 at 18:14
  • its only test. ofcource i have a service and dao level. – Mikhail Oct 23 '14 at 18:18
  • after launch i want to see 1 but not 3 – Mikhail Oct 23 '14 at 18:20
  • 4
    http://stackoverflow.com/questions/4460661/what-to-do-with-chrome-sending-extra-requests – Sotirios Delimanolis Oct 23 '14 at 18:20
  • 1) Please check whether the browser is sending multiple requests or not. 2) it this an initalization problem, or does the counter is incremented by 3, every time you open an new broser, refresh the browser or enter the url? – Ralph Oct 23 '14 at 18:32
  • the counter is incremented by 3 only first time after launch the tomcat and then every time when i open an new broser, refresh the browser or enter the url its increment by 1. – Mikhail Oct 23 '14 at 19:50
  • hi,how did you solve your problem finally? – TIMFUNNY May 09 '16 at 11:39

1 Answers1

0

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.

Please refer the complete answer

http://forum.spring.io/forum/spring-projects/web/46115-controller-called-twice-in-one-request-when-changed-the-view

Pavan
  • 1,219
  • 13
  • 15