JSTL URL tag doesn't work on Heroku

2019-05-22 22:20发布

I have a Simple application which I've deployed locally (in a Tomcat), as well as on Google App Engine and Heroku.

The app uses JSTL tags, and they all work fine in all deployments, except the URL tag (<c:url value="/someUrl"/>) which, only on Heroku, prepends an extra forward slash which screws everything up. Here's what I mean:

<c:url value='/laundryLists/search.htm'/>

In my local Tomcat, where the app is deployed with a context of "testApp" the above code snipped will genreate the following string in the page:

"/testApp/laundryLists/search.htm"

which is correct.

In my GAE deployed app, which sits at address testApp.appspot.com, the above code snippet generates:

"/laundryLists/search.htm"

which is correct, because here the app context is "/" (root)

In my Heroku version, which is deployed at "testApp.herokuapp.com"

The exact same code generates:

"//laundryLists/search.htm" (notice two forward slashes at the beginning)

which is wrong, because if placed in a link, such as

<a href="<c:url value='/laundryLists/search.htm'/>">whatever</a>

the link URL will be:

http://laundryLists/search.htm

which is obviously wrong.

It's worth mentioning that other JSTL tags (such as "forEach") work fine in Heroku.

So my question is, why is this happening on Heroku, and how can I fix it (other than hardcoding the URLS)?

2条回答
Root(大扎)
2楼-- · 2019-05-22 22:46

When deploying WAR files to Heroku, the WAR file is launched with Webapp Runner, which is just a thin wrapper around Tomcat to allow it to be run easily from the command line. In the currently deployed version of Webapp Runner, the default context path is /, which is where the extra slash is coming from. To set the path to something else (including empty string), you can use the --path parameter. To set this for a Heroku app using WAR deployment, you can set it in the WEBAPP_RUNNER_OPTS config var like thisr:

heroku config:add WEBAPP_RUNNER_OPTS="--path ''"
查看更多
贼婆χ
3楼-- · 2019-05-22 22:54

To add to what what ryanbrainard has said and in case anyone had the same trouble I did. I found that trying to set this flag at the command line had no effect when I deployed, perhaps since I was deploying when performing git push and not deploying a war using the command line.

I had to update the /Procfile in my project and add the options there. So mine then looked like: web: java $JAVA_OPTS -Dspring.profiles.active=prod -jar target/dependency/webapp-runner.jar --port $PORT --path '' target/*.war

This solved the issue for me.

查看更多
登录 后发表回答