您的位置:首页 > Web前端 > JavaScript

在jsp页面中使用basePath

2016-01-17 21:15 447 查看
在同一个站点,尽量不要加http地址,尽量不要加绝对路径,一是因为容易暴漏用户,二是部署的时候如果一旦工程名字修改,那一下子要改很多地方。

这里转载两个使用basePath的方法:


在WEB项目中使用basePath(jsp,js)

在JSP中,有相对路径和绝对路径。

相对路径容易理不清,而导致无法正确加载资源文件。

So,在做WEB项目时,建议都统一使用绝对路径。

一 JS中使用basePath

<code class="hljs scala has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/*****basePath统一设置*******/</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> curPath = window.document.location.href;
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> pathName = window.document.location.pathname;
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> basePath;
basePath = curPath.substring(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,curPath.indexOf(pathName)) + <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/"</span>;
</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>


或者:

var location = (window.location+'').split('/');

var basePath = location[0]+'//'+location[2]+'/'+location[3];

var url = basePath + '/js/xxx.js';

js中即可使用basePath + “URL”来使用了。

二 JSP 绝对路径

共用jsp文件:如 WEBROOT/common/basePath.jsp。
<code class="hljs xml has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"><span class="vbscript" style="box-sizing: border-box;"><%
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">String</span> path = <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">request</span>.getContextPath();
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">String</span> basePath = <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">request</span>.getScheme() + <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"://"</span>
+ <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">request</span>.getServerName() + <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">":"</span> + <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">request</span>.getServerPort()
+ path + <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/"</span>;
%></span>
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">script</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">type</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"text/javascript"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">src</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"<%=basePath %>js/path.js"</span>></span><span class="javascript" style="box-sizing: border-box;"></span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">script</span>></span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"><!-- 其他公共资源 --></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li></ul>


在每个jsp页面,引用公共页面
<code class="hljs mel has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"><<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%@</span>include <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">file</span>=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/common/common.jsp"</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>


在jsp中,使用路径前加
<%=basePath %>+url

<code class="hljs rust has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"><link rel=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"stylesheet"</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">type</span>=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"text/css"</span> href=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"<%=basePath %>css/styles.css"</span>></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>


或设置当前jsp页面的默认地址为basePath
<code class="hljs r has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"><head>
<base href=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"<%=basePath %>"</span> />

<link rel=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"stylesheet"</span> type=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"text/css"</span> href=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"css/styles.css"</span>>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">...</span></code>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: