Sunday, October 26, 2014

pageContext.request.contextPath returns "/" instead of empty string [FIXED]

Recently I was creating a Spring application hosted on Heroku and realized that ${pageContext.request.contextPath} was returning "/" instead of "" for application deployed on root.

When "" (Empty String) is returned:

Ideally the contextPath should not end with slash "/" because we will be using it in the urls like given below:

 <link href="${pageContext.request.contextPath}/css/bootstrap.min.css" rel="stylesheet">  

so here url in my local machine would map to something like

 <link href="/my-app-name/resources/css/bootstrap.min.css" rel="stylesheet">  

and when run from the remote server, it should map to something like

 <link href="/resources/css/bootstrap.min.css" rel="stylesheet">  

When "/" is returned:

When the contextPath wrongly returns "/" my urls are wrongly mapped like below (with double slash //):

 <link href="//resources/css/bootstrap.min.css" rel="stylesheet">  

Fix:

After a lot of research, I found the issue. Heroku uses webapp-runner. Version 7.0.34.0 returns "/" as context path and version 7.0.34.1 returns "". So upgrading webapp-runner to 7.0.34.1 solved the issue for me.

Saturday, October 25, 2014

FIX: Missing web.xml issue on Heroku when trying Spring Java Based Configuration

If you are trying to use Spring Java Based Configuration instead of web.xml and other xml configuration files on application hosted on Heroku, you might come across the following error:

 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.2:war (default-war) on project sg-compare: Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode) -> [Help 1]

Fix:

In your pom.xml add

  <failOnMissingWebXml>false</failOnMissingWebXml>  

to the maven-war-plugin

Example:

Replace the following code

 <plugin>  
         <artifactId>maven-war-plugin</artifactId>  
         <version>2.2</version>  
 </plugin>  


with the following:

 <plugin>  
         <artifactId>maven-war-plugin</artifactId>  
         <version>2.2</version>  
         <configuration>  
                <failOnMissingWebXml>false</failOnMissingWebXml>  
         </configuration>  
 </plugin>