JS中怎么获得项目的根目录名

一、在当前JSP中的java代码中获取

java代码


String path = request.getContextPath();

String basePath = request.getScheme() +"://"

+ request.getServerName() +":"+ request.getServerPort()

+ path +"/";

再在JS中调用:


var basePath ='<%=basePath%>';

alert(basePath);//http://localhost:8080/

二、在跳入当前页面的action中获取根路径,放入request中,再在JS中获取


String path = request.getContextPath();

String basePath = request.getScheme() +"://"

+ request.getServerName() +":"+ request.getServerPort()

+ path +"/";

request.setAttribute("basePath", basePath);

再在JS中通过EL表达式调用:


var basePath = ${'basePath'};

alert(basePath);//http://localhost:8080/

你可能感兴趣的:(JS中怎么获得项目的根目录名)