[英]Java: Returning a relative URL from a Java class?
I have some images stored in /images the problem is that i wish to return a url like so from a class. 我在/ images中存储了一些图像,问题是我希望从类中返回一个url。
/images/test.png
The problem is that in my development machine my url is in the form of 问题是在我的开发机器中,我的网址格式为
http://localhost/myApp
and in production its in the form of 并在生产中以
http://www.mysite.com
so i can't just return /images as this doesn't work on the development machine 所以我不能只返回/ images,因为这在开发机器上不起作用
I suppose what i am trying to do in Java is the same as teh JSTL tag does 我想我在Java中尝试做的与JSTL标签一样
<c:url
So when i am in my development machine it would return 因此,当我在开发机器中时,它将返回
/myApp/images/test.png
and in production 并在生产中
/images/test.png
can anyone help? 有人可以帮忙吗?
Thanks in advance 提前致谢
${request.contextPath}/images/foo
您可能正在寻找上下文路径 。
您需要在ServletRequest
上使用getContextPath()
方法,然后附加其余路径:
String url = request.getContextPath() + "/images/test.png";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.