Unable to load spring application Context - spring-mvc

I want to load myapp-servlet.xml as my web application context. I have neither defined spring contextLoaderListner not have defined context params, only the dispatcher servlet is defined.
<display-name>myapp</display-name>
<description/>
<servlet>
<servlet-name>myapp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myapp</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
config file is present in /WEB-INF/myapp-servlet.xml and I'm expecting it to be loaded. But I'm getting the following exception
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/applicationContext.xml]
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
I'm want to know why the default applicationContext.xml file is looked for even when context-param and contextLoaderListener is not defined?

Just add the appropriate context-param for your Spring config file:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/myapp-servlet.xml</param-value>
</context-param>
There is probably a default config file location in the Spring code... Seems like a good opportunity to UTSL...

Just add
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
to your web.xml

Related

'No suitable driver' exception implementing Olap4J XMLA Server

I'm trying to implement olap4j-xmlaserver to access Mondrian via HTTP/XMLA. I compiled the project, created a war file (including a web.xml file) and deployed it in Tomcat 7. Problem is that I'm getting an exception when the pivot client application sends an XMLA request:
Caused by: org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of class 'com.mysql.jdbc.Driver' for connect URL 'jdbc:mondrian:Jdbc=jdbc:mysql://localhost:3306/foodmart?user=root&password=xxx;Catalog=/WEB-INF/FoodMart.xml'
at org.apache.commons.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1452)
at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)
at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
at mondrian.xmla.impl.Olap4jXmlaServlet$Olap4jPoolingConnectionFactory.getConnection(Olap4jXmlaServlet.java:300)
at mondrian.xmla.XmlaHandler.getConnection(XmlaHandler.java:2917)
... 33 more
Caused by: java.sql.SQLException: No suitable driver at java.sql.DriverManager.getDriver (DriverManager.java:315)
at org.apache.commons.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1437)
... 37 more
The web.xml I put together (I couldn't find a sample in the olap4j-xmlaserver project). I'm sure this web.xml file needs to be fixed to solve the problem, especially the connection string:
<web-app ....
<display-name>olap4j-xmlaserver</display-name>
<description>
Olap4J XML/A server
</description>
<servlet>
<servlet-name>xmla</servlet-name>
<servlet-class>mondrian.xmla.impl.Olap4jXmlaServlet</servlet-class>
<init-param>
<param-name>OlapDriverClassName</param-name>
<param-value>com.mysql.jdbc.Driver</param-value>
</init-param>
<init-param>
<param-name>OlapDriverConnectionString</param-name>
<param-value>
jdbc:mondrian:Jdbc=jdbc:mysql://localhost:3306/foodmart?user=root&password=xxx;Catalog=/WEB-INF/FoodMart.xml
</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>xmla</servlet-name>
<url-pattern>/xmla</url-pattern>
</servlet-mapping>
</web-app>
Any ideas how to fix the web.xml configuration or pointers to relevant documentation will be greatly appreciated.
You are missing the Java driver for MySQL in your classpath. Add it to your webapp in the folder named 'WEB-INF/lib'.

Web.xml servlet-class references to non-existent class "ClassName"

I am working on a project and hit a brick wall. I couldn't find any resources or bug reports to solve this issue. I have a maven project setup as maven-archetype-webapp. I am new to JEE and it took me sometime to get this far but I have a better understanding of how some of this works. I have hit a brick wall and don't know how to solve the problem below.
I get the following warnings within the web.xml file
servlet-class references to non-existent class "servlets.RegisterServlet"
servlet-class references to non-existent class "servlets.HomePageServlet"
file name references to "index" that does not exist in web content
I get the following error in WildFly 10.x.
Caused by: java.lang.ClassNotFoundException: servlets.RegisterServlet from [Module "deployment.PracticeProject.war:main" from Service Module Loader]
at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:198)
at org.jboss.modules.ConcurrentClassLoader.performLoadClassUnchecked(ConcurrentClassLoader.java:363)
at org.jboss.modules.ConcurrentClassLoader.performLoadClass(ConcurrentClassLoader.java:351)
at org.jboss.modules.ConcurrentClassLoader.loadClass(ConcurrentClassLoader.java:93)
at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService.createServletConfig(UndertowDeploymentInfoService.java:723)
... 6 more
The java classes appear in the directory structure of the war so its packaging it correctly. For the entities, session beans and servlets I have a scope of provided for the dependencies of the pom.xml file.
My directory structure is as follows:
My web.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>PracticeProject</display-name>
<servlet>
<servlet-name>RegisterServlet</servlet-name>
<servlet-class>servlets.RegisterServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RegisterServlet</servlet-name>
<url-pattern>/register</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>HomePageServlet</servlet-name>
<servlet-class>servlets.HomePageServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HomePageServlet</servlet-name>
<url-pattern></url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index</welcome-file>
</welcome-file-list>
</web-app>
edit:
CHKJ3013E: The URL Mapping "" is invalid. web.xml /PracticeProject/src/main/webapp/WEB-INF org.eclipse.jst.j2ee.webapplication.internal.impl.ServletMappingImpl#53667dce (urlPattern: , name: null) Validation Message

How does Spring 3.1 Java based configuration work

Just a general question, when you define a Java based configuration web app. Ie.e have a class for : ApplicationContext and a WebApplicationInitializer class.
How does Spring know it has to load the beans, as no xml config files exists.. how does tomcat know anything about the webapp without a web.xml
Its a newbie question.. i appreciate that. :)
See this blog post from SpringSource blog, important part about web.xml has an example, basically you point to JavaConfigWebApplicationContext instead of default XmlWebApplicationContext in DispatcherServlet's <init-param>:
<web-app>
<!-- Configure ContextLoaderListener to use JavaConfigWebApplicationContext
instead of the default XmlWebApplicationContext -->
<context-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.config.java.context.JavaConfigWebApplicationContext</param-value>
</context-param>
<!-- Configuration locations must consist of one or more comma- or space-delimited
fully-qualified #Configuration classes -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>example.RootApplicationConfig</param-value>
</context-param>
<!-- Bootstrap the root application context as usual using ContextLoaderListener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Declare a Spring MVC DispatcherServlet as usual -->
<servlet>
<servlet-name>dispatcher-servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- Configure DispatcherServlet to use JavaConfigWebApplicationContext
instead of the default XmlWebApplicationContext -->
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.config.java.context.JavaConfigWebApplicationContext</param-value>
</init-param>
<!-- Again, config locations must consist of one or more comma- or space-delimited
and fully-qualified #Configuration classes -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>example.web.WebBeansConfig</param-value>
</init-param>
</servlet>
</web-app>
I have a VERY GOOD WAY to help you learn Spring MVC if you have Maven up and running.
IF SO: go to your command line (Cygwin) I use...
mvn archetype:generate
It will ask for an 'archetype number'. For you... type 16
Enter the group ID which is just the main package.
Enter Artifact ID which is your project name.
SNAP-SHOT --- just press enter and same with version.
Package - is the same as your group ID name. EX: com.spring
Confirm it by entering the letter 'y' and press enter.
DO all of the above after your are in your workspace directory. That way it is created there.
You can do "mvn eclipse:eclipse" to load it in Eclipse OR you can just import it. I prefer the old fashioned importing an existing project.
Everything will be 'already' set up for you in terms of ALL configuration (Java-Based) which is good for you. It will have all the Maven dependencies you need as well already in your pom.xml. You can add or take from it if you want.
The point here is that you will have a running project already and you can play with it from there. I create all my projects like this at first and erase what I don't need and add what I do and then go from there.
Good luck!!!
Anywho... add this to your web.xml. This will help you in your answer. Research this below:
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

Tomcat7 deploy Fail: org.apache.catalina.LifecycleException: Failed to start component /w org.springframework.web.servlet.DispatcherServlet

I have a very sample web.xml written below:
The problem is...If I remove org.springframework.web.servlet.DispatcherServlet section, I can successfully deploy my project in Tomcat7 as a simple JSP-Servlet app. However, once I use Spring MVC, my deployment will FAIL - Encountered exception org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/WebMVCProj2]]
How can I fix this error?
<?xml version="1.0" encoding="UTF-8"?>
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
Spring Web MVC Application
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/HelloServlet</url-pattern>
</servlet-mapping>
<!-- I can remove the part below to make deployment successful -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
I add Spring MVC dependency from Enterprise Bundle Repository (EBR). I also use Eclipse Dynamic Web Module 3.0 which Tomcat7 should support. My jre is 1.6.x 64bits used by Tomcat and my project. I also use Web Deployment Assembly in Eclipse.
Here is my complete exception:
SEVERE: Error deploying web application archive E:\MyServers\apache-tomcat-7.0.30\webapps\WebMVCProj2.war
java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/WebMVCProj2]]
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:904)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:618)
at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:963)
at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1600)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
You should have applicationContext.xml in web-inf folder or you have to define contextConfigLocation in your web.xml
<!-- Spring Context -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/application-contexts/*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
It would be more helpful, if you could post exception trace.
you can check your servlet class is having any annotation like #WebServlet(". I resolved the similar issue by removing the annotation in servlet class.

What is causing the mapping failure in this Spring MVC app?

I'm trying to get a very simple Spring #mvc app to work and I'm running into what appears to be a mapping error.
From web.xml:
<servlet>
<servlet-name>works</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>works</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
The controller:
#Controller
#RequestMapping("/test")
public class TESTController {
private static Logger appLogger = Logger.getLogger("AppLogFile");
public String serviceRequest( Model model)
{
appLogger.info("======================= TESTController GET ===============================");
model.addAttribute("returnString","TESTController handled the request") ;
return "SingleStringView";
}
works-servlet.xml:
<context:component-scan base-package="com.ami.dbconnect.controller" />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<bean id="TESTController" class="com.ami.dbconnect.controller.TESTController"></bean>
<!-- view resolver not shown -->
The app is deployed to Tomcat 7 at /webapps/works. The Tomcat file structure is:
webapps
/works
/WEB-INF
/classes
/lib
I'm trying to invoke the controller with the url: localhost:8080/works/test
In tomcat7-stdout I see:
1106 [pool-2-thread-1] DEBUG org.springframework.web.servlet.DispatcherServlet - Published WebApplicationContext of servlet 'works' as ServletContext attribute with name [org.springframework.web.servlet.FrameworkServlet.CONTEXT.works]
1106 [pool-2-thread-1] INFO org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'works': initialization completed in 728 ms
1106 [pool-2-thread-1] DEBUG org.springframework.web.servlet.DispatcherServlet - Servlet 'works' configured successfully
14068 [http-apr-8080-exec-2] DEBUG org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'works' processing GET request for [/works/test]
14071 [http-apr-8080-exec-2] WARN org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/works/test] in DispatcherServlet with name 'works'
So my question (finally!):
Is Spring not recognizing the annotations in the controller? If not, what could be wrong in this simple set up?
Thanks for any help or advice,
beeky
You need to move #RequestMapping annotation to your serviceRequest method.
#RequestMapping at class level may be used to specify common path prefix for all #RequestMapping-annotated of that class, but it doesn't take any effect without annotations at method level.
Maybe problem is in the servlet mapping section?
Try to change:
<servlet-mapping>
<servlet-name>works</servlet-name>
<url-pattern>/works/*</url-pattern>
</servlet-mapping>
And try again.

Resources