我在项目开发环境中是这样配置的:
首先,在tomcat的conf下的server.xml中<Context标签里面配置上你自己项目的path,如: path="/Platform"。
其次,编写自己获取相对路径的class,如:
package platform.commons.utils;
import javax.servlet.http.HttpServletRequest;
public class ViewUtil
{
public static String getRequestPath(HttpServletRequest request)
{
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
return basePath;
}
}
其中,request.getScheme();返回的协议名称,一般默认是http。
request.getServerName();返回的是你浏览器中显示的主机名,如localhost。
request.getServerPort();获取服务器端口号,如8080。
path就是获取你上面配置的“<Context标签里面配置上你自己项目的path,如: path="/Platform"。”
最后,在项目中使用:
jsp头加入<%@ page import="platform.commons.utils.ViewUtil"%>
head标签里面加入<base href="<%=ViewUtil.getRequestPath(request)%>">就可以了,如: http://localhost:8080/Platform/com/test/index.jsp