Spring MVC- trzy razy wywoływana metoda z kontrolera

0

Moja metoda z kontrolera jest wywoływana trzy razy i sądzę, że jest to problem z context'em springowym oraz konfiguracją w web.xml, ale na razie nie potrafię tego naprawić. Oto kod:

WebApplication\src\main\webapp\WEB-INF\web.xml

<?xml version="1.0" encoding="UTF-8"?>

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

	<display-name>Spring MVC Application</display-name>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

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

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

</web-app>

WebApplication\src\main\webapp\WEB-INF\dispatcher-servlet.xml

<?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:context="http://www.springframework.org/schema/context"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context.xsd
               http://www.springframework.org/schema/data/jpa
               http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
               http://www.springframework.org/schema/tx
               http://www.springframework.org/schema/tx/spring-tx.xsd">

    <context:component-scan base-package="com.mp.webapp"/>

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

    <context:property-placeholder location="classpath:messeges.properties"/>

    <jpa:repositories base-package="com.mp.webapp.repository"/>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="org.postgresql.Driver"/>
        <property name="url" value="jdbc:postgresql://localhost:5432/web_application"/>
        <property name="username" value="mariola"/>
        <property name="password" value="webapp123!"/>
    </bean>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="persistenceUnitName" value="PersistenceUnit"/>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>

    <tx:annotation-driven/>

</beans>

WebApplication\src\main\java\com\mp\webapp\controller\MainController.java

package com.mp.webapp.controller;

import com.mp.webapp.util.AutowiredLogger;
import org.slf4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/")
public class MainController {

    @AutowiredLogger
    private Logger logger;

	@RequestMapping(method = RequestMethod.GET)
	public String showForm() {
        logger.info("showForm method called");
        return "index";
	}
}

Logi z Tomcata:

"C:\Program Files\apache-tomcat-8.0.5\bin\catalina.bat" run
[2014-08-12 11:45:36,000] Artifact WebApplication:war: Server is not connected. Deploy is not available.
Using CATALINA_BASE:   "C:\Users\micha_000\.IntelliJIdea14\system\tomcat\Unnamed_WebApplication"
Using CATALINA_HOME:   "C:\Program Files\apache-tomcat-8.0.5"
Using CATALINA_TMPDIR: "C:\Program Files\apache-tomcat-8.0.5\temp"
Using JRE_HOME:        "C:\Program Files\Java\jdk1.7.0_45"
Using CLASSPATH:       "C:\Program Files\apache-tomcat-8.0.5\bin\bootstrap.jar;C:\Program Files\apache-tomcat-8.0.5\bin\tomcat-juli.jar"
12-Aug-2014 23:45:36.478 INFO [main] org.apache.catalina.core.AprLifecycleListener.init Loaded APR based Apache Tomcat Native library 1.1.29 using APR version 1.4.8.
12-Aug-2014 23:45:36.479 INFO [main] org.apache.catalina.core.AprLifecycleListener.init APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
12-Aug-2014 23:45:36.949 INFO [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized (OpenSSL 1.0.1e 11 Feb 2013)
12-Aug-2014 23:45:37.020 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-apr-8080"]
12-Aug-2014 23:45:37.026 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["ajp-apr-8009"]
12-Aug-2014 23:45:37.028 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 747 ms
12-Aug-2014 23:45:37.045 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service Catalina
12-Aug-2014 23:45:37.045 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.0.5
12-Aug-2014 23:45:37.052 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-apr-8080"]
12-Aug-2014 23:45:37.057 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-apr-8009"]
12-Aug-2014 23:45:37.058 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 30 ms
Connected to server
[2014-08-12 11:45:37,545] Artifact WebApplication:war: Artifact is being deployed, please wait...
2014-08-12 23:45:39 [org.springframework.web.context.ContextLoader] INFO   - Root WebApplicationContext: initialization started
2014-08-12 23:45:39 [org.springframework.web.context.support.XmlWebApplicationContext] INFO   - Refreshing Root WebApplicationContext: startup date [Tue Aug 12 23:45:39 CEST 2014]; root of context hierarchy
2014-08-12 23:45:39 [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] INFO   - Loading XML bean definitions from ServletContext resource [/WEB-INF/dispatcher-servlet.xml]
2014-08-12 23:45:39 [org.springframework.context.annotation.ClassPathBeanDefinitionScanner] INFO   - JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning
2014-08-12 23:45:39 [org.springframework.context.support.PropertySourcesPlaceholderConfigurer] INFO   - Loading properties file from class path resource [messeges.properties]
2014-08-12 23:45:39 [org.springframework.beans.factory.support.DefaultListableBeanFactory] INFO   - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1c4b47c2: defining beans [mainController,loggerPostProcessor,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.web.servlet.view.InternalResourceViewResolver#0,org.springframework.context.support.PropertySourcesPlaceholderConfigurer#0,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
2014-08-12 23:45:39 [org.springframework.web.context.ContextLoader] INFO   - Root WebApplicationContext: initialization completed in 432 ms
2014-08-12 23:45:39 [org.springframework.web.servlet.DispatcherServlet] INFO   - FrameworkServlet 'dispatcher': initialization started
2014-08-12 23:45:39 [org.springframework.web.context.support.XmlWebApplicationContext] INFO   - Refreshing WebApplicationContext for namespace 'dispatcher-servlet': startup date [Tue Aug 12 23:45:39 CEST 2014]; parent: Root WebApplicationContext
2014-08-12 23:45:39 [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] INFO   - Loading XML bean definitions from ServletContext resource [/WEB-INF/dispatcher-servlet.xml]
2014-08-12 23:45:39 [org.springframework.context.annotation.ClassPathBeanDefinitionScanner] INFO   - JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning
2014-08-12 23:45:39 [org.springframework.context.support.PropertySourcesPlaceholderConfigurer] INFO   - Loading properties file from class path resource [messeges.properties]
2014-08-12 23:45:39 [org.springframework.beans.factory.support.DefaultListableBeanFactory] INFO   - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@6759ae65: defining beans [mainController,loggerPostProcessor,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.web.servlet.view.InternalResourceViewResolver#0,org.springframework.context.support.PropertySourcesPlaceholderConfigurer#0,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; parent: org.springframework.beans.factory.support.DefaultListableBeanFactory@1c4b47c2
2014-08-12 23:45:39 [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] INFO   - Root mapping to handler 'mainController'
2014-08-12 23:45:40 [org.springframework.web.servlet.DispatcherServlet] INFO   - FrameworkServlet 'dispatcher': initialization completed in 226 ms
[2014-08-12 11:45:40,029] Artifact WebApplication:war: Artifact is deployed successfully
[2014-08-12 11:45:40,029] Artifact WebApplication:war: Deploy took 2 484 milliseconds
2014-08-12 23:45:40 [com.mp.webapp.controller.MainController] INFO   - showForm method called
2014-08-12 23:45:40 [com.mp.webapp.controller.MainController] INFO   - showForm method called
2014-08-12 23:45:40 [com.mp.webapp.controller.MainController] INFO   - showForm method called
2014-08-12 23:45:40 [org.springframework.web.servlet.PageNotFound] WARN   - No mapping found for HTTP request with URI [/css/style.css] in DispatcherServlet with name 'dispatcher'
0

Konfiguracja na moje nóbskie oko wygląda ok - ale na końcu masz jakiś problem z assetami ? spróbuj rozwiązać: - No mapping found for HTTP request with URI [/css/style.css] in DispatcherServlet with name 'dispatcher'

0

Co od assetów to pewnie trzeba użyć mvc:resources, ale zastanawiam się o co chodzi z tym trzykrotnym wywoływaniem metod kontrolera.

1

@bakeraw2 ale właściwie co za różnica? ;] Może framework sobie puszcza request do tego co wystawia jako GET w celu walidacji poprawnego deploymentu?

0
Shalom napisał(a):

@bakeraw2 ale właściwie co za różnica? ;] Może framework sobie puszcza request do tego co wystawia jako GET w celu walidacji poprawnego deploymentu?

Wszystko na to wskazuje, ponieważ po deploy wysyłany jest trzy razy request GET a przy późniejszych requestach jest tylko raz, także problem rozwiązany ;) Dzięki

0

Ciekawe podejście - "dziwne ale nie tak będzie" proponuje sprawdzić dokładnie. Prosta aplikacja na springu u mnie tak się nie zachowuje przy czym ja nie mam controlera obsługującego /

1 użytkowników online, w tym zalogowanych: 0, gości: 1