Popular Posts

Tuesday, September 13, 2011

Spring: MVC 3 Showcase: Git, Maven & Eclipse

Step by Step Guide to import 'Spring MVC 3 Showcase' sample from Git repository into Eclipse with Maven

This page is put together to facilitate those folks who are facing some issues running & integrating Springsource Spring MVC showcase sample code into eclipse with maven.

Step1: Get the code
download code by clicking download link from https://github.com/SpringSource/spring-mvc-showcase

Clone from Git repository via Git command line as follows

$ git clone git://github.com/SpringSource/spring-mvc-showcase.git

If this is your first time using Github, review http://help.github.com to learn the basics.

Clone from Git repository via EGit plugin for eclipse 
For installation instruction and usage for Egit plugin see this tutorial which gives nice overview.

Step2: Import the code into Eclipse

In eclipse click File and then Import...
Select "Existing Maven Project" as below

Note: If you don't see "Maven" category in Import dialog above consider downloading m2e (M2Eclipse) plugin for eclipse
Now Provide the location of downloaded code repository and then click Finish

This will import nice Maven project into eclipse but this is not ready yet to test within eclipse using tomcat. In order to deploy this project into Tomcat within Eclipse the project has to be a "Web Project" in nature. This imported project is just a Java Project with Maven support in eclipse.

Let make this project a "Web Project" as follow

Step3: Convert Project into Web Project

Right click on the spring-mvc-showcase project in eclipse and then Properties

Then Select "Project Facets" as below...

As you can see this project is not yet configured to use facets. lets click on the link "convert to faceted form..." as in the above dialog box

Now select "Dynamic Web Module" from Project Facets and then Click on the Link "Further configuration available" link at the bottom as per the below screenshot

Now change "Content directory" of Web Module to "/src/main/webapp" to comply with Maven structure.

Also select "Apache Tomcat..." from Runtimes tab on the right column as above screen.
Click OK to finish configuring project facets and converting our project to a dynamic web project.

Now this project is ready to be deployed into tomcat within eclipse.
Right click on the project and select Run as --> Run on server
Select Tomcat and click Finish as below screen

At this stage the project should be added and deployed to Tomcat server and could be accessed via URL http://localhost/spring-mvc-showcase/

Note: In my case I got the below error while Tomcat's attempt to deploy the application. I thought its worth mentioning, may be this help others to resolve the same issue

SEVERE: Error configuring application listener of class org.springframework.web.context.ContextLoaderListener
java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
	at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1664)
	at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1509)
	at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:406)
	at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:388)
	at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:117)
	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4268)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:4771)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:138)
	at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:990)
	at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:772)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:138)
	at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:990)
	at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:275)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:138)
	at org.apache.catalina.core.StandardService.startInternal(StandardService.java:424)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:138)
	at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:648)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:138)
	at org.apache.catalina.startup.Catalina.start(Catalina.java:576)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
	at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:415)
Sep 12, 2011 2:37:04 AM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Skipped installing application listeners due to previous error(s)
Sep 12, 2011 2:37:04 AM org.apache.catalina.core.StandardContext startInternal
SEVERE: Error listenerStart
Sep 12, 2011 2:37:04 AM org.apache.catalina.core.StandardContext startInternal
SEVERE: Context [/spring-mvc-showcase] startup failed due to previous errors

So this suggests that Tomcat could not find Spring's ContextLoaderListner class. which is kinda odd. lets see whats happening under the hood.
As all our project dependencies are handled by Maven we should not be worried about handling & deploying dependencies into Tomcat (servlet container) within eclipse. Lets first check we have spring related dependencies added to the Maven POM file.
Now as we see that pom.xml already contains spring dependencies, the next clue which comes into mind is that to check deployment assembly of the project and make sure Maven Libraries are included.

Right click on the project, then "Properties"
Now check "Deployment Assembly"
Yups, that was the issue.... Maven Dependencies were missing from Deployment Assembly.

Lets add Maven Library into Deployment Assembly as below screen

Click Apply and OK

Now lets restart the application by right clicking on the tomcat server and then click "Restart"
This should work now but again in my case with Tomcat version 7, I got below error...

SEVERE: Servlet.service() for servlet jsp threw exception
org.apache.jasper.JasperException: Unable to read TLD "jndi:/localhost/spring-mvc-showcase/WEB-INF/lib/jstl-impl-1.2.jar" from JAR file "jndi:/localhost/spring-mvc-showcase/WEB-INF/lib/jstl-impl-1.2.jar": org.apache.jasper.JasperException: Failed to load or instantiate TagLibraryValidator class: org.apache.taglibs.standard.tlv.JstlCoreTLV
	at org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:51)
	at org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:409)
	at org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:181)
	at org.apache.jasper.compiler.TagLibraryInfoImpl.(TagLibraryInfoImpl.java:179)
	at org.apache.jasper.compiler.Parser.parseTaglibDirective(Parser.java:387)
	at org.apache.jasper.compiler.Parser.parseDirective(Parser.java:451)
	at org.apache.jasper.compiler.Parser.parseElements(Parser.java:1401)
	at org.apache.jasper.compiler.Parser.parse(Parser.java:130)
	at org.apache.jasper.compiler.ParserController.doParse(ParserController.java:238)
	at org.apache.jasper.compiler.ParserController.parse(ParserController.java:101)
	at org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:198)
	at org.apache.jasper.compiler.Compiler.compile(Compiler.java:360)
	at org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
	at org.apache.jasper.compiler.Compiler.compile(Compiler.java:327)
	at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:594)
	at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:314)
	at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
	at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
	at ....

Now this time the issue is different.
As we already checked that the Maven dependencies are already there but why Tomcat is complaining Failed to load instance of TagLibrary.

I suspect there might be a conflict in the libraries. lets compared the default Tomcat's (version 7 in my case) loaded libraries with Maven Libraries.

Got it, we have a duplicate library loaded named "jsp-api-2.1.jar" in Maven pom.
Lets exclude it by right clicking on the above duplicate library in the Maven dependency tree and select Maven-->Exclude Maven Artifact...

Restart Tomcat one more time.
Congratulations!, we got the "spring-mvc-showcase" application working.... 
successfully deployed to Tomcat version 7 within eclipse.

I hope this help some folks who are trying hard to get started with spring-mvc and related tools like GIT etc with eclipse.
Happy coding and exploring the world of SpringMVC!!!

Note: Here is the link to Spring MVC3-showcase blog

No comments: