Tomcat application fails to start after adding CORS support - maven

I have a Tomcat 7 project which works like a charm on my Eclipse-integrated test server, but fails to start on the production server.
It previously worked on production too, but I had to add CORS support, which somehow made it fail.
To add CORS support, I added this to my pom.xml
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-catalina</artifactId>
<version>8.0.22</version>
<scope>provided</scope>
</dependency>
and this to my web.xml
<filter>
<filter-name>CorsFilter</filter-name>
<filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
If I remove these lines again, it works on the production server, but CORS doesn't work.
The server throws a ClassNotFoundException when I try to start the application, which is strange, because the class is clearly there in the .war-file.
The catalina log claims a few .jar-files fails to validate because "jar not loaded". The .jars are all in the .war, so I do not understand why it fails.
Output from the logs:
Catalina log:
May 19, 2015 9:10:29 AM org.apache.catalina.util.LifecycleBase stop
INFO: The stop() method was called on component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/APIMR]] after stop() had already been called. The second call will be ignored.
May 19, 2015 9:10:29 AM org.apache.catalina.startup.HostConfig deleteRedeployResources
INFO: Undeploying context [/APIMR]
May 19, 2015 9:10:36 AM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deploying web application archive /var/lib/tomcat/webapps/APIMR.war
May 19, 2015 9:10:37 AM org.apache.catalina.loader.WebappClassLoader validateJarFile
INFO: validateJarFile(/usr/share/tomcat/webapps/APIMR/WEB-INF/lib/tomcat-el-api-8.0.22.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/el/Expression.class
May 19, 2015 9:10:37 AM org.apache.catalina.loader.WebappClassLoader validateJarFile
INFO: validateJarFile(/usr/share/tomcat/webapps/APIMR/WEB-INF/lib/tomcat-servlet-api-8.0.22.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class
May 19, 2015 9:10:38 AM org.apache.catalina.core.StandardContext startInternal
SEVERE: Error filterStart
May 19, 2015 9:10:38 AM org.apache.catalina.core.StandardContext startInternal
SEVERE: Context [/APIMR] startup failed due to previous errors
localhost log:
May 19, 2015 8:22:11 AM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'dispatcher'
May 19, 2015 8:22:13 AM org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: contextInitialized()
May 19, 2015 8:22:13 AM org.apache.catalina.core.ApplicationContext log
INFO: SessionListener: contextInitialized()
May 19, 2015 8:22:13 AM org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: attributeAdded('org.apache.jasper.compiler.TldLocationsCache', 'org.apache.jasper.compiler.TldLocationsCache#d45f6a5')
May 19, 2015 9:10:05 AM org.apache.catalina.core.StandardContext filterStart
SEVERE: Exception starting filter CorsFilter
java.lang.ClassNotFoundException: org.apache.catalina.filters.CorsFilter
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
<snip>
May 19, 2015 9:10:38 AM org.apache.catalina.core.StandardContext filterStart
SEVERE: Exception starting filter CorsFilter
java.lang.ClassNotFoundException: org.apache.catalina.filters.CorsFilter
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
<snip>
Any suggestions?

The server will reject jars which already belong to the servers runtime (tomcat-**.jar, servlet*.jar, ...). Try this CORS filter instead: http://mvnrepository.com/artifact/com.thetransactioncompany/cors-filter
pom.xml
<dependency>
<groupId>com.thetransactioncompany</groupId>
<artifactId>cors-filter</artifactId>
<version>2.4</version>
</dependency>
web.xml
<filter>
<filter-name>CORS</filter-name>
<filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CORS</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

In my case adding the dependency did not work. It worked when I added two jars in the tomcat lib folder as below:
cors-filter-2.5.jar
java-property-utils-1.10.jar

Although being an old post, this was so helpful. I cannot be grateful enough for sharing this .. Man you saved me from many more days of struggle.
Just in case someone else will bump into the same issue, here is what I done:
Added the maven dependency to my project's pom.xml file:
http://mvnrepository.com/artifact/com.thetransactioncompany/cors-filter/2.6
Added the filter to my project's web.xml /WEB-INF/web.xml file to read like below and restarted tomcat:
</web-app>
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>packages</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/appapi/*</url-pattern>
</servlet-mapping>
<filter>
<filter-name>CORS</filter-name>
<filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CORS</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
This seem to be working perfectly with tomcat server, 8 and 9. If the changes wont take effect, restart the IDE "eclipse" in my case.
Thank you again for the awesome work guys. :)

Related

Spring Web Service - Unable to find WSDL

I have created Spring Web service and deployed in the Weblogic service . I got below logs:
Jun 19, 2014 9:21:29 PM org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization started
Jun 19, 2014 9:21:30 PM org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh
INFO: Refreshing Root WebApplicationContext: startup date [Thu Jun 19 21:21:30 IST 2014]; root of context hierarchy
Jun 19, 2014 9:21:31 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory#3b14dd4: defining beans []; root of factory hierarchy
Jun 19, 2014 9:21:31 PM org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization completed in 2041 ms
Jun 19, 2014 9:21:32 PM org.springframework.ws.transport.http.MessageDispatcherServlet initServletBean
INFO: FrameworkServlet 'test-ws': initialization started
Jun 19, 2014 9:21:32 PM org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh
INFO: Refreshing WebApplicationContext for namespace 'test-ws-servlet': startup date [Thu Jun 19 21:21:32 IST 2014]; parent: Root WebApplicationContext
Jun 19, 2014 9:21:32 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/test-ws-servlet.xml]
Jun 19, 2014 9:21:32 PM org.springframework.ws.soap.addressing.server.AnnotationActionEndpointMapping afterPropertiesSet
INFO: Supporting [WS-Addressing August 2004, WS-Addressing 1.0]
Jun 19, 2014 9:21:32 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory#213f529: defining beans [org.springframework.ws.server.endpoint.mapping.PayloadRootAnnotationMethodEndpointMapping#0,org.springframework.ws.soap.server.endpoint.mapping.SoapActionAnnotationMethodEndpointMapping#0,org.springframework.ws.soap.addressing.server.AnnotationActionEndpointMapping#0,org.springframework.ws.server.endpoint.adapter.method.dom.DomPayloadMethodProcessor#0,org.springframework.ws.server.endpoint.adapter.method.SourcePayloadMethodProcessor#0,org.springframework.ws.server.endpoint.adapter.method.jaxb.XmlRootElementPayloadMethodProcessor#0,org.springframework.ws.server.endpoint.adapter.method.jaxb.JaxbElementPayloadMethodProcessor#0,org.springframework.ws.server.endpoint.adapter.DefaultMethodEndpointAdapter#0,org.springframework.ws.soap.server.endpoint.SoapFaultAnnotationExceptionResolver#0,org.springframework.ws.soap.server.endpoint.SimpleSoapExceptionResolver#0,getCptSkuDetailsWebService,getSkuDetailsWebService,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,skuDetails,skuDetailsSchema,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; parent: org.springframework.beans.factory.support.DefaultListableBeanFactory#3b14dd4
Jun 19, 2014 9:21:33 PM org.springframework.ws.soap.saaj.SaajSoapMessageFactory afterPropertiesSet
INFO: Creating SAAJ 1.3 MessageFactory with SOAP 1.1 Protocol
Jun 19, 2014 9:21:33 PM org.springframework.ws.transport.http.MessageDispatcherServlet initServletBean
INFO: FrameworkServlet 'test-ws': initialization completed in 1167 ms
But when I try to hit the URL cant able to find the WSDL saying 404 .
Please find the web.xml
<servlet>
<servlet-name>test-ws</servlet-name>
<servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>
<init-param>
<param-name>transformWsdlLocations</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>test-ws</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>test-ws</servlet-name>
<url-pattern>*.wsdl</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
Please find the test-ws-servlet.xml file
<bean id="testDetails"
class="org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition">
<property name="schema" ref="testDetailsSchema" />
<property name="portTypeName" value="GetTestServicePortType" />
<property name="locationUri" value="/testDetailsService/" />
</bean>
<bean id="testDetailsSchema" class="org.springframework.xml.xsd.SimpleXsdSchema">
<property name="xsd" value="/schemas/TestDetails.xsd" />
</bean>
URL i used is :
http://localhost:7001/<projectName>/<locationUri>/<beanid>.wsdl
Please help me in this case. I can understand from logs that WSDL has been generated, if my understanding is wrong please help in that.
Also please help me in finding the WSDL. Is the URL I am using to access the WSDL is correct ?
Should work like this
http://localhost:7001/<projectName>/<locationUri>.wsdl
beanid is redundant, because one locationUri presumes only one WebService, so only one WSDL.
See here: https://docs.spring.io/spring-ws/docs/current/reference/#tutorial-publishing-wsdl

integrating jsf2.1 Spring 4

As title is saying I want to integrate Spring with JSF and aslo with Spring Security...
The problem that Im having now is that I can't run the index page when i put on web.xml org.springframework.web.context.ContextLoaderListener
even though I have org.springframework.web.jsf.el.SpringBeanFacesELRe solver declared in faces-config.xml.
this the stack :
mai 11, 2014 5:03:55 PM org.apache.catalina.core.AprLifecycleListener init
Infos: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre7\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;D:\app\Ayman\product\11.2.0\dbhome_1\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\7-Zip;.
mai 11, 2014 5:03:55 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
Avertissement: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:monProjet' did not find a matching property.
mai 11, 2014 5:03:55 PM org.apache.coyote.AbstractProtocol init
Infos: Initializing ProtocolHandler ["http-bio-5050"]
mai 11, 2014 5:03:55 PM org.apache.coyote.AbstractProtocol init
Infos: Initializing ProtocolHandler ["ajp-bio-8009"]
mai 11, 2014 5:03:55 PM org.apache.catalina.startup.Catalina load
Infos: Initialization processed in 361 ms
mai 11, 2014 5:03:55 PM org.apache.catalina.core.StandardService startInternal
Infos: Démarrage du service Catalina
mai 11, 2014 5:03:55 PM org.apache.catalina.core.StandardEngine startInternal
Infos: Starting Servlet Engine: Apache Tomcat/7.0.53
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/Users/Ayman/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/monProjet/WEB-INF/lib/slf4j-jdk14-1.5.8.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Users/Ayman/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/monProjet/WEB-INF/lib/slf4j-log4j12-1.6.6.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.JDK14LoggerFactory]
SLF4J: The requested version 1.5.8 by your slf4j binding is not compatible with [1.6]
SLF4J: See http://www.slf4j.org/codes.html#version_mismatch for further details.
mai 11, 2014 5:03:59 PM org.apache.catalina.core.ApplicationContext log
Infos: Initializing Spring root WebApplicationContext
mai 11, 2014 5:03:59 PM org.apache.catalina.core.StandardContext listenerStart
Grave: Exception lors de l'envoi de l'évènement contexte initialisé (context initialized) à l'instance de classe d'écoute (listener) org.springframework.web.context.ContextLoaderListener
java.lang.AbstractMethodError: org.slf4j.impl.JDK14LoggerAdapter.log(Lorg/slf4j/Marker;Ljava/lang/String;ILjava/lang/String;[Ljava/lang/Object;Ljava/lang/Throwable;)V
at org.apache.commons.logging.impl.SLF4JLocationAwareLog.info(SLF4JLocationAwareLog.java:159)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:194)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4973)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5467)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
mai 11, 2014 5:03:59 PM com.sun.faces.config.ConfigureListener contextInitialized
Infos: Initialisation de Mojarra 2.2.2 ( 20130809-1625 https://svn.java.net/svn/mojarra~svn/tags/2.2.2#12376) pour le contexte «/monProjet»
mai 11, 2014 5:03:59 PM com.sun.faces.spi.InjectionProviderFactory createInstance
Infos: JSF1048 : Présence d’annotations PostConstruct/PreDestroy Les méthodes de beans gérés marquées avec ces annotations auront des annotations dites traitées.
mai 11, 2014 5:03:59 PM org.primefaces.webapp.PostConstructApplicationEventListener processEvent
Infos: Running on PrimeFaces 3.5
mai 11, 2014 5:03:59 PM org.apache.catalina.core.StandardContext startInternal
Grave: Error listenerStart
mai 11, 2014 5:04:00 PM org.apache.catalina.core.StandardContext startInternal
Grave: Erreur de démarrage du contexte [/monProjet] suite aux erreurs précédentes
mai 11, 2014 5:04:00 PM org.apache.catalina.core.ApplicationContext log
Infos: Closing Spring root WebApplicationContext
mai 11, 2014 5:04:00 PM org.apache.coyote.AbstractProtocol start
Infos: Starting ProtocolHandler ["http-bio-5050"]
mai 11, 2014 5:04:00 PM org.apache.coyote.AbstractProtocol start
Infos: Starting ProtocolHandler ["ajp-bio-8009"]
mai 11, 2014 5:04:00 PM org.apache.catalina.startup.Catalina start
Infos: Server startup in 4527 ms
and this is web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<description>generated-servlet</description>
<servlet-name>Final Servlet</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Welcome page -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- JSF Mapping -->
<servlet>
<servlet-name>facesServlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>facesServlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>facesServlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
</web-app>
The Problem
This is not necessarily a JSF/Spring problem as it is an SLF4J library incompatibility issue. The warning signs are there:
First there's the SLF4J warning:
SLF4J: Class path contains multiple SLF4J bindings.
Then there's the log output showing the location of the conflicting libs:
SLF4J: Found binding in [jar:file:/C:/Users/Ayman/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/monProjet/WEB-INF/lib/slf4j-jdk14-1.5.8.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Users/Ayman/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/monProjet/WEB-INF/lib/slf4j-log4j12-1.6.6.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SL4J automatically selects a binding
Actual binding is of type [org.slf4j.impl.JDK14LoggerFactory]
Then the warning/error that breaks everything:
SLF4J: The requested version 1.5.8 by your slf4j binding is not compatible with [1.6]
Then there's the context-loader listener error that follows, as a result of the previous 2 warnings:
Grave: Exception lors de l'envoi de l'évènement contexte initialisé (context initialized) à l'instance de classe d'écoute (listener) org.springframework.web.context.ContextLoaderListener
java.lang.AbstractMethodError: org.slf4j.impl.JDK14LoggerAdapter.log(Lorg/slf4j/Marker;Ljava/lang/String;ILjava/lang/String;[Ljava/lang/Object;Ljava/lang/Throwable;)V
The incompatibility is between the 1.5.8 version of your SLF4J binding (slf4j-jdk14-1.5.8.jar) and your version of the SLF4J api. Your version of SLF4J-api (1.6 in your case) must match the version of the selected binding.
To Solve
You need to either step down your version of the SLF4J api to 1.5.8. or update the version of your binding to 1.6

The requested resource is not available. Spring MVC

I have 5 hello world projects that have the same error:
"description: The requested resource is not available."
I'm using jdk7, tomcat7, maven3.1.1. I always use mvn clean/package.
Here is one of this projects
/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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- the package is right( checked it twice) -->
<context:component-scan base-package="ua.abond.tutor.controller" />
<!-- without this tag I get "No mapping found for HTTP request with URI [/SecondSite/hello.htm] in DispatcherServlet with name 'dispatcher'" -->
<mvc:default-servlet-handler />
<!-- also tried mvc:annotation-driven and context:annotation-config tags
didnt help too-->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/pages/" p:suffix=".jsp">
</bean>
<!-- prefix is right, if I had permission, I would have shared my root screenshot -->
</beans>
/WEB-INF/web.xml
<web-app 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"
version="2.5">
<display-name>SecondSite</display-name>
<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>*.htm</url-pattern>
<!-- also tried /*, *, /dispatcher/*, /dispatcher/*.htm and / -->
</servlet-mapping>
<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>
</web-app>
controller
#Controller
public class Home {
String message = "Welcome to your 1st Maven Spring project !";
#RequestMapping(value = "/hello")//also tried: hello, /hello.htm ... no result
public ModelAndView showMessage() {
System.out.println("from controller");
return new ModelAndView("hello", "message", message);
}
}
and my pages
1)index.jsp
<html>
<head>
<title>Tutorial | Spring</title>
</head>
<body>
<h4>
Click Here
</h4>
</body>
</html>
2)WEB-INF/pages/hello.jsp ------ The page I cant reach
<html>
<head>
<title>Tutorial | Spring</title>
</head>
<body>
<h4>${message}</h4>
</body>
</html>
console
Jan 28, 2014 8:57:15 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: D:\Programmes\JRE7\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files (x86)\iis express\PHP\v5.4;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Microsoft\Web Platform Installer\;C:\Program Files (x86)\Microsoft SQL ;;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files\Microsoft SQL Server\110\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\ManagementStudio\;C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\PrivateAssemblies\;C:\Program Files (x86)\Microsoft SQL Server\110\DTS\Binn\;C:\Program Files\MySQL\MySQL Server 5.1\bin;C:\Program Files (x86)\nodejs\ ;C:\Program Files\Apache Software Foundation\apache-maven-3.1.1\bin;C:\Program Files (x86)\Java\jre7\bin;C:\Users\Alex\AppData\Roaming\npm\;.
Jan 28, 2014 8:57:15 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:SecondSite' did not find a matching property.
Jan 28, 2014 8:57:15 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Jan 28, 2014 8:57:15 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Jan 28, 2014 8:57:15 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 475 ms
Jan 28, 2014 8:57:15 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Jan 28, 2014 8:57:15 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.47
Jan 28, 2014 8:57:16 PM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
Jan 28, 2014 8:57:16 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
Jan 28, 2014 8:57:16 PM org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization started
Jan 28, 2014 8:57:16 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing Root WebApplicationContext: startup date [Tue Jan 28 20:57:16 EET 2014]; root of context hierarchy
Jan 28, 2014 8:57:16 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/dispatcher-servlet.xml]
Jan 28, 2014 8:57:17 PM org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
INFO: Mapped URL path [/**] onto handler 'org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler#0'
Jan 28, 2014 8:57:17 PM org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization completed in 692 ms
Jan 28, 2014 8:57:17 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'dispatcher'
Jan 28, 2014 8:57:17 PM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: FrameworkServlet 'dispatcher': initialization started
Jan 28, 2014 8:57:17 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing WebApplicationContext for namespace 'dispatcher-servlet': startup date [Tue Jan 28 20:57:17 EET 2014]; parent: Root WebApplicationContext
Jan 28, 2014 8:57:17 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/dispatcher-servlet.xml]
Jan 28, 2014 8:57:17 PM org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
INFO: Mapped URL path [/**] onto handler 'org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler#0'
Jan 28, 2014 8:57:17 PM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: FrameworkServlet 'dispatcher': initialization completed in 178 ms
Jan 28, 2014 8:57:17 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Jan 28, 2014 8:57:17 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Jan 28, 2014 8:57:17 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 2025 ms
The dispatcher servlet is only configured to handle *.htm files, so when accessing /hello it won't work.
Try this:
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
Try with this,
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Put the <mvc:annotation-driven /> in spring configuration.

Spring MVC Simple Project - 404

I new about Spring, i'm trying to create a simple project but i can't figure it out!
I have java JDK 7 up to date.
Apache Tomcat 7 up to date.
Spring 3 framework / Eclipse JUNO 4 integrated.
HERE
it is my project structure (sorry for the external link bur i have not 10 reputation points)
My web.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
My servlet-context.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Scans within the base package of the application for #Components to configure as beans -->
<!-- #Controller, #Service, #Configuration, etc. -->
<context:component-scan base-package="controller" />
<!-- Enables the Spring MVC #Controller programming model -->
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
My HomeController.java file:
package controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* Handles requests for the application home page.
*/
#Controller
public class HomeController {
#RequestMapping(value = "/home")
public String home() {
System.out.println("HomeController: Passing through...");
return "home";
}
}
My problem is that when i call the servlet from the browser (for instance in that way):
http://localhost:8080/SpringMVC/home
i have a HTTP 400 error - description The requested resource is not available.
I suspect is a libraries problem but i put all the Spring libraries (and much more) in WEB-INF/lib. In the Eclipse project obviously i add everything in the classpath.
I paste the Tomcat's localhost log:
*
apr 13, 2013 3:44:43 PM org.apache.catalina.core.ApplicationContext log
INFO: SessionListener: contextDestroyed()
apr 13, 2013 3:44:43 PM org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: contextDestroyed()
apr 13, 2013 3:44:47 PM org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: contextInitialized()
apr 13, 2013 3:44:47 PM org.apache.catalina.core.ApplicationContext log
INFO: SessionListener: contextInitialized()
apr 13, 2013 3:44:47 PM org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: attributeAdded('org.apache.jasper.compiler.TldLocationsCache', 'org.apache.jasper.compiler.TldLocationsCache#189b904')
*
and the Tomcat strERR log:
*
2013-04-13 15:44:46 Commons Daemon procrun stderr initialized
apr 13, 2013 3:44:47 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Apache Software Foundation\Tomcat 7.0\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Program Files\AMD APP\bin\x86;C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\Program Files\NVIDIA Corporation\PhysX\Common;C:\oraclexe\app\oracle\product\10.2.0\server\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\ATI Technologies\ATI.ACE\Core-Static;C:\Program Files\Windows Live\Shared;C:\Program Files\Autodesk\Backburner\;C:\Program Files\QuickTime\QTSystem\;C:\Program Files\Microsoft\Web Platform Installer\;C:\Program Files\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\;C:\Program Files\Windows Kits\8.0\Windows Performance Toolkit\;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;;.
apr 13, 2013 3:44:47 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
apr 13, 2013 3:44:47 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
apr 13, 2013 3:44:47 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 397 ms
apr 13, 2013 3:44:47 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
apr 13, 2013 3:44:47 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.39
apr 13, 2013 3:44:47 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\.metadata
apr 13, 2013 3:44:47 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\docs
apr 13, 2013 3:44:47 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\examples
apr 13, 2013 3:44:47 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\host-manager
apr 13, 2013 3:44:47 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\manager
apr 13, 2013 3:44:47 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\ROOT
apr 13, 2013 3:44:47 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\Servers
apr 13, 2013 3:44:47 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\SpringMVC
apr 13, 2013 3:44:47 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
apr 13, 2013 3:44:47 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
apr 13, 2013 3:44:47 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 540 ms
*
I'm EXHAUSTED, i'm since yesterday to fight with this project, please help me! :)
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</context-param>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
This might probably cause due to lack of needed libraries.Following is a list of libraries which spring includes in it's template project(library versions might differ).
When moving forward you might experience that copying libraries is painful and there must be a better way to do this.
I have prepared a blog post on ,how to easily setup a springMVC project with the help of spring toolsuit plugin for eclipse.Hope it will be a very helpful reference for you.
What is actually in "C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\SpringMVC" directory? Does it have webapp directory structure?
It seems that Tomcat does not detect your appServlet(org.springframework.web.servlet.DispatcherServlet) according to stdErr log.
If you deploy your SpringMVC app correctly, you should see the following messages in the log file:
INFO : org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'appServlet': initialization started
...
...
INFO : org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'appServlet': initialization completed in 725 ms
9 times out of 10, when I see the 404 error with Spring MVC, its usually because I fat-fingered the view name/path returned by my Controller.
Based on your code structure everything looks perfect.
Still if I were you, I would check the System.out.println() value to see whether the controller is really invoked or not and then I will make sure the location of home.jsp is correct.
Try changing the request mapping of controller method to :
#Controller
public class HomeController {
#RequestMapping(value = "home")
public String home() {
System.out.println("HomeController: Passing through...");
return "home";
}
}
Remove / from /home.
Also your web.xml does not seem to have context listener.
<context-param>
<param-name>contextConfigLocation</param-name><param-value>
/WEB-INF/spring/appServlet/spring-security.xml,
/WEB-INF/spring/appServlet/hibernate-config.xml
</param-value>
</context-param>
<!-- you seem to be missing this -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

Spring's ContentLoaderListener not found in the web-app but was found on the system classpath

I am building a GWT app with Spring. I am having some issues to inject a dependency to one of my Servlets, so I am trying to narrow down what can be wrong.
First, when my app starts I get:
[WARN] Server class
'org.springframework.web.context.ContextLoaderListener'
could not be found in the web app, but
was found on the system classpath
[WARN] Adding classpath entry
'file:/home/macarse/.m2/repository/org/springframework/spring/2.5.6/spring-2.5.6.jar'
to the web app classpath for this
session For additional info see:
file:/home/macarse/tpf/eclipse/plugins/com.google.gwt.eclipse.sdkbundle.2.0.4_2.0.4.v201006301309/gwt-2.0.4/doc/helpInfo/webAppClassPath.html
[WARN] Server class
'org.apache.commons.collections.map.CaseInsensitiveMap'
could not be found in the web app, but
was found on the system classpath
[WARN] Adding classpath entry
'file:/home/macarse/tpf/eclipse/plugins/com.google.gwt.eclipse.sdkbundle.2.0.4_2.0.4.v201006301309/gwt-2.0.4/gwt-dev.jar' to the web app classpath for this
session For additional info see:
file:/home/macarse/tpf/eclipse/plugins/com.google.gwt.eclipse.sdkbundle.2.0.4_2.0.4.v201006301309/gwt-2.0.4/doc/helpInfo/webAppClassPath.html
Jul 18, 2010 11:07:00 AM
org.springframework.web.context.ContextLoader
initWebApplicationContext INFO: Root
WebApplicationContext: initialization
started Jul 18, 2010 11:07:00 AM
org.springframework.context.support.AbstractApplicationContext
prepareRefresh INFO: Refreshing
org.springframework.web.context.support.XmlWebApplicationContext#16b904d:
display name [Root
WebApplicationContext]; startup date
[Sun Jul 18 11:07:00 ART 2010]; root
of context hierarchy Jul 18, 2010
11:07:00 AM
org.springframework.context.support.AbstractApplicationContext
obtainFreshBeanFactory INFO: Bean
factory for application context
[org.springframework.web.context.support.XmlWebApplicationContext#16b904d]:
org.springframework.beans.factory.support.DefaultListableBeanFactory#1a8dfb3
Jul 18, 2010 11:07:01 AM
org.springframework.beans.factory.support.DefaultListableBeanFactory
preInstantiateSingletons INFO:
Pre-instantiating singletons in
org.springframework.beans.factory.support.DefaultListableBeanFactory#1a8dfb3:
defining beans []; root of factory
hierarchy Jul 18, 2010 11:07:01 AM
org.springframework.web.context.ContextLoader
initWebApplicationContext
Is that OK?
In my web.xml I have:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:/META-INF/spring-presentation.xml, classpath*:/META-INF/spring-persistence.xml
</param-value>
</context-param>
Is there a way to know if those two xml were loaded?
Scan your application startup logs. You should find logs telling you which context files it loaded in the following format (log pattern might differ based on your log config):
org.springframework.beans.factory.xml.XmlBeanDefinitionReader (315): Loading XML bean definitions from ServletContext resource [/META-INF/spring-presentation.xml]
org.springframework.beans.factory.xml.XmlBeanDefinitionReader (315): Loading XML bean definitions from ServletContext resource [/META-INF/spring-persistence.xml]

Resources