zoukankan      html  css  js  c++  java
  • 如何理解<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>

  • 相关阅读:
    Working with macro signatures
    Reset and Clear Recent Items and Frequent Places in Windows 10
    git分支演示
    The current .NET SDK does not support targeting .NET Core 2.1. Either target .NET Core 2.0 or lower, or use a version of the .NET SDK that supports .NET Core 2.1.
    Build website project by roslyn through devenv.com
    Configure environment variables for different tools in jenkins
    NUnit Console Command Line
    Code Coverage and Unit Test in SonarQube
    头脑王者 物理化学生物
    头脑王者 常识,饮食
  • 原文地址:https://www.cnblogs.com/fifiyong/p/JSP.html
Copyright © 2011-2022 走看看