假设项目的路径如下:
http://localhost:8080/project1/当前页面 http://localhost:8080/project1/test/test1.jsp
我要在test1.jsp页面中用js 修改document.href.location属性为test2/test1.jsp
我的写法如下: document.href.location="/project1/test2/test2.jsp"
以上写法是可以的,但缺点是必须写死 /project1 这相根目录,当我的项目改名为project2 的时候,所有相关的js都要修改.
所以,我的问题是,js 能否用相对路径访问project1/test2/test.jsp?
我尝试过的做法: document.href.location="/test2/test2.jsp" 或 document.href.location="../test2/test2.jsp" 都不行,提示找不到这个url
http://localhost:8080/project1/当前页面 http://localhost:8080/project1/test/test1.jsp
我要在test1.jsp页面中用js 修改document.href.location属性为test2/test1.jsp
我的写法如下: document.href.location="/project1/test2/test2.jsp"
以上写法是可以的,但缺点是必须写死 /project1 这相根目录,当我的项目改名为project2 的时候,所有相关的js都要修改.
所以,我的问题是,js 能否用相对路径访问project1/test2/test.jsp?
我尝试过的做法: document.href.location="/test2/test2.jsp" 或 document.href.location="../test2/test2.jsp" 都不行,提示找不到这个url
request.getContextPath()+路径
request.getContextPath()+"/test2/test2.jsp"
这样可以<script type="text/javascript">
var pathname=window.location.pathname;
var arr=pathname.split("/");
var proName=arr[1];
window.location.href="/"+proName+"/test2/test2.jsp";
</script>
var url = "***.do?method=abc"
<script language="javascript">
var baselocation='<%=request.getContextPath()%>';
</script>
后面你就可以用它了
这个是标准用法吧,struts in action中推荐的