如何理解<base href="<%=basePath%>" ---转载

原文链接http://316325524.blog.163.com/blog/static/6652052320111118111620897/

"base href "

今天在写一个JSP网页的时候,href不能用了,所有href鼠标放上去前面现实的都是“http:///”,竟然有三个“/”,而且前面也没有显示“localhost:8080”找了大半天找不出来,最后才发现不小心将Eclipse自动生成的下面两行代码误删了,
< %
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

  我的补充:

    为了加深理解,将上面代码分块逐个打印输出:

<%out.println("request.getScheme()==="+request.getScheme()+"<br/>");
    out.println("request.getServerName()==="+request.getServerName()+"<br/>");
    out.println("request.getServerPort()==="+request.getServerPort()+"<br/>");
    out.println("request.getContextPath()==="+path);
   %>

页面输出结果是:

request.getScheme()===http
request.getServerName()===localhost
request.getServerPort()===8080
request.getContextPath()===/news

这下就知道basePath是什么东西了。

我一直没有使用path 和 basepath ,为什么会这样呢,最后终于发现,原来在 <head></head>中,有一句 <base href="<%=basePath%>"> 使用了basepath,就是因为这句,所有的链接才不能使用了。看来问题就出在base href 上了,顾名思义,base href不是就是基链接嘛。
上网搜索了一下,原来base href 不单单只有这么点作用,尤其在框架中。

资料如下:

base标记是一个基链接标记,是一个单标记。用以改变文件中所有连结标记的参数内定值。它只能应用于标记<head>与</head>之间。
你网页上的所有相对路径在链接时都将在前面加上基链接指向的地址。

重要属性:
href
设定前缀的链接地址

target
设定文件显示的窗口,同a标记中的target

简单例子:

< html>
< head>
< base href="http://www.baidu.com" target="_blank">
< meta http-equiv="Content-Type" content="text/html; charset=gb2312">
< title>base标记</title>
< link rel="Shortcut Icon" href="ani.CUR">
< /head>
< body>
< a href="77.htm" target="_self">77</a>
< a href="88.htm">88</a>
< /body>
< /html>
当点了链接后,跳出的文件是http://www.baidu.com/77.htm或http://www.baidu.com/88.htm,它就是在这些相对路径的文件前加上基链接指向的地址。如果目标文件中的链接没有指定target属性,就用base标记中的target属性。
常在框架结构中用,如左右两个框架,把左边的框架中文件里的连接都显示在右边的框架里。只要用base标记,把其target属性值写为右框架名称,这就不用再为左框架里的文件中的每一个连接都指定target属性。

当使用时,BASE 元素必须出现在文档的 HEAD 内,在任何对外部源的引用之前。

此元素在 Microsoft? Internet Explorer 3.0 的 HTML 中可用,在 Internet Explorer 4.0 的脚本中可用。

此元素不会被渲染。

此元素不需要关闭标签。

这个标签的用处是解决编程时候的相对路径问题,比如有的cms,因为每页路径不一样,他就给你生成<a href="/sdsd/dsd.html">sddsds</a>之类的,如果我在本地调试,肯定会在本地开一个目录的,这样就乱了,你可以把它生成相对路径,如<a href="sdsd/dsd.html">sddsds</a>,只要在head部分加上<base href=http://localhost/abc/>即可。

所以说,这个标签主要为了解决web编程的时候一些相对路径的问题。

当然,这个base还有一个用法,如在head部分加上这么一行: <base href="_blank"> ,就是默认所有链接在新窗口打开。

还可以这么理解:
这是设置基础路径的,basepath为变量
简单的静态网页的话你设置比如:<base href="http://www.baidu.com">,那你下面的href属性就会以你上面设的为基准,如:<a href="http://www.baidu.com/xxx.htm"></a>你现在就只需要写<a href="xxx.htm"></a>

时间: 2024-08-07 17:42:20

如何理解<base href="<%=basePath%>" ---转载的相关文章

如何理解&lt;base href=&quot;&lt;%=basePath%&gt;&quot;

原文链接http://316325524.blog.163.com/blog/static/6652052320111118111620897/ "base href " 今天在写一个JSP网页的时候,href不能用了,所有href鼠标放上去前面现实的都是“http:///”,竟然有三个“/”,而且前面也没有显示“localhost:8080”找了大半天找不出来,最后才发现不小心将Eclipse自动生成的下面两行代码误删了,<%String path = request.getC

如何理解jsp页面的“&lt;base href=&quot;&lt;%=basePath%&gt;&quot;&gt;”

jsp文件里通常有以下代码: 这里的<base href="<%=basePath%>">是什么意思呢? 我在W3School网站上查了html中的base标签,解释如下: <base> 标签为页面上的所有链接规定默认地址或默认目标. 通常情况下,浏览器会从当前文档的 URL 中提取相应的元素来填写相对 URL 中的空白. 使用 <base> 标签可以改变这一点.浏览器随后将不再使用当前文档的 URL,而使用指定的基本 URL 来解析所有

&lt;base href=&quot;basepath&quot;&gt; 的理解

"base href " 今天在写一个JSP网页的时候,href不能用了,所有href鼠标放上去前面现实的都是"http:///",竟然有三个"/",而且前面也没有显示"localhost:8080"找了大半天找不出来,最后才发现不小心将Eclipse自动生成的下面两行代码误删了,<%String path = request.getContextPath();String basePath = request.getS

JSP中&lt;base href=&quot;&lt;%=basePath%&gt;&quot;&gt;作用

通常在JSP页面开通有如下代码: 1 <% 2 String path = request.getContextPath(); 3 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 4 5 %> 这段代码的意思是获取当前项目的路径,如:http://localhost:808

JSP中&lt;base href="&lt;%=basePath%&gt;"&gt;作用

通常在JSP页面开通有如下代码: Java代码   <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> 这段代码的意思是获取当前项目的路径,如:http://localhost:8080

request.getScheme() 的使用方法以及request的其他类似函数,&lt;base href=&quot;&lt;%=basePath%&gt;&quot;&gt;用法

request.getSchema()可以返回当前页面使用的协议,http 或是 https; request.getServerName()可以返回当前页面所在的服务器的名字; request.getServerPort()可以返回当前页面所在的服务器使用的端口; request.getContextPath()可以返回当前页面所在的应用的名字; 在编写jsp页面时,总会遇到各种文件的路径问题,其中遇到了如下代码: <% String path = request.getContextPath

JSP中&lt;base href=&quot;&lt;%=basePath%&gt;&quot;&gt;的作用

来源于:http://fanshuyao.iteye.com/blog/2097229 首先了解是什么是<base href=""> <base href="value">为页面上所有相对 URL 规定基准 URL: Html代码   <head> <base href="http://www.w3school.com.cn/i/" /> </head> <body> <

jsp--&lt;base href=&quot;&lt;%=basePath%&gt;&quot; /&gt;

如果没有这个 <base href="<%=basePath%>" />   ,那么该jsp页面是无法加载 <script type="text/javascript" src="scripts/jquery-1.9.1.min.js"></script> ,

window.location.href = basePath + "paper/deleteExpertComment.action?expertId="+$(this).prev().val();

window.location.href = basePath + "paper/deleteExpertComment.action?expertId="+$(this).prev().val(); window.location.href = basePath + "paper/deleteExpertComment.action?expertId="+$(this).prev().val();