Jsp页面常见代码:
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> base href="<%=basePath%>"> <title>My JSP 'test.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <%=request.getAttribute("yfddtjbData") %> <input type="button" value="打印" onclick="javascript:print();"/> </body> </html>
一、解释<base href="<%=basePath%>">
base标记是一个基链接标记,是一个单标记。用以改变文件中所有连结标记的参数内定值。它只能应用于标记<head>与</head>之间。
你网页上的所有相对路径在链接时都将在前面加上基链接指向的地址。
重要属性:
href
设定前缀的链接地址
target
设定文件显示的窗口,同a标记中的target(如果目标文件中的链接没有指定target属性,就用base标记中的target属性。)
常在框架结构中用,如左右两个框架,把左边的框架中文件里的连接都显示在右边的框架里。只要用base标记,把其target属性值写为右框架名称,这就不用再为左框架里的文件中的每一个连接都指定target属性。
二、base href标记其他说明:
1、当使用时,BASE 元素必须出现在文档的 HEAD 内,在任何对外部源的引用之前。
2、此元素在 Microsoft? Internet Explorer 3.0 的 HTML 中可用,在 Internet Explorer 4.0 的脚本中可用。
3、此元素不会被渲染。
4、此元素不需要关闭标签。
5、base标签其实还是有用处的,比如<base href="_blank">将使得本页所有的链接都在新页打开,这样就不必在每个链接处都以上target="_blank" 了。
IE7中注意:
1 、<html>之前不能加入额外的输出字符。否则IE7就对 base 不进行解析。
2、< base >是一个链接基准标记,用以改变文件中所有链接标记的参数默认值。它只能适应于文件的开头部分,即标记< head >与< /head >之间。
二、实例
<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="stylesheet" type="text/css" href="styles.css"> </head> <body> <a href="1.htm" target="_self">测试链接1</a> <a href="2.htm">测试链接2</a> </body> </html>
三、链接结果
http://www.baidu.com/1.htm //当前页打开 http://www.baidu.com/2.htm //弹出页打开