发表人:ricelane | 发表时间: 2006年九月12日, 09:12
- 事件源对象
event.srcElement.tagName
event.srcElement.type - 捕获释放 event.srcElement.setCapture();
event.srcElement.releaseCapture(); - 事件按键
event.keyCode
event.shiftKey
event.altKey
event.ctrlKey - 事件返回值
event.returnValue - 鼠标位置 event.x
event.y - 窗体活动元素 document.activeElement
- 绑定事件
document.captureEvents(Event.KEYDOWN); - 访问窗体元素 document.all("txt").focus();
document.all("txt").select(); - 窗体命令
document.execCommand - 窗体COOKIE document.cookie
- 菜单事件
document.oncontextmenu - 创建元素 document.createElement("SPAN");
- 根据鼠标获得元素: document.elementFromPoint(event.x,event.y).tagName=="TD document.elementFromPoint(event.x,event.y).appendChild(ms)
- 窗体图片
document.images[索引] - 窗体事件绑定 document.onmousedown=scrollwindow;
- 元素
document.窗体.elements[索引] - 对象绑定事件 document.all.xxx.detachEvent('onclick',a);
- 插件数目
navigator.plugins - 取变量类型 typeof($js_libpath) == "undefined"
- 下拉框 下拉框.options[索引]
下拉框.options.length - 查找对象
document.getElementsByName("r1");
document.getElementById(id); - 定时 timer=setInterval('scrollwindow()',delay);
clearInterval(timer); - UNCODE编码 escape() ,unescape
- 父对象
obj.parentElement(dhtml)
obj.parentNode(dom) - 交换表的行 TableID.moveRow(2,1)
- 替换CSS document.all.csss.href = "a.css";
- 并排显示 display:inline
- 隐藏焦点 hidefocus=true
- 根据宽度换行 style="word-break:break-all"
- 自动刷新 <meta HTTP-EQUIV="refresh" CONTENT="8;URL=http://c98.yeah.net">
- 简单邮件 <a href="mailto:aaa@bbb.com?subject=ccc&body=xxxyyy">
- 快速转到位置 obj.scrollIntoView(true)
- 锚 <a name="first">
<a href="#first">anchors</a> - 网页传递参数 location.search();
- 可编辑 obj.contenteditable=true
- 执行菜单命令
obj.execCommand - 双字节字符
/[^x00-xff]/
汉字
/[u4e00-u9fa5]/ - 让英文字符串超出表格宽度自动换行
word-wrap: break-word; word-break: break-all; - 透明背景
<IFRAME src="1.htm" width=300 height=180 allowtransparency></iframe> - 获得style内容 obj.style.cssText
- HTML标签
document.documentElement.innerHTML - 第一个style标签
document.styleSheets[0] - style标签里的第一个样式
document.styleSheets[0].rules[0] - 防止点击空链接时,页面往往重置到页首端。 <a href="javascript:function()">word</a>
- 上一网页源
asp:
request.servervariables("HTTP_REFERER")
javascript:
document.referrer - 释放内存 CollectGarbage();
- 禁止右键
document.oncontextmenu = function() { return false;} - 禁止保存
<noscript><iframe src="*.htm"></iframe></noscript> - 禁止选取<body oncontextmenu="return false" ondragstart="return false" onselectstart ="return false" onselect="document.selection.empty()" oncopy="document.selection.empty()" onbeforecopy="return false"onmouseup="document.selection.empty()>
- 禁止粘贴
<input type=text onpaste="return false"> - 地址栏图标
<link rel="Shortcut Icon" href="favicon.ico">
favicon.ico 名字最好不变16*16的16色,放虚拟目录根目录下 - 收藏栏图标
<link rel="Bookmark" href="favicon.ico"> - 查看源码
<input type=button value=查看网页源代码 onclick="window.location = 'view-source:'+ 'http://www.csdn.net/'"> - 关闭输入法
<input style="ime-mode:disabled"> - 自动全选
<input type=text name=text1 value="123" onfocus="this.select()"> - ENTER键可以让光标移到下一个输入框 <input onkeydown="if(event.keyCode==13)event.keyCode=9">
- 文本框的默认值 <input type=text value="123" onfocus="alert(this.defaultValue)">
- title换行 obj.title = "123
sdfs "
- 获得时间所代表的微秒 var n1 = new Date("2004-10-10".replace(/-/g, "/")).getTime()
- 窗口是否关闭 win.closed
- checkbox扁平 <input type=checkbox style="position: absolute; clip:rect(5px 15px 15px 5px)"><br>
- 获取选中内容 document.selection.createRange().duplicate().text
- 自动完成功能 <input type=text autocomplete=on>打开该功能
<input type=text autocomplete=off>关闭该功能 - 窗口最大化
<body onload="window.resizeTo(window.screen.width - 4,window.screen.height-50);window.moveTo(-4,-4)"> - 无关闭按钮IE window.open("aa.htm", "meizz", "fullscreen=7");
- 统一编码/解码 alert(decodeURIComponent(encodeURIComponent("http://你好.com?as= hehe")))
encodeURIComponent对":"、"/"、";" 和 "?"也编码 - 表格行指示 <tr onmouseover="this.bgColor='#f0f0f0'" onmouseout="this.bgColor='#ffffff'">
//各种尺寸
s += "rn网页可见区域宽:"+ document.body.clientWidth;
s += "rn网页可见区域高:"+ document.body.clientHeight;
s += "rn网页可见区域高:"+ document.body.offsetWeight +" (包括边线的宽)";
s += "rn网页可见区域高:"+ document.body.offsetHeight +" (包括边线的宽)";
s += "rn网页正文全文宽:"+ document.body.scrollWidth;
s += "rn网页正文全文高:"+ document.body.scrollHeight;
s += "rn网页被卷去的高:"+ document.body.scrollTop;
s += "rn网页被卷去的左:"+ document.body.scrollLeft;
s += "rn网页正文部分上:"+ window.screenTop;
s += "rn网页正文部分左:"+ window.screenLeft;
s += "rn屏幕分辨率的高:"+ window.screen.height;
s += "rn屏幕分辨率的宽:"+ window.screen.width;
s += "rn屏幕可用工作区高度:"+ window.screen.availHeight;
s += "rn屏幕可用工作区宽度:"+ window.screen.availWidth;
- 锚 <a name="first">
<a href="#first">anchors</a> - 网页传递参数 location.search();
- 可编辑 obj.contenteditable=true
- 执行菜单命令
obj.execCommand - 双字节字符
/[^x00-xff]/
汉字
/[u4e00-u9fa5]/ - 让英文字符串超出表格宽度自动换行
word-wrap: break-word; word-break: break-all; - 透明背景
<IFRAME src="1.htm" width=300 height=180 allowtransparency></iframe> - 获得style内容 obj.style.cssText
- HTML标签
document.documentElement.innerHTML - 第一个style标签
document.styleSheets[0] - style标签里的第一个样式
document.styleSheets[0].rules[0] - 防止点击空链接时,页面往往重置到页首端。 <a href="javascript:function()">word</a>
- 上一网页源
asp:
request.servervariables("HTTP_REFERER")
javascript:
document.referrer - 释放内存 CollectGarbage();
- 禁止右键
document.oncontextmenu = function() { return false;} - 禁止保存
<noscript><iframe src="*.htm"></iframe></noscript> - 禁止选取<body oncontextmenu="return false" ondragstart="return false" onselectstart ="return false" onselect="document.selection.empty()" oncopy="document.selection.empty()" onbeforecopy="return false"onmouseup="document.selection.empty()>
- 禁止粘贴
<input type=text onpaste="return false"> - 地址栏图标
<link rel="Shortcut Icon" href="favicon.ico">
favicon.ico 名字最好不变16*16的16色,放虚拟目录根目录下 - 收藏栏图标
<link rel="Bookmark" href="favicon.ico"> - 查看源码
<input type=button value=查看网页源代码 onclick="window.location = 'view-source:'+ 'http://www.csdn.net/'"> - 关闭输入法
<input style="ime-mode:disabled"> - 自动全选
<input type=text name=text1 value="123" onfocus="this.select()"> - ENTER键可以让光标移到下一个输入框 <input onkeydown="if(event.keyCode==13)event.keyCode=9">
- 文本框的默认值 <input type=text value="123" onfocus="alert(this.defaultValue)">
- title换行 obj.title = "123
sdfs "
- 获得时间所代表的微秒 var n1 = new Date("2004-10-10".replace(/-/g, "/")).getTime()
- 窗口是否关闭 win.closed
- checkbox扁平 <input type=checkbox style="position: absolute; clip:rect(5px 15px 15px 5px)"><br>
- 获取选中内容 document.selection.createRange().duplicate().text
- 自动完成功能 <input type=text autocomplete=on>打开该功能
<input type=text autocomplete=off>关闭该功能 - 窗口最大化
<body onload="window.resizeTo(window.screen.width - 4,window.screen.height-50);window.moveTo(-4,-4)"> - 无关闭按钮IE window.open("aa.htm", "meizz", "fullscreen=7");
- 统一编码/解码 alert(decodeURIComponent(encodeURIComponent("http://你好.com?as= hehe")))
encodeURIComponent对":"、"/"、";" 和 "?"也编码 - 表格行指示 <tr onmouseover="this.bgColor='#f0f0f0'" onmouseout="this.bgColor='#ffffff'">
//各种尺寸
s += "rn网页可见区域宽:"+ document.body.clientWidth;
s += "rn网页可见区域高:"+ document.body.clientHeight;
s += "rn网页可见区域高:"+ document.body.offsetWeight +" (包括边线的宽)";
s += "rn网页可见区域高:"+ document.body.offsetHeight +" (包括边线的宽)";
s += "rn网页正文全文宽:"+ document.body.scrollWidth;
s += "rn网页正文全文高:"+ document.body.scrollHeight;
s += "rn网页被卷去的高:"+ document.body.scrollTop;
s += "rn网页被卷去的左:"+ document.body.scrollLeft;
s += "rn网页正文部分上:"+ window.screenTop;
s += "rn网页正文部分左:"+ window.screenLeft;
s += "rn屏幕分辨率的高:"+ window.screen.height;
s += "rn屏幕分辨率的宽:"+ window.screen.width;
s += "rn屏幕可用工作区高度:"+ window.screen.availHeight;
s += "rn屏幕可用工作区宽度:"+ window.screen.availWidth;
- 自动刷新 <meta HTTP-EQUIV="refresh" CONTENT="8;URL=http://c98.yeah.net">
- 简单邮件 <a href="mailto:aaa@bbb.com?subject=ccc&body=xxxyyy">
- 快速转到位置 obj.scrollIntoView(true)
- 锚 <a name="first">
<a href="#first">anchors</a> - 网页传递参数 location.search();
- 可编辑 obj.contenteditable=true
- 执行菜单命令
obj.execCommand - 双字节字符
/[^x00-xff]/
汉字
/[u4e00-u9fa5]/ - 让英文字符串超出表格宽度自动换行
word-wrap: break-word; word-break: break-all; - 透明背景
<IFRAME src="1.htm" width=300 height=180 allowtransparency></iframe> - 获得style内容 obj.style.cssText
- HTML标签
document.documentElement.innerHTML - 第一个style标签
document.styleSheets[0] - style标签里的第一个样式
document.styleSheets[0].rules[0] - 防止点击空链接时,页面往往重置到页首端。 <a href="javascript:function()">word</a>
- 上一网页源
asp:
request.servervariables("HTTP_REFERER")
javascript:
document.referrer - 释放内存 CollectGarbage();
- 禁止右键
document.oncontextmenu = function() { return false;} - 禁止保存
<noscript><iframe src="*.htm"></iframe></noscript> - 禁止选取<body oncontextmenu="return false" ondragstart="return false" onselectstart ="return false" onselect="document.selection.empty()" oncopy="document.selection.empty()" onbeforecopy="return false"onmouseup="document.selection.empty()>
- 禁止粘贴
<input type=text onpaste="return false"> - 地址栏图标
<link rel="Shortcut Icon" href="favicon.ico">
favicon.ico 名字最好不变16*16的16色,放虚拟目录根目录下 - 收藏栏图标
<link rel="Bookmark" href="favicon.ico"> - 查看源码
<input type=button value=查看网页源代码 onclick="window.location = 'view-source:'+ 'http://www.csdn.net/'"> - 关闭输入法
<input style="ime-mode:disabled"> - 自动全选
<input type=text name=text1 value="123" onfocus="this.select()"> - ENTER键可以让光标移到下一个输入框 <input onkeydown="if(event.keyCode==13)event.keyCode=9">
- 文本框的默认值 <input type=text value="123" onfocus="alert(this.defaultValue)">
- title换行 obj.title = "123
sdfs "
- 获得时间所代表的微秒 var n1 = new Date("2004-10-10".replace(/-/g, "/")).getTime()
- 窗口是否关闭 win.closed
- checkbox扁平 <input type=checkbox style="position: absolute; clip:rect(5px 15px 15px 5px)"><br>
- 获取选中内容 document.selection.createRange().duplicate().text
- 自动完成功能 <input type=text autocomplete=on>打开该功能
<input type=text autocomplete=off>关闭该功能 - 窗口最大化
<body onload="window.resizeTo(window.screen.width - 4,window.screen.height-50);window.moveTo(-4,-4)"> - 无关闭按钮IE window.open("aa.htm", "meizz", "fullscreen=7");
- 统一编码/解码 alert(decodeURIComponent(encodeURIComponent("http://你好.com?as= hehe")))
encodeURIComponent对":"、"/"、";" 和 "?"也编码 - 表格行指示 <tr onmouseover="this.bgColor='#f0f0f0'" onmouseout="this.bgColor='#ffffff'">
//各种尺寸
s += "rn网页可见区域宽:"+ document.body.clientWidth;
s += "rn网页可见区域高:"+ document.body.clientHeight;
s += "rn网页可见区域高:"+ document.body.offsetWeight +" (包括边线的宽)";
s += "rn网页可见区域高:"+ document.body.offsetHeight +" (包括边线的宽)";
s += "rn网页正文全文宽:"+ document.body.scrollWidth;
s += "rn网页正文全文高:"+ document.body.scrollHeight;
s += "rn网页被卷去的高:"+ document.body.scrollTop;
s += "rn网页被卷去的左:"+ document.body.scrollLeft;
s += "rn网页正文部分上:"+ window.screenTop;
s += "rn网页正文部分左:"+ window.screenLeft;
s += "rn屏幕分辨率的高:"+ window.screen.height;
s += "rn屏幕分辨率的宽:"+ window.screen.width;
s += "rn屏幕可用工作区高度:"+ window.screen.availHeight;
s += "rn屏幕可用工作区宽度:"+ window.screen.availWidth;
- 锚 <a name="first">
<a href="#first">anchors</a> - 网页传递参数 location.search();
- 可编辑 obj.contenteditable=true
- 执行菜单命令
obj.execCommand - 双字节字符
/[^x00-xff]/
汉字
/[u4e00-u9fa5]/ - 让英文字符串超出表格宽度自动换行
word-wrap: break-word; word-break: break-all; - 透明背景
<IFRAME src="1.htm" width=300 height=180 allowtransparency></iframe> - 获得style内容 obj.style.cssText
- HTML标签
document.documentElement.innerHTML - 第一个style标签
document.styleSheets[0] - style标签里的第一个样式
document.styleSheets[0].rules[0] - 防止点击空链接时,页面往往重置到页首端。 <a href="javascript:function()">word</a>
- 上一网页源
asp:
request.servervariables("HTTP_REFERER")
javascript:
document.referrer - 释放内存 CollectGarbage();
- 禁止右键
document.oncontextmenu = function() { return false;} - 禁止保存
<noscript><iframe src="*.htm"></iframe></noscript> - 禁止选取<body oncontextmenu="return false" ondragstart="return false" onselectstart ="return false" onselect="document.selection.empty()" oncopy="document.selection.empty()" onbeforecopy="return false"onmouseup="document.selection.empty()>
- 禁止粘贴
<input type=text onpaste="return false"> - 地址栏图标
<link rel="Shortcut Icon" href="favicon.ico">
favicon.ico 名字最好不变16*16的16色,放虚拟目录根目录下 - 收藏栏图标
<link rel="Bookmark" href="favicon.ico"> - 查看源码
<input type=button value=查看网页源代码 onclick="window.location = 'view-source:'+ 'http://www.csdn.net/'"> - 关闭输入法
<input style="ime-mode:disabled"> - 自动全选
<input type=text name=text1 value="123" onfocus="this.select()"> - ENTER键可以让光标移到下一个输入框 <input onkeydown="if(event.keyCode==13)event.keyCode=9">
- 文本框的默认值 <input type=text value="123" onfocus="alert(this.defaultValue)">
- title换行 obj.title = "123
sdfs "
- 获得时间所代表的微秒 var n1 = new Date("2004-10-10".replace(/-/g, "/")).getTime()
- 窗口是否关闭 win.closed
- checkbox扁平 <input type=checkbox style="position: absolute; clip:rect(5px 15px 15px 5px)"><br>
- 获取选中内容 document.selection.createRange().duplicate().text
- 自动完成功能 <input type=text autocomplete=on>打开该功能
<input type=text autocomplete=off>关闭该功能 - 窗口最大化
<body onload="window.resizeTo(window.screen.width - 4,window.screen.height-50);window.moveTo(-4,-4)"> - 无关闭按钮IE window.open("aa.htm", "meizz", "fullscreen=7");
- 统一编码/解码 alert(decodeURIComponent(encodeURIComponent("http://你好.com?as= hehe")))
encodeURIComponent对":"、"/"、";" 和 "?"也编码 - 表格行指示 <tr onmouseover="this.bgColor='#f0f0f0'" onmouseout="this.bgColor='#ffffff'">
//各种尺寸
s += "rn网页可见区域宽:"+ document.body.clientWidth;
s += "rn网页可见区域高:"+ document.body.clientHeight;
s += "rn网页可见区域高:"+ document.body.offsetWeight +" (包括边线的宽)";
s += "rn网页可见区域高:"+ document.body.offsetHeight +" (包括边线的宽)";
s += "rn网页正文全文宽:"+ document.body.scrollWidth;
s += "rn网页正文全文高:"+ document.body.scrollHeight;
s += "rn网页被卷去的高:"+ document.body.scrollTop;
s += "rn网页被卷去的左:"+ document.body.scrollLeft;
s += "rn网页正文部分上:"+ window.screenTop;
s += "rn网页正文部分左:"+ window.screenLeft;
s += "rn屏幕分辨率的高:"+ window.screen.height;
s += "rn屏幕分辨率的宽:"+ window.screen.width;
s += "rn屏幕可用工作区高度:"+ window.screen.availHeight;
s += "rn屏幕可用工作区宽度:"+ window.screen.availWidth;
- 隐藏焦点 hidefocus=true
- 根据宽度换行 style="word-break:break-all"
- 自动刷新 <meta HTTP-EQUIV="refresh" CONTENT="8;URL=http://c98.yeah.net">
- 简单邮件 <a href="mailto:aaa@bbb.com?subject=ccc&body=xxxyyy">
- 快速转到位置 obj.scrollIntoView(true)
- 锚 <a name="first">
<a href="#first">anchors</a> - 网页传递参数 location.search();
- 可编辑 obj.contenteditable=true
- 执行菜单命令
obj.execCommand - 双字节字符
/[^x00-xff]/
汉字
/[u4e00-u9fa5]/ - 让英文字符串超出表格宽度自动换行
word-wrap: break-word; word-break: break-all; - 透明背景
<IFRAME src="1.htm" width=300 height=180 allowtransparency></iframe> - 获得style内容 obj.style.cssText
- HTML标签
document.documentElement.innerHTML - 第一个style标签
document.styleSheets[0] - style标签里的第一个样式
document.styleSheets[0].rules[0] - 防止点击空链接时,页面往往重置到页首端。 <a href="javascript:function()">word</a>
- 上一网页源
asp:
request.servervariables("HTTP_REFERER")
javascript:
document.referrer - 释放内存 CollectGarbage();
- 禁止右键
document.oncontextmenu = function() { return false;} - 禁止保存
<noscript><iframe src="*.htm"></iframe></noscript> - 禁止选取<body oncontextmenu="return false" ondragstart="return false" onselectstart ="return false" onselect="document.selection.empty()" oncopy="document.selection.empty()" onbeforecopy="return false"onmouseup="document.selection.empty()>
- 禁止粘贴
<input type=text onpaste="return false"> - 地址栏图标
<link rel="Shortcut Icon" href="favicon.ico">
favicon.ico 名字最好不变16*16的16色,放虚拟目录根目录下 - 收藏栏图标
<link rel="Bookmark" href="favicon.ico"> - 查看源码
<input type=button value=查看网页源代码 onclick="window.location = 'view-source:'+ 'http://www.csdn.net/'"> - 关闭输入法
<input style="ime-mode:disabled"> - 自动全选
<input type=text name=text1 value="123" onfocus="this.select()"> - ENTER键可以让光标移到下一个输入框 <input onkeydown="if(event.keyCode==13)event.keyCode=9">
- 文本框的默认值 <input type=text value="123" onfocus="alert(this.defaultValue)">
- title换行 obj.title = "123
sdfs "
- 获得时间所代表的微秒 var n1 = new Date("2004-10-10".replace(/-/g, "/")).getTime()
- 窗口是否关闭 win.closed
- checkbox扁平 <input type=checkbox style="position: absolute; clip:rect(5px 15px 15px 5px)"><br>
- 获取选中内容 document.selection.createRange().duplicate().text
- 自动完成功能 <input type=text autocomplete=on>打开该功能
<input type=text autocomplete=off>关闭该功能 - 窗口最大化
<body onload="window.resizeTo(window.screen.width - 4,window.screen.height-50);window.moveTo(-4,-4)"> - 无关闭按钮IE window.open("aa.htm", "meizz", "fullscreen=7");
- 统一编码/解码 alert(decodeURIComponent(encodeURIComponent("http://你好.com?as= hehe")))
encodeURIComponent对":"、"/"、";" 和 "?"也编码 - 表格行指示 <tr onmouseover="this.bgColor='#f0f0f0'" onmouseout="this.bgColor='#ffffff'">
//各种尺寸
s += "rn网页可见区域宽:"+ document.body.clientWidth;
s += "rn网页可见区域高:"+ document.body.clientHeight;
s += "rn网页可见区域高:"+ document.body.offsetWeight +" (包括边线的宽)";
s += "rn网页可见区域高:"+ document.body.offsetHeight +" (包括边线的宽)";
s += "rn网页正文全文宽:"+ document.body.scrollWidth;
s += "rn网页正文全文高:"+ document.body.scrollHeight;
s += "rn网页被卷去的高:"+ document.body.scrollTop;
s += "rn网页被卷去的左:"+ document.body.scrollLeft;
s += "rn网页正文部分上:"+ window.screenTop;
s += "rn网页正文部分左:"+ window.screenLeft;
s += "rn屏幕分辨率的高:"+ window.screen.height;
s += "rn屏幕分辨率的宽:"+ window.screen.width;
s += "rn屏幕可用工作区高度:"+ window.screen.availHeight;
s += "rn屏幕可用工作区宽度:"+ window.screen.availWidth;
- 锚 <a name="first">
<a href="#first">anchors</a> - 网页传递参数 location.search();
- 可编辑 obj.contenteditable=true
- 执行菜单命令
obj.execCommand - 双字节字符
/[^x00-xff]/
汉字
/[u4e00-u9fa5]/ - 让英文字符串超出表格宽度自动换行
word-wrap: break-word; word-break: break-all; - 透明背景
<IFRAME src="1.htm" width=300 height=180 allowtransparency></iframe> - 获得style内容 obj.style.cssText
- HTML标签
document.documentElement.innerHTML - 第一个style标签
document.styleSheets[0] - style标签里的第一个样式
document.styleSheets[0].rules[0] - 防止点击空链接时,页面往往重置到页首端。 <a href="javascript:function()">word</a>
- 上一网页源
asp:
request.servervariables("HTTP_REFERER")
javascript:
document.referrer - 释放内存 CollectGarbage();
- 禁止右键
document.oncontextmenu = function() { return false;} - 禁止保存
<noscript><iframe src="*.htm"></iframe></noscript> - 禁止选取<body oncontextmenu="return false" ondragstart="return false" onselectstart ="return false" onselect="document.selection.empty()" oncopy="document.selection.empty()" onbeforecopy="return false"onmouseup="document.selection.empty()>
- 禁止粘贴
<input type=text onpaste="return false"> - 地址栏图标
<link rel="Shortcut Icon" href="favicon.ico">
favicon.ico 名字最好不变16*16的16色,放虚拟目录根目录下 - 收藏栏图标
<link rel="Bookmark" href="favicon.ico"> - 查看源码
<input type=button value=查看网页源代码 onclick="window.location = 'view-source:'+ 'http://www.csdn.net/'"> - 关闭输入法
<input style="ime-mode:disabled"> - 自动全选
<input type=text name=text1 value="123" onfocus="this.select()"> - ENTER键可以让光标移到下一个输入框 <input onkeydown="if(event.keyCode==13)event.keyCode=9">
- 文本框的默认值 <input type=text value="123" onfocus="alert(this.defaultValue)">
- title换行 obj.title = "123
sdfs "
- 获得时间所代表的微秒 var n1 = new Date("2004-10-10".replace(/-/g, "/")).getTime()
- 窗口是否关闭 win.closed
- checkbox扁平 <input type=checkbox style="position: absolute; clip:rect(5px 15px 15px 5px)"><br>
- 获取选中内容 document.selection.createRange().duplicate().text
- 自动完成功能 <input type=text autocomplete=on>打开该功能
<input type=text autocomplete=off>关闭该功能 - 窗口最大化
<body onload="window.resizeTo(window.screen.width - 4,window.screen.height-50);window.moveTo(-4,-4)"> - 无关闭按钮IE window.open("aa.htm", "meizz", "fullscreen=7");
- 统一编码/解码 alert(decodeURIComponent(encodeURIComponent("http://你好.com?as= hehe")))
encodeURIComponent对":"、"/"、";" 和 "?"也编码 - 表格行指示 <tr onmouseover="this.bgColor='#f0f0f0'" onmouseout="this.bgColor='#ffffff'">
//各种尺寸
s += "rn网页可见区域宽:"+ document.body.clientWidth;
s += "rn网页可见区域高:"+ document.body.clientHeight;
s += "rn网页可见区域高:"+ document.body.offsetWeight +" (包括边线的宽)";
s += "rn网页可见区域高:"+ document.body.offsetHeight +" (包括边线的宽)";
s += "rn网页正文全文宽:"+ document.body.scrollWidth;
s += "rn网页正文全文高:"+ document.body.scrollHeight;
s += "rn网页被卷去的高:"+ document.body.scrollTop;
s += "rn网页被卷去的左:"+ document.body.scrollLeft;
s += "rn网页正文部分上:"+ window.screenTop;
s += "rn网页正文部分左:"+ window.screenLeft;
s += "rn屏幕分辨率的高:"+ window.screen.height;
s += "rn屏幕分辨率的宽:"+ window.screen.width;
s += "rn屏幕可用工作区高度:"+ window.screen.availHeight;
s += "rn屏幕可用工作区宽度:"+ window.screen.availWidth;
- 自动刷新 <meta HTTP-EQUIV="refresh" CONTENT="8;URL=http://c98.yeah.net">
- 简单邮件 <a href="mailto:aaa@bbb.com?subject=ccc&body=xxxyyy">
- 快速转到位置 obj.scrollIntoView(true)
- 锚 <a name="first">
<a href="#first">anchors</a> - 网页传递参数 location.search();
- 可编辑 obj.contenteditable=true
- 执行菜单命令
obj.execCommand - 双字节字符
/[^x00-xff]/
汉字
/[u4e00-u9fa5]/ - 让英文字符串超出表格宽度自动换行
word-wrap: break-word; word-break: break-all; - 透明背景
<IFRAME src="1.htm" width=300 height=180 allowtransparency></iframe> - 获得style内容 obj.style.cssText
- HTML标签
document.documentElement.innerHTML - 第一个style标签
document.styleSheets[0] - style标签里的第一个样式
document.styleSheets[0].rules[0] - 防止点击空链接时,页面往往重置到页首端。 <a href="javascript:function()">word</a>
- 上一网页源
asp:
request.servervariables("HTTP_REFERER")
javascript:
document.referrer - 释放内存 CollectGarbage();
- 禁止右键
document.oncontextmenu = function() { return false;} - 禁止保存
<noscript><iframe src="*.htm"></iframe></noscript> - 禁止选取<body oncontextmenu="return false" ondragstart="return false" onselectstart ="return false" onselect="document.selection.empty()" oncopy="document.selection.empty()" onbeforecopy="return false"onmouseup="document.selection.empty()>
- 禁止粘贴
<input type=text onpaste="return false"> - 地址栏图标
<link rel="Shortcut Icon" href="favicon.ico">
favicon.ico 名字最好不变16*16的16色,放虚拟目录根目录下 - 收藏栏图标
<link rel="Bookmark" href="favicon.ico"> - 查看源码
<input type=button value=查看网页源代码 onclick="window.location = 'view-source:'+ 'http://www.csdn.net/'"> - 关闭输入法
<input style="ime-mode:disabled"> - 自动全选
<input type=text name=text1 value="123" onfocus="this.select()"> - ENTER键可以让光标移到下一个输入框 <input onkeydown="if(event.keyCode==13)event.keyCode=9">
- 文本框的默认值 <input type=text value="123" onfocus="alert(this.defaultValue)">
- title换行 obj.title = "123
sdfs "
- 获得时间所代表的微秒 var n1 = new Date("2004-10-10".replace(/-/g, "/")).getTime()
- 窗口是否关闭 win.closed
- checkbox扁平 <input type=checkbox style="position: absolute; clip:rect(5px 15px 15px 5px)"><br>
- 获取选中内容 document.selection.createRange().duplicate().text
- 自动完成功能 <input type=text autocomplete=on>打开该功能
<input type=text autocomplete=off>关闭该功能 - 窗口最大化
<body onload="window.resizeTo(window.screen.width - 4,window.screen.height-50);window.moveTo(-4,-4)"> - 无关闭按钮IE window.open("aa.htm", "meizz", "fullscreen=7");
- 统一编码/解码 alert(decodeURIComponent(encodeURIComponent("http://你好.com?as= hehe")))
encodeURIComponent对":"、"/"、";" 和 "?"也编码 - 表格行指示 <tr onmouseover="this.bgColor='#f0f0f0'" onmouseout="this.bgColor='#ffffff'">
//各种尺寸
s += "rn网页可见区域宽:"+ document.body.clientWidth;
s += "rn网页可见区域高:"+ document.body.clientHeight;
s += "rn网页可见区域高:"+ document.body.offsetWeight +" (包括边线的宽)";
s += "rn网页可见区域高:"+ document.body.offsetHeight +" (包括边线的宽)";
s += "rn网页正文全文宽:"+ document.body.scrollWidth;
s += "rn网页正文全文高:"+ document.body.scrollHeight;
s += "rn网页被卷去的高:"+ document.body.scrollTop;
s += "rn网页被卷去的左:"+ document.body.scrollLeft;
s += "rn网页正文部分上:"+ window.screenTop;
s += "rn网页正文部分左:"+ window.screenLeft;
s += "rn屏幕分辨率的高:"+ window.screen.height;
s += "rn屏幕分辨率的宽:"+ window.screen.width;
s += "rn屏幕可用工作区高度:"+ window.screen.availHeight;
s += "rn屏幕可用工作区宽度:"+ window.screen.availWidth;
- 锚 <a name="first">
<a href="#first">anchors</a> - 网页传递参数 location.search();
- 可编辑 obj.contenteditable=true
- 执行菜单命令
obj.execCommand - 双字节字符
/[^x00-xff]/
汉字
/[u4e00-u9fa5]/ - 让英文字符串超出表格宽度自动换行
word-wrap: break-word; word-break: break-all; - 透明背景
<IFRAME src="1.htm" width=300 height=180 allowtransparency></iframe> - 获得style内容 obj.style.cssText
- HTML标签
document.documentElement.innerHTML - 第一个style标签
document.styleSheets[0] - style标签里的第一个样式
document.styleSheets[0].rules[0] - 防止点击空链接时,页面往往重置到页首端。 <a href="javascript:function()">word</a>
- 上一网页源
asp:
request.servervariables("HTTP_REFERER")
javascript:
document.referrer - 释放内存 CollectGarbage();
- 禁止右键
document.oncontextmenu = function() { return false;} - 禁止保存
<noscript><iframe src="*.htm"></iframe></noscript> - 禁止选取<body oncontextmenu="return false" ondragstart="return false" onselectstart ="return false" onselect="document.selection.empty()" oncopy="document.selection.empty()" onbeforecopy="return false"onmouseup="document.selection.empty()>
- 禁止粘贴
<input type=text onpaste="return false"> - 地址栏图标
<link rel="Shortcut Icon" href="favicon.ico">
favicon.ico 名字最好不变16*16的16色,放虚拟目录根目录下 - 收藏栏图标
<link rel="Bookmark" href="favicon.ico"> - 查看源码
<input type=button value=查看网页源代码 onclick="window.location = 'view-source:'+ 'http://www.csdn.net/'"> - 关闭输入法
<input style="ime-mode:disabled"> - 自动全选
<input type=text name=text1 value="123" onfocus="this.select()"> - ENTER键可以让光标移到下一个输入框 <input onkeydown="if(event.keyCode==13)event.keyCode=9">
- 文本框的默认值 <input type=text value="123" onfocus="alert(this.defaultValue)">
- title换行 obj.title = "123
sdfs "
- 获得时间所代表的微秒 var n1 = new Date("2004-10-10".replace(/-/g, "/")).getTime()
- 窗口是否关闭 win.closed
- checkbox扁平 <input type=checkbox style="position: absolute; clip:rect(5px 15px 15px 5px)"><br>
- 获取选中内容 document.selection.createRange().duplicate().text
- 自动完成功能 <input type=text autocomplete=on>打开该功能
<input type=text autocomplete=off>关闭该功能 - 窗口最大化
<body onload="window.resizeTo(window.screen.width - 4,window.screen.height-50);window.moveTo(-4,-4)"> - 无关闭按钮IE window.open("aa.htm", "meizz", "fullscreen=7");
- 统一编码/解码 alert(decodeURIComponent(encodeURIComponent("http://你好.com?as= hehe")))
encodeURIComponent对":"、"/"、";" 和 "?"也编码 - 表格行指示 <tr onmouseover="this.bgColor='#f0f0f0'" onmouseout="this.bgColor='#ffffff'">
//各种尺寸
s += "rn网页可见区域宽:"+ document.body.clientWidth;
s += "rn网页可见区域高:"+ document.body.clientHeight;
s += "rn网页可见区域高:"+ document.body.offsetWeight +" (包括边线的宽)";
s += "rn网页可见区域高:"+ document.body.offsetHeight +" (包括边线的宽)";
s += "rn网页正文全文宽:"+ document.body.scrollWidth;
s += "rn网页正文全文高:"+ document.body.scrollHeight;
s += "rn网页被卷去的高:"+ document.body.scrollTop;
s += "rn网页被卷去的左:"+ document.body.scrollLeft;
s += "rn网页正文部分上:"+ window.screenTop;
s += "rn网页正文部分左:"+ window.screenLeft;
s += "rn屏幕分辨率的高:"+ window.screen.height;
s += "rn屏幕分辨率的宽:"+ window.screen.width;
s += "rn屏幕可用工作区高度:"+ window.screen.availHeight;
s += "rn屏幕可用工作区宽度:"+ window.screen.availWidth;
- 并排显示 display:inline
- 隐藏焦点 hidefocus=true
- 根据宽度换行 style="word-break:break-all"
- 自动刷新 <meta HTTP-EQUIV="refresh" CONTENT="8;URL=http://c98.yeah.net">
- 简单邮件 <a href="mailto:aaa@bbb.com?subject=ccc&body=xxxyyy">
- 快速转到位置 obj.scrollIntoView(true)
- 锚 <a name="first">
<a href="#first">anchors</a> - 网页传递参数 location.search();
- 可编辑 obj.contenteditable=true
- 执行菜单命令
obj.execCommand - 双字节字符
/[^x00-xff]/
汉字
/[u4e00-u9fa5]/ - 让英文字符串超出表格宽度自动换行
word-wrap: break-word; word-break: break-all; - 透明背景
<IFRAME src="1.htm" width=300 height=180 allowtransparency></iframe> - 获得style内容 obj.style.cssText
- HTML标签
document.documentElement.innerHTML - 第一个style标签
document.styleSheets[0] - style标签里的第一个样式
document.styleSheets[0].rules[0] - 防止点击空链接时,页面往往重置到页首端。 <a href="javascript:function()">word</a>
- 上一网页源
asp:
request.servervariables("HTTP_REFERER")
javascript:
document.referrer - 释放内存 CollectGarbage();
- 禁止右键
document.oncontextmenu = function() { return false;} - 禁止保存
<noscript><iframe src="*.htm"></iframe></noscript> - 禁止选取<body oncontextmenu="return false" ondragstart="return false" onselectstart ="return false" onselect="document.selection.empty()" oncopy="document.selection.empty()" onbeforecopy="return false"onmouseup="document.selection.empty()>
- 禁止粘贴
<input type=text onpaste="return false"> - 地址栏图标
<link rel="Shortcut Icon" href="favicon.ico">
favicon.ico 名字最好不变16*16的16色,放虚拟目录根目录下 - 收藏栏图标
<link rel="Bookmark" href="favicon.ico"> - 查看源码
<input type=button value=查看网页源代码 onclick="window.location = 'view-source:'+ 'http://www.csdn.net/'"> - 关闭输入法
<input style="ime-mode:disabled"> - 自动全选
<input type=text name=text1 value="123" onfocus="this.select()"> - ENTER键可以让光标移到下一个输入框 <input onkeydown="if(event.keyCode==13)event.keyCode=9">
- 文本框的默认值 <input type=text value="123" onfocus="alert(this.defaultValue)">
- title换行 obj.title = "123
sdfs "
- 获得时间所代表的微秒 var n1 = new Date("2004-10-10".replace(/-/g, "/")).getTime()
- 窗口是否关闭 win.closed
- checkbox扁平 <input type=checkbox style="position: absolute; clip:rect(5px 15px 15px 5px)"><br>
- 获取选中内容 document.selection.createRange().duplicate().text
- 自动完成功能 <input type=text autocomplete=on>打开该功能
<input type=text autocomplete=off>关闭该功能 - 窗口最大化
<body onload="window.resizeTo(window.screen.width - 4,window.screen.height-50);window.moveTo(-4,-4)"> - 无关闭按钮IE window.open("aa.htm", "meizz", "fullscreen=7");
- 统一编码/解码 alert(decodeURIComponent(encodeURIComponent("http://你好.com?as= hehe")))
encodeURIComponent对":"、"/"、";" 和 "?"也编码 - 表格行指示 <tr onmouseover="this.bgColor='#f0f0f0'" onmouseout="this.bgColor='#ffffff'">
//各种尺寸
ireport简单介绍
发表人:ricelane | 发表时间: 2006年八月29日, 17:54
1、前言
在网络上可以搜索到很多使用iReport和Jasperreport配合实现各种报表任务的文章,但是我觉得很少有一篇(几乎没有)做一个比较详细的介绍如何使用iReport制作报表的全过程,我所看过的文章的基本思想是覆盖面广,很多内容都是提及即过,并不是开发人员都有时间为每个实践花费时间自己实现,如果能有更详细的资料,那岂不是一件乐事。出于这个念头促使我写这篇文章,希望能对那些使用iReport和Jasperreport朋友有所帮助,特别是需要亲身去实现报表的朋友,希望能给你们带来一点帮助。本文不是对iReport的每个细节进行介绍,关于iReport的每个细节可以参考iReport网站的资料,但是可能得花费一些费用。一般用户没有必要付出这些费用,因为我们关心的是如何制作我们需要的报表,而不是去扩展iReport,比如制作iReport的插件。
2、准备
2.1、下载JDK
地址:http://www.sun.com,选择1.3以上版本(建议1.4.2以上版本),安装JDK,默认安装即可;如果你的系统已经有安装过JDK或是有JRE即可省略这一步骤,验证JDK或是JRE是否可以默认运行,在命令行(CMD)打入X:>java 如果出现:Usage:java………………………..开头的一堆信息既是通过验证。否则必须进行配置,配置信息如下,在windows的环境变量设置:
path:在最后面加入“;java的安装目录”
JAVA_HOME :“java的安装目录”
CLASSPATH:“java的安装目录bin”重新验证JDK或是JRE是否可以在CMD任何位置运行
2.2、下载iReport
地址:http://ireport.sourceforge.net/,选择0.3.2版本(发稿之前为止建议使用的版本)解压iReport在任意目录,解压后的文件里面有一个iReport.bat,通过双击,过大约30秒钟如果可以弹出iReport的主窗体即表明你的系统已经可以运行iReport了,如果不能弹出主窗体,一般是第一步骤错误,或是没有完成。
2.3、准备数据库
iReport支持绝大部分数据库,只要该数据库能提供JDBC驱动器。本文提供MySql数据库作为例子,但是文章最后会提到如何配置Oracle的支持。关于数据库的安装和建立表不属于本文的范围,请参考其他资料。本文假设已经安装了MySql和在MySql已经有一些表,并且确定表中已经有数据了。
*【特别提示】MySql的版本要求与iReport文件夹下的Lib目录的使用MySql驱动程序兼容,笔者建议到 http://dev.mysql.com/downloads/ 下载最新版本mysql的驱动器,这样就不会应为JDBC驱动器的问题而当心数据库的支持问题。
2.4、启动MySql服务
确定Mysql使用的的字符集是重要的问题,特别是对需要中文报表的朋友,应该特别注意这个问题。
2.5、确定商务逻辑
也就是希望完成什么样的报表任务。需要实现的报表的详细描述,这是实现报表的业务条件,否则所有的任务绝大部分没有意义。本文使用一个Bug记录表为例,本文的例子是制作一个根据项目和项目中的模块分组的Bug量统计。
3、开始配置基本信息
3.1、配置界面使用的语言和报表输出路径
第一次进入系统是英文环境,可以通过【Tools】-【Options】开启配置iReport系统的基本信息对话框。在“Language”选项里面选择你需要的界面语言,比如“中文-中国”。点击【Apply】按钮,系统既把整个界面中文化。
在配置iReport系统的基本信息对话框中选择【编译】Label,之后决定你的报表输出路径,可以把“编译在报表数据文件夹”选择打勾,这样报表的jrxml文件和jasper文件就放置同一文件夹。(在新建报表时会要求你把jrxml文件保存到指定的文件夹)
【Options】选项中的一部分参数修改不能通过【Apply】按钮直接起作用,比如“Look&Feel”,必须重新启动iReport才能起作用。不知道是不是iReport的Bug?!最后【存档】。
3.2、配置MySql的数据库连接
这就是报表与数据库的接口。可以通过【资料来源】-【连接/资料来源】开启配置列表对话框,iReport会记录以前使用的所有连接,除非你手工删除这些连接,否则所有的连接都会存在连接/资料来源配置列表对话框中,不管是否确实可用。点击【New】进入配置新连接界面,填写JDBC连接需要的信息,iReport支持多种数据源连接,本文只是介绍DataBase JDBC Connection连接方式,这也是最常用的方式,特别是在嵌入式报表应用。所有的信息填写并测试通过之后,最后就是保存信息。回到配置列表对话框,关闭对话框,完成MySql数据库JDBC连接配置。提示:如果你需要报表提供中文内容显示可以在JDBC URL下工夫,比如输入: jdbc:mysql://localhost/SUBRDB?user=****&password=****&useUnicode=true&characterEncoding=GB2312 其中的****号替换成数据库的用户合密码。 3.3、新建一个空报表的基本配置单击工具栏的第一个工具“New Report”,新建一个报表,输入报表名称和定义报表的一些参数,比如名称输入BugsRpt(例子是做一个项目的Bug量统计报表),单击【More….】选择标签,填写或是选择XML编码,这是关系到你的XML支持的字符集的选择,请根据需要选择,比如需要你的XML文件支持中文,那么可以输入GB2312或是GBK,之后点击【OK】按钮,进入报表的设计界面。 *【特别提示】请在开始任何工作之前保存报表,这时iReport提示保存报表的位置,选择合适的位置之后输入BugsRpt作为名称。 3.4、定义报表可能需要的字体类型及其属性 一个报表的内容五花八门,有表头、栏位名、数据、其他变量信息等等,如果这些信息都是一致的字体和属性(比如颜色),那么整个报表就死气沉沉,显得很粗糙了。我们可以在为报表添加每个元素时定义元素的属性,但是那是一个多么费时的工作,如果能预先定义一些属性的组合,之后在创建每个元素时只需选择这些组合的其中一个即可,省事又快速。 单击【预览】-【报表字体】开启自定义组合对话框。单击【New】进入定义详细对话框,如图:按照图中的顺序填写信息和步骤,依次定义“表头”、“组”、“列”、“列内容”、“统计计算”、“其他”等6中字体组合。注意PDF内嵌字体的选择,如果你需要报表时以PDF文件格式提供,那么对此需要作出选择。 4、理解几个重要的概念 4.1、iReport的输出格式 iReport的预览输出格式可以支持以下几种: PDF、HTML、CSV、JAVA2D、Excel、纯文字、JRViewer,其中最常用的是PDF、JRViewer。本文以JRViewer为例子。JRViewer是直接以C/S方式作为报表的输出格式,在JFrame框架下输出。Jasperreport提供默认的JRViewer输出类。 4.2、报表的动态对象变量、参数、字段在使用iReport的过程中会碰到很多与变量(Variables)、参数(Parameters)、字段(Fields)这些有关的内容,我们要介绍这些对象的使用和意义: ·字段(Fields):是数据库抽取出来的,希望在报表中出现的数据库内容。比如一个ID的所有值。$F{ filedsName } ·参数(Parameters):这是你的应用需要提供给报表的入口,比如你希望在报表被解释的时候提供Where语句的条件值,那么就可以使用参数(Parameters)。$P{ parameterName } ·变量(Variables):这是报表中一些逻辑运算的表现,比如统计值。$V{ variablesName } 每种对象的定义格式如每个对象的后面说明,比如定义一个变量(Variables),那么表达式就写成$V{ variablesName },报表中出现的就是这个变量的名称。后文会详细的介绍使用方法。 4.3、编译、静态运行、动态运行 Jasperreport运行时需要的就是一个jasper后缀的文件,编译过程其实就是把jrxml后缀的文件生成jasper后缀的文件。(可以参考Jasperreport的运行原理)静态运行和动态运行是相对的,后者带数据源运行,比如带数据库运行。前者就是静态文本运行,和数据源无关,如果报表中出现和数据源有关的对象,则以null显示。 4.4、报表结构 一个报表的结构大致是几个部分:title、pageHeader、columnHeader、detial、columnFooter、pageFooter、summary、groupHeader、groupfooter。 ·Title:每个报表一般会有一个名字,比如×××销售报表,title就是搁置这个名称的最好地方了,当然你也可以根据需要搁置在合适的地方。 ·pageHeader:报表的一些公共要素,比如页码、创建时间、创建人等信息放置在这里是比较好的选择。 ·columnHeader:无可非议的这里是放置列的名称,记住不是列数据。 ·Detial:放置需要循环的数据,比如销售记录数据。 ·columnFooter:放置列级别的统计计算值或是列的说明。 ·pageFooter:放置页级别的统计值或是页的说明。 ·Summary:可能需要对几页(你的报表可能有几个页组成)的统计值。比如50个销售记录共占用了3页,那么放置这些统计记录的统计值最好的地方就是summary。 ·groupHeader:每个表的内容可能需要根据某个属性进行划分显示内容和计算内容,比如希望以月份为单位每组分开显示销售记录,那么就可以定义一个组(组的定义参考后文),groupHeader就是放置组说明或是组标志最好的地方。 ·Groupfooter:放置组的统计或是说明 5、向表添加对象 5.1、添加静态对象可以通过工具栏的工具添加静态对象,比如文本,点击【T】,之后在报表的空白处单击,如此即可把静态对象添加到报表,然后拖动对象的边框,使它的大小合适,双击对象弹出对象的属性配置对话框,切换到【Font】Label,在“Report font”的ComBox选择“表头”字体,(表头字体是前文提供的自定义属性组合),再切换到【Static Text】Label,修改表头的名字,比如“Bug统计报表”或是“销售记录统计表”等等与业务有关的内容。添加图片,请点击【Image Tool】,之后的操作与Text类似。其他静态对象操作步骤类似。 5.2、使用连接 还记得前文提供的(3.2节)配置MySql数据库连接吗?这里我们将要使用前文配置的连接了。选择菜单【建立】-【使用动态连接】开启可选的动态连接,选择任何一个你需要的连接最后【OK】,保存报表,这样你的报表就使用了这个连接了。 *【特别提示】此连接必须与以后应用程序使用的连接一致。 5.3、创建SQL查询语句 SQL语句是对任何RDBMS起作用的语言,外部用户需要使用这些语言管理维护数据库中的数据,同样的,iReport也是需要这么做,我们需要提供查询数据库的语言-SQL语句,这样,iReport即可通过此SQL语句获取数据,之后组织到报表中并显示出来,以满足用户的需要为目的。 通过菜单【资料来源】-【报表查询】开启SQL输入对话框,并在【Report SQL Query】 Label中输入SQL语句如图:图中的“Automatically Retrieve Fields”checkbox和“ReadFields”Button是确认自动获取还是手动获取数据库表的可用Fields。单击【OK】,保存报表。 5.4、创建字段动态对象报表的动态对象有变量、参数、字段,前文提及了他们的概念,这里将要一一讲解如何使用。字段也就是数据库中的字段,通过菜单【预览】-【报表字段】开启字段的列表(工具条上可以找到相应的工具),可以拖放任意字段到报表的任何位置,比如拖动一部分Bug的内容字段到detial段(内容无关紧要,只要知道原理)。 5.5、创建组组是一个很重要的概念,一个报表可以多个组,每个组以一个关键字为标记,比如希望Bug统计是根据项目(或是产品)进行统计的。那么可以设立一个项目标记的组。如图: 组的参数设定可以看界面即可理解部分,其中最主要的是“Group expression”,这是必须输入格式正确的并且存在的字段名称,本文的“proname”是【字段】中的一个元素。依此类推,建立其他的组对象。每建立一个组,在报表的界面上都会出现该组对应的段,如图:至于他们的意义和容纳的内容参考“4.4报表结构”,他们是首尾对应出现的。(Header和Footer) 5.6、添加参数和使用参数我们重申参数作用,一般是需要外界提供参数给报表的入口,比如SQL语句的where条件的表达式。通过【预览】-【报表参数】开启报表参数列表对话框(工具条上可以找到相应的工具)。如图:输入名称及其他参数。【ok】,保存报表。 那么如何使用呢?打开SQL语句对话框,参考“5.3创建SQL查询语句”,这时候的SQL语句应该是: SELECT * FROM bugs where proname=$P{ProjectName} order by proname,modulename 注意其中的红体字部分,就是把刚才定义的变量运用到SQL语句了。这样当应用提供参数时,只要指定提供给这个参数,那么报表解释引擎即可替换这些变量然后再执行SQL语句,在第二部分提到编程时,会提供参数设定代码。 5.7、添加变量和使用变量变量的定义类似参数,通过【预览】-【报表变量】开启报表变量列表对话框(工具条上可以找到相应的工具),如图,图中定义的变量的作用是:定义一个Bug的计数器,数据类型是java.lang.Integer,使用Count函数进行统计字段tester,作用范围是模块组,也就是统计模块的Bug量。其中的tester可以改成其他非组对象,比如proname是组对象,就不要用作这里的统计参数。以上提供的是自定义变量,其实iReport系统还有提供一些内嵌(Buildin)的变量,比如页码,行记录数等,视需要而使用。 6、最后的报表 6.1、完成后的报表 6.2、预览报表点击动态运行报表,出现如图内容: 7、总结第一部分只是介绍了如何制作一个动态数据报表,其实iReport还有提供很多的特性供开发人员使用,比如柱状图、饼图、及各种形状的图形等,满足企业绝大部分应用的需求。希望你能继续研究并充分利用。 关于使用Oracle数据库作为数据源的内容:提供与使用的Oracle版本对应的JDBC驱动,把驱动放置在iReport的lib目录,配置数据库的JDBC连接时如图:其它操作基本没有区别。 8、补充内容 8.1、实现表格可以在Detial中加入必要线条实现表格,配合columnFooter、columnHeader、Detial这3个位置实现,您可以试试画线的位置!
动态配置log4j
发表人:ricelane | 发表时间: 2006年八月25日, 15:56
1 配置外部配置文件来配置的基本步骤
1.1 一个运用配置文件的实例
Log4j之所以能成功的原因之一是它的灵活性。但如果只是简单的调用BasicConfigurator.configure()来进行配置工作,那么所有的配置都是在函数中写死的,以后修改配置就要修改原代码,这就不能体现出log4j的灵活性了,所以基本上不会通过BasicConfigurator.configure()来进行配置工作的。
为了增加软件的灵活性,最常用的做法就是使用配置文件,如web.xml之于J2EE,struts-config.xml之于struts一样,log4j也提供了让我们把配置信息从程序转移到配置文件中的方法。Log4j提供了两种方式的配置文件:XML文件和Java的property配置文件。通过把配置信息转移到外部文件中,当我们要修改配置信息时,就可以直接修改配置文件而不用去修改代码了,下面,我们就来完成一个通过配置文件来实现log4j的实例。
例2-a:
package TestLog4j;
import org.apache.log4j.Logger;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.Priority; public class TestLog4j
{
static Logger logger = Logger.getLogger(TestLog4j.class.getName());
public TestLog4j(){}
public static void main(String[] args)
{
//通过BasicConfigurator类来初始化
//BasicConfigurator.configure();
//(1)通过配置文件来初始化
PropertyConfigurator.configure("F:nepalonlog4j.properties");
logger.debug("Start of the main() in TestLog4j"); //代码(2)
logger.info("Just testing a log message with priority set to INFO");
logger.warn("Just testing a log message with priority set to WARN");
logger.error("Just testing a log message with priority set to ERROR");
logger.fatal("Just testing a log message with priority set to FATAL");
logger.log(Priority.WARN, "Testing a log message use a alternate form");
logger.debug(TestLog4j.class.getName()); //代码(2)
}
}
在这个例子中,我们用PropertyConfigurator.configure("F:nepalonlog4j.properties")代替BasicConfigurator.configure()进行配置。PropertyConfigurator.configure()函数的参数可以是一个properties文件所在路径的String对象,可以是一个properties文件所在路径的URL对象,也可以是一个properties对象。通过PropertyConfigurator.configure()可以通过指定的properties文件来配置信息。如果要用XML文件进行信息配置,可以在代码中调用DOMConfigurator()函数来进行配置工作。在这里,我们只以properties文件来完成例子。接着,我们来看一下log4j.properties文件中都有些什么东西:
例2-b:
log4j.rootLogger = DEBUG, A1
log4j.appender.A1 = org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout = org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern = %-4r [%t] %-5p %c %x - %m%n
运行这个实例,运行结果为
0 [main] DEBUG TestLog4j.TestLog4j - Start of the main() in TestLog4j
20 [main] INFO TestLog4j.TestLog4j - Just testing a log message with priority set to INFO
20 [main] WARN TestLog4j.TestLog4j - Just testing a log message with priority set to WARN
20 [main] ERROR TestLog4j.TestLog4j - Just testing a log message with priority set to ERROR
20 [main] FATAL TestLog4j.TestLog4j - Just testing a log message with priority set to FATAL
180 [main] WARN TestLog4j.TestLog4j - Testing a log message use a alternate form
180 [main] DEBUG TestLog4j.TestLog4j - TestLog4j.TestLog4j
下面,我们分析一下这个配置文件。
1) 由于每一个Logger对旬都有一个级别,文件的第一行就是定义了一个Logger及其级别。在这里定义了一个根记录器(root logger),这涉及到记录器的层次问题,在些暂时不深入讨论,在后面的章节再进行讨论。
2) 第二行定义了一个名为A1的输出流,这个流就是控制台,所以通过Logger对象打印的信息会在控制台输出。
3) 第三行定义了打印信息的布局。在这里我们用PatternLayout作为此记录器的布局,PatternLayout允许你以灵活的格式来打印信息。
4) 第四行指定的打印信息的具体格式,从结果可知,这个实例的打印格式为:当前打印语句所使用的时间 [日志所在的线程] 打印的级别 当前日志所在的类的全名 日志信息。
现在我们来修改一下这个记录器的级别,把第一行的DEBUG改为INFO,再运行程序,结果将变为:
0 [main] INFO TestLog4j.TestLog4j - Just testing a log message with priority set to INFO
10 [main] WARN TestLog4j.TestLog4j - Just testing a log message with priority set to WARN
10 [main] ERROR TestLog4j.TestLog4j - Just testing a log message with priority set to ERROR
10 [main] FATAL TestLog4j.TestLog4j - Just testing a log message with priority set to FATAL
10 [main] WARN TestLog4j.TestLog4j - Testing a log message use a alternate form
由于这个Logger的级别变为INFO,而代码(2)是调用debug()函数来输出日志信息时只能当记录器级别为DEBUG时才输出信息,所以代码(2)将不输出信息。
1.2 实例原理
1.2.1 初始化配置信息
如果要通过JAVA的properties文件来配置信息,那么在代码中就要通过PropertyConfigurator.configure()函数从properties文件中加载配置信息,这个函数有三种参数形式:一个properties文件所在路径的String对象,可以是一个properties文件所在路径的URL对象,也可以是一个properties对象。如果要用XML文件来配置信息,则可用类型的
DOMConfigurator()函数来从一个XML文件中加载配置信息。
1.2.2 输出端Appender
在上面的例子中,我们都是简单的把日志信息输出到控制台中。其实在log4j中还可以把日志信息输出到其它的输出端,对于同一个日志信息,我们还可以让它同时输出到多个输出端中,如同时在控制台和文件中进行打印。一个输出端就是一个appender。要在配置文件中定义一个appender有三步:
1) 在定义一个记录器的同时定义出该记录器的输出端appender。在例2的配置文件的第一句log4j.rootLogger = DEBUG, A1中,我们定义了一个根记录器,它的级别为DEBUG,它有一个appender名为A1。定义根记录器的格式为log4j.rootLogger = [ level ], appendName1, appendName2, ...appendNameN。同一个记录器可有多个输出端。
2) 定义appender的输出目的地。定义一个appender的输出目的地的格式为log4j.appender.appenderName = fully.qualified.name.of.appender.class。log4j提供了以下几种常用的输出目的地:
? org.apache.log4j.ConsoleAppender,将日志信息输出到控制台
? org.apache.log4j.FileAppender,将日志信息输出到一个文件
? org.apache.log4j.DailyRollingFileAppender,将日志信息输出到一个,并且每天输出到一个新的日志文件
? org.apache.log4j.RollingFileAppender,将日志信息输出到一个文件,通过指定文件的的尺寸,当文件大小到达指定尺寸的时候会自动把文件改名,如名为example.log的文件会改名为example.log.1,同时产生一个新的example.log文件。如果新的文件再次达到指定尺寸,又会自动把文件改名为example.log.2,同时产生一个example.log文件。依此类推,直到example.log. MaxBackupIndex,MaxBackupIndex的值可在配置文件中定义。
? org.apache.log4j.WriterAppender,将日志信息以流格式发送到任意指定的地方。
? org.apache.log4j.jdbc.JDBCAppender,通过JDBC把日志信息输出到数据库中。
在例2中,log4j.appender.A1 = org.apache.log4j.ConsoleAppender定义了名为A1的appender的输出目的地为控制台,所以日志信息将输出到控制台。
3) 定义与所选的输出目的地相关的参数,定义格式为:
log4j.appender.appenderName.optionName1 = value1
......
log4j.appender.appenderName.optionNameN = valueN
其中一个最常用的参数layout将在下面介绍。
1.2.3 输出格式(布局)layout
通过appender可以控制输出的目的地,而如果要控制输出的格式,就可通过log4j的layout组件来实现。通过配置文件定义一个appender的输出格式,也通常需要两个步骤:
1) 定义appender的布局模式。定义一个appender的布局模式的格式为log4j.appender.appenderName.layout = fully.qualified.name.of.layout.class。Log4j提供的布局模式有以下几种:
? org.apache.log4j.HTMLLayout,以HTML表格形式布局
? org.apache.log4j.PatternLayout,可以灵活地指定布局模式
? org.apache.log4j.SimpleLayout,包含日志信息的级别和信息字符串
在例2 中log4j.appender.A1.layout = org.apache.log4j.PatternLayout定义了名为A1的appender的布局模式为PatternLayout。
2) 定义与所选的布局模式相关的设置信息,定义格式为:
log4j.appender.appenderName.layout.optionName1 = value1
......
log4j.appender.appenderName.layout.optionNameN = valueN
选择了不同的布局模式可能会有不同的设置信息。实例2所选的布局模式PatternLayout的一个PatternLayout为ConversionPattern ,通过定义这个PatternLayout的值,我们可以指定输出信息的输出格式。在例2的配置文件中的定义如下log4j.appender.A1.layout.ConversionPattern = %-4r [%t] %-5p %c %x - %m%n。在下面,我们将介绍布局模式PatternLayout的参数ConversionPattern的各个值代表的含义。
1.2.4 ConversionPattern参数的格式含义
格式名 含义
%c 输出日志信息所属的类的全名
%d 输出日志时间点的日期或时间,默认格式为ISO8601,也可以在其后指定格式,比如:%d{yyy-MM-dd HH:mm:ss },输出类似:2002-10-18- 22:10:28
%f 输出日志信息所属的类的类名
%l 输出日志事件的发生位置,即输出日志信息的语句处于它所在的类的第几行
%m 输出代码中指定的信息,如log(message)中的message
%n 输出一个回车换行符,Windows平台为"rn",Unix平台为"n"
%p 输出优先级,即DEBUG,INFO,WARN,ERROR,FATAL。如果是调用debug()输出的,则为DEBUG,依此类推
%r 输出自应用启动到输出该日志信息所耗费的毫秒数
%t 输出产生该日志事件的线程名
1.3 定义多个输出目的地的实例
从上面的实例原理中我们已经知道,同一个日志信息可以同时输出到多个输出目的地,在这个例子中,我们将实现一个把日志信息同时输出到控制器、一个文件中的实例和数据库中。这个实例的Java代码我们沿用例2中的代码,我们只需修改配置文件即可。这也体现了log4j的灵活性。
例3-a:
create table log4j(
logID int primary key identity,
message varchar(1024),
priority varchar(10),
milliseconds int,
category varchar(256),
thread varchar(100),
NDC varchar(256),
createDate datetime,
location varchar(256),
caller varchar(100),
method varchar(100),
filename varchar(100),
line int
)
例3-b:
#1 定义了两个输出端
log4j.rootLogger = INFO, A1, A2,A3
#2 定义A1输出到控制器
log4j.appender.A1 = org.apache.log4j.ConsoleAppender
#3 定义A1的布局模式为PatternLayout
log4j.appender.A1.layout = org.apache.log4j.PatternLayout
#4 定义A1的输出格式
log4j.appender.A1.layout.ConversionPattern = %-4r [%t] %-5p %c - %m%n
#5 定义A2输出到文件
log4j.appender.A2 = org.apache.log4j.RollingFileAppender
#6 定义A2要输出到哪一个文件
log4j.appender.A2.File = F:nepalonclassesexample3.log
#7 定义A2的输出文件的最大长度
log4j.appender.A2.MaxFileSize = 1KB
#8 定义A2的备份文件数
log4j.appender.A2.MaxBackupIndex = 3
#9 定义A2的布局模式为PatternLayout
log4j.appender.A2.layout = org.apache.log4j.PatternLayout
#10 定义A2的输出格式
log4j.appender.A2.layout.ConversionPattern = %d{yyyy-MM-dd hh:mm:ss}:%p %t %c - %m%n
#11区 定义A3输出到数据库
log4j.appender.A3 = org.apache.log4j.jdbc.JDBCAppender
log4j.appender.A3.BufferSize = 40
log4j.appender.A3.Driver = com.microsoft.jdbc.sqlserver.SQLServerDriver
log4j.appender.A3.URL = jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=nepalon
log4j.appender.A3.User = sa
log4j.appender.A3.Password =
log4j.appender.A3.layout = org.apache.log4j.PatternLayout
log4j.appender.A3.layout.ConversionPattern = INSERT INTO log4j (createDate, thread, priority, category, message) values(getdate(), '%t', '%-5p', '%c', '%m')
配置文件中的6、7、8行显示了输出端为RollingFileAppender的特有参数及其运用的方法。11区显示了输出端为JDBCAppender的特有参数及其运用方法。在这着重讲解一下6、7、8行的作用。6行指定日志信息输出到哪个文件,7行指定日志文件的最大长度,最后要详细介绍8行。第8行的参数是设置备份文件的个数的参数,在这里我们设置为3,表示最多有3个备份文件,具体作用为:
1) 当example3.log文件的大小超过K时,就把文件改名为example3.log.1,同时生成一个新的example3.log文件
2) 当example3.log文件的大小再次超过1K,又把文件改名为example3.log.1。但由于此时example3.log.1已存在,则先把example3.log.1更名为example3.log.2,再把example3.log文件改名为example3.log.1
3) 同理,当example3.log文件的大小再次超过1K,先把example3.log.2文件更名为example3.log.3,把example3.log.1文件更名为example3.log.2,再把example3.log文件改名为example3.log.1
4) 当example3.log文件的大小再次超过1K,先把example3.log.2文件更名为example3.log.3,旧的example3.log.3文件将被覆盖;把example3.log.1文件更名为example3.log.2,旧的example3.log.2文件被覆盖;最后把example3.log文件改名为example3.log.1并覆盖掉旧的example3.log.1文件。
运行结果将分为两部分
在控制器中:
0 [main] INFO TestLog4j.TestLog4j - Just testing a log message with priority set to INFO
11 [main] WARN TestLog4j.TestLog4j - Just testing a log message with priority set to WARN
21 [main] ERROR TestLog4j.TestLog4j - Just testing a log message with priority set to ERROR 21 [main] FATAL TestLog4j.TestLog4j - Just testing a log message with priority set to FATAL
21 [main] WARN TestLog4j.TestLog4j - Testing a log message use a alternate form
在文件example3.log中:
2003-12-18 04:23:02:INFO main TestLog4j.TestLog4j - Just testing a log message with priority set to INFO
2003-12-18 04:23:02:WARN main TestLog4j.TestLog4j - Just testing a log message with priority set to WARN
2003-12-18 04:23:02:ERROR main TestLog4j.TestLog4j - Just testing a log message with priority set to ERROR
2003-12-18 04:23:02:FATAL main TestLog4j.TestLog4j - Just testing a log message with priority set to FATAL
2003-12-18 04:23:02:WARN main TestLog4j.TestLog4j - Testing a log message use a alternate form
1.4 配置log4j的总结
这个教程到这里,关于配置log4j的配置文件的基本原理已经讲完了,而且通过例3我们已经可以完成基本的日志工作了。现在,我们就做一个总结。配置一个配置文件的基本步骤如下:
1) 定义一个Logger。在定义Logger时指定该Logger的级别级其输出目的地。定义Logger的格式为
log4j.rootLogger = [ level ], appendName1, appendName2, ...appendNameN。
2) 定义appender的输出目的地。定义一个appender的输出目的地的格式为
log4j.appender.appenderName = fully.qualified.name.of.appender.class。
log4j提供的输出端有ConsoleAppender、FileAppender 、DailyRollingFileAppender、RollingFileAppender和WriterAppender。
3) 定义appender的除布局模式外的其它相关参数,如例3中第6、7、8定义了A2的相关参数。定义格式为
log4j.appender.appenderName.optionName1 = value1
......
log4j.appender.appenderName.optionNameN = valueN
如果除了布局模式外不需要定义别的参数,可跳过这一步(如例3中的A1)。
4) 定义appender的布局模式。定义一个appender的布局模式的格式为
log4j.appender.appenderName.layout = fully.qualified.name.of.layout.class。
布局模式其实也是步骤3)中的一个部分,只是布局模式参数是每一个appender必须定义的参数。Log4j提供的布局模式有HTMLLayout、PatternLayout和SimpleLayout。
5) 定义与所选的布局模式相关的设置信息,定义格式为
og4j.appender.appenderName.layout.optionName1 = value1
......
log4j.appender.appenderName.layout.optionNameN = valueN
2 记录器的层次Logger hierarchy
2.1 何为记录器的层次hierarchy
首先,我们先看一下何为层次,以我们最熟悉的继承为例,下面是一张类图
在这个继承体系中,类B是类C的父类,类A是类C的祖先类,类D是类C的子类。这些类之间就构成一种层次关系。在这些具有层次关系的类中,子类都可继承它的父类的特征,如类B的对象能调用类A中的非private实例变量和函数;而类C由于继承自类B,所以类B的对象可以同时调用类A和类B中的非private实例变量和函数。
在log4j中,处于不同层次中的Logger也具有象类这样的继承关系。
2.2 记录器的层次
如果一个应用中包含了上千个类,那么也几乎需要上千个Logger实例。如何对这上千个Logger实例进行方便地配置,就是一个很重要的问题。Log4J采用了一种树状的继承层次巧妙地解决了这个问题。在Log4J中Logger是具有层次关系的。它有一个共同的根,位于最上层,其它Logger遵循类似包的层次。下面我们将进行介绍。
2.2.1 根记录器root logger
就象一个Java中的Object类一样,log4j中的logger层次中有一个称之为根记录器的记录器,其它所有的记录器都继承自这个根记录器。根记录器有两个特征:
1) 根记录器总是存在。就像Java中的Object类一样,因为用log4j输出日志信息是通过记录器来实现的,所以只要你应用了log4j,根记录器就肯定存在的。
2) 根记录器没有名称,所以不能通过名称来取得根记录器。但在Logger类中提供了getRootLogger()的方法来取得根记录器。
2.2.2 记录器的层次
Logger遵循类似包的层次。如
static Logger rootLog = Logger.getRootLogger();
static Logger log1 = Logger.getLogger("test4j");
static Logger log2 = Logger.getLogger("test4j.test4j2");
static Logger log3 = Logger.getLogger("test4j.test4j2.test4j2");
那么rootLog是log2的祖先子记录器,log1是log2的父子记录器,log3是log2的子记录器。记录器象Java中的类继承一样,子记录器可以继承父记录器的设置信息,也可以可以覆写相应的信息。
首先先看一下记录器层次中的继承有什么用处。假设程序中的每个包都具有一些基本的日志信息,而包中的不同包可能会有些额外的日志信息要输出,这种情况就可以象处理Java中的类一样,运用Logger中的层次关系来达到目的。假设有个名为A的包,我包下的所有类都要把日志信息输出到控制台中;A.B包除了输出到控制台外还要输出到文件中;A.C包除了输出到控制台中还要输出到HTML文档中。这样我们就可以通过定义一个父记录器A,它负责把日志信息输出到控制台中;定义一个A的子记录器A.B,它负责把日志信息输出到文件中;定义一个A的子记录器A.C,它负责把日志信息输出到HTML文档中。
记录器遵循的是类似包的层次,这样做为我们带来了大大的方便。Logger类中的getLogger()方法可以取得Logger对象,这个方法有三种参数形式String、Class和URL,其实不论是用哪一种,最终都是通过记录器的名字来取得记录器对象的。如果要取得一个名为A.B的记录器对象,我们可以Logger.getLogger("A.B")。但从上面的例子中,我们都是通过Logger.getLogger(TestLog4j.class.getName())这种方法来取得记录器对象。这是为什么呢?现在我们假设A.B的包下有一个类BClass,那么我们调用BClass.class.getName()得到的是这个类的全名A.B.BClass。所以当调用Logger.getLogger(BClass.class.getName())时,最理想的情况是返回名为A.B.BClass的记录器对象。但是如果不存在名为A.B.BClass的记录器时它会怎样呢?其实通过Logger类的getLogger方法取得记录器时存在下面两种情况:
1) 如果存在与所要找的名字完全相同的记录器,则返回相应的记录器对象。
当调用Logger.getLogger(BClass.class.getName())时,如果定义了名为A.B.BClass的记录器,它就返回该记录器的对象。
2) 但如果找不到,它会尝试返回在记录器层次中与所要找的记录器最接近的记录器对象。
当调用Logger.getLogger(BClass.class.getName())时,如果没有定义了名为A.B.BClass的记录器,那会尝试返回名为A.B的记录器的对象;如果又没有定义名为A.B的记录器,它会尝试返回名为A的记录器的对象;如果也没定义名为A的记录器,它就会返回根记录器的对象,而根记录器是必须存在的,所以你总能得到一个记录器对象。
好了,现在我们回到前面的问题,我们为什么总要通过Logger.getLogger(BClass.class.getName())这种以类全名作为参数来取得记录器对象呢?其实这是为了管理方便。因为我们在定义设计Logger时也遵循类似包的规则,使设计器的名称与程序中的类包对应。如上面的假设中我们的程序中有A包,A包下有B包和C包,B包下又有类BClass,那么我们就可使设计器的名为A、A.B、A.C、A.B.BClass,以此类推。那么当我们通过类命名来取得设计器对象时,总能取到与所要的设计器最接近的设计器对象。
2.3 如何应用记录器的层次
2.3.1 如果定义及获取不同层次的记录器
任何一个记录器的使用都有两个步骤:
1) 在配置文件中定义相应的记录器。
在配置文件中定义记录器的格式有两种
? 定义根记录器的格式为
log4j.rootLogger = [ level ], appendName1, appendName2, ...appendNameN
? 定义一个非根记录器的格式为
log4j.logger.loggerName1 = [ level ], appendName1,...appendNameN
......
log4j.logger.loggerNameM = [ level ], appendName1, ...appendNameN
我们可以定义任意个非根记录器。
2) 在代码中调用Logger类的取得记录器方法取得相应的记录器对象。
要取得根记录器对象可通过Logger.getRootLogger()函数,要取得非根记录器可通过Logger.getLogger()函数。
理论知道就讲到这里,纸上得来终觉浅,下面,我们来小小演练一下。
例4-a:
package TestLog4j;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.Priority;
import TestLog4j.TestLog4j2.TestLog4j2;
public class TestLog4j
{
static Logger logger = Logger.getLogger(TestLog4j.class.getName()); //(2)
public TestLog4j(){}
public static void main(String[] args)
{
//同时输出到控制台和一个文件的实例并实现了Logger的继承
PropertyConfigurator.configure("F:nepalonlog4j2.properties");
logger.debug("Start of the main() in TestLog4j");
logger.info("Just testing a log message with priority set to INFO");
logger.warn("Just testing a log message with priority set to WARN");
logger.error("Just testing a log message with priority set to ERROR");
logger.fatal("Just testing a log message with priority set to FATAL");
logger.log(Priority.WARN, "Testing a log message use a alternate form");
logger.debug(TestLog4j.class.getName());
TestLog4j2 testLog4j2 = new TestLog4j2(); //(1)
testLog4j2.testLog();
}
}
在类TestLog4j中我们调用了另一个类TestLog4j2,下面看一下类TestLog4j2的代码。
例4-b:
package TestLog4j.TestLog4j2;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.Priority;
public class TestLog4j2
{
static Logger logger = Logger.getLogger(TestLog4j2.class.getName()); //(1)
public TestLog4j2(){}
public void testLog()
{
//同时输出到控制台和一个文件的实例
PropertyConfigurator.configure("F:nepalonlog4j2.properties");
logger.debug("2Start of the main()");
logger.info("2Just testing a log message with priority set to INFO");
logger.warn("2Just testing a log message with priority set to WARN");
logger.error("2Just testing a log message with priority set to ERROR");
logger.fatal("2Just testing a log message with priority set to FATAL");
logger.log(Priority.DEBUG, "Testing a log message use a alternate form");
logger.debug("2End of the main()");
}
}
最后我们来看一下配置文件。
例4-c:
log4j2.properties文件内容
#1区
#### Use two appenders, one to log to console, another to log to a file
log4j.rootLogger = debug, stdout
#2区
#Print only messages of priority WARN or higher for your category
log4j.logger.TestLog4j= , R
log4j.logger.TestLog4j.TestLog4j2=WARN
#3区
#### First appender writes to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
#4区
#### Second appender writes to a file
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=F:nepalonclassesTestLog4jexample.log
# Control the maximum log file size
log4j.appender.R.MaxFileSize=100KB
# Archive log files (one backup file here)
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d{yyyy-MM-dd hh:mm:ss}:%p %t %c - %m%n
先看一下运行结果。
在控制台中的结果为:
DEBUG [main] (?:?) - Start of the main() in TestLog4j
INFO [main] (?:?) - Just testing a log message with priority set to INFO
WARN [main] (?:?) - Just testing a log message with priority set to WARN
ERROR [main] (?:?) - Just testing a log message with priority set to ERROR
FATAL [main] (?:?) - Just testing a log message with priority set to FATAL
WARN [main] (?:?) - Testing a log message use a alternate form
DEBUG [main] (?:?) - TestLog4j.TestLog4j
WARN [main] (?:?) - 2Just testing a log message with priority set to WARN
ERROR [main] (?:?) - 2Just testing a log message with priority set to ERROR
FATAL [main] (?:?) - 2Just testing a log message with priority set to FATAL
输出文件的结果为:
2003-12-19 04:19:44:DEBUG main TestLog4j.TestLog4j - Start of the main() in TestLog4j
2003-12-19 04:19:44:INFO main TestLog4j.TestLog4j - Just testing a log message with priority set to INFO
2003-12-19 04:19:44:WARN main TestLog4j.TestLog4j - Just testing a log message with priority set to WARN
2003-12-19 04:19:44:ERROR main TestLog4j.TestLog4j - Just testing a log message with priority set to ERROR
2003-12-19 04:19:44:FATAL main TestLog4j.TestLog4j - Just testing a log message with priority set to FATAL
2003-12-19 04:19:44:WARN main TestLog4j.TestLog4j - Testing a log message use a alternate form
2003-12-19 04:19:44:DEBUG main TestLog4j.TestLog4j - TestLog4j.TestLog4j
2003-12-19 04:19:44:WARN main TestLog4j.TestLog4j2.TestLog4j2 - 2Just testing a log message with priority set to WARN
2003-12-19 04:19:44:ERROR main TestLog4j.TestLog4j2.TestLog4j2 - 2Just testing a log message with priority set to ERROR
2003-12-19 04:19:44:FATAL main TestLog4j.TestLog4j2.TestLog4j2 - 2Just testing a log message with priority set to FATAL
首先,先来看一下配置文件都有些什么东西。
1) 在1区中定义了一个根记录器。这个根记录器具有DEBUG级别并有一个名称为stdout的输出端appender。
2) 2区中的内容是这一节的重点,也是应用到记录器层次的地方,但其实也只有两句,充分体现了log4j的简单性。在这里,我们定义了两个名称分别为TestLog4j和TestLog4j.TestLog4j2设计器。
? 在定义TestLog4j记录器时没有指定级别,所以它的级别继承自它的父记录器,即要记录器,所以它的级别也为DEBUG。在定义TestLog4j记录器时又定义了一个名称为R的输出端,所以它的输出端有两个,一个从根记录器继承而来的名为stdout的输出端,另一个为在此定义的名为R的输出端。在此需要注意的是,在定义记录器时必须先定义记录器的级别,然后才是记录器的输出端。如果只想定义输出端而不定义级别,则虽然级别可以为空,但逗号分隔符不能省略。如定义TestLog4j记录器的做法。
? 在定义TestLog4j.TestLog4j2记录器时又指定了它的级别,由于一个记录器的级别只能有一个,所以新指定的级别将覆写掉它的父记录器的级别(这就象Java中的多态)。我们没有定义TestLog4j.TestLog4j2记录器的输出端,所以它的输出端将从它的父记录器中继承而来。它的父记录器为estLog4j记录器,所以它和estLog4j记录器一样具有两个名称分别为stdout和R的输出端。
3) 剩下的3区和4区分别设置了两个输出端的参数值。
接下来,回到我们的代码,看一下是如何取得记录器,在取记录器时又发生了什么。
1) 例4-a中的代码(2)中,语句Logger.getLogger()中的参数TestLog4j.class.getName()的值为TestLog4j. TestLog4j,所以此语句的结果是取得一个名为TestLog4j. TestLog4j的记录器的对象。但在配置文件中并没有定义这样的记录器,所以最终将返回与所需的名称TestLog4j. TestLog4j最接近的记录器对象,即名为TestLog4j的记录器的对象。
2) 例4-b中的代码(1)的原理与例4-a中的代码(2)相似,期望取得的是名为TestLog4j.TestLog4j2. TestLog4j2的记录器对象,但最终返回的是TestLog4j.TestLog4j2记录器的对象。
三 log4j与J2EE的结合
到目前为止,这篇文章讲的都是如何在application中应用log4j,而Java现在的应用主流是J2EE和J2ME。现在,我们来看一下要如何在J2EE开发中应用log4j。其实在Web application中应用log4j也很简单,与在application中应用log4j不同之处就是要在所有应用log4j的代码之前对log4j进行初始化。所以,我们在web application中就要把log4j的初始化工作独立出来,把它放在Servlet中。下面,我们看一个例子。
例5-a:
进行初始化的Servlet:
import org.apache.log4j.PropertyConfigurator;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* log4j.jar的初始化类,参考web.xml
*/
public class Log4jInit extends HttpServlet
{
public void init()
{
//通过web.xml来动态取得配置文件
String prefix = getServletContext().getRealPath("/");
String file = getInitParameter("log4j-init-file");
// 如果没有给出相应的配置文件,则不进行初始化
if(file != null)
{
PropertyConfigurator.configure(prefix+file); //(1)
}
}
public void doGet(HttpServletRequest req, HttpServletResponse res)
{}
}
下面来看一下这个Servlet在web.xml中的定义。
例5-b:
log4j-init
TestLog4j.Log4jInit
log4j-init-file
sort.properties
1
因为log4j的初始化要在所有的log4j调用之前完成,所以在web.xml文件中,我们一定要把对应的Servlet定义的load-on-startup应设为1,以便在Web容器启动时即装入该Servlet。
完成了这两个步骤这后,我们就可以象在application开发中一样在web application任何地方应用log4j了。下面是在javabean中的应用的一个例子。
例5-c:
import org.apache.log4j.Logger;
public class InfoForm
{
static Logger logger = Logger.getLogger(InfoForm.class);
protected String title;
protected String content;
public InfoForm() {}
public void setTitle(Object value)
{
logger.debug("nepalon:title = " + title);
title = value;
}
public String getTitle()
{
logger.debug("nepalon:title = " + title);
return title;
}
public void setContent(String value)
{
content = value;
logger.debug("nepalon: content() = " + content);
}
public String getContent()
{
logger.debug("nepalon: content = n" + content);
return content;
}
}