您的位置:首页 > 其它

DWR使用:文字提示功能实现、简易聊天室

2007-05-12 11:14 751 查看
DWR使用:

1.文字提示功能实现。
说明:使用java.util.ResouceBundle来读取properties文件中的内容,properties文件可以放在读取类的相同目录下。
ResourceBundle在读取properties文件时使用ISO8859-1编码,需要进行编码转换。

book.java

package com.dwr.test;

import java.util.ResourceBundle;
import java.util.Locale;

public class book {
private ResourceBundle resource;

public book(){
resource = ResourceBundle.getBundle("com.dwr.test.book");
}
public String getDescription(String strKey){
return ISO2GB(resource.getString(strKey));
}

public static String ISO2GB(String src) {
if (src != null) {
try {
return new String(src.getBytes("ISO-8859-1"), "GBK");
}
catch (Exception e) {
e.printStackTrace();
return null;
}
} else {
return null;
}
}

}

book.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<script type='text/javascript' src='dwr/interface/Book.js'></script>
<script type='text/javascript' src='dwr/engine.js'></script>
<script type='text/javascript' src='dwr/util.js'></script>
<script language='javascript'>
function getBookData(Obj) {
Book.getDescription(Obj.id,setBookData);
}
function setBookData(description) {
DWRUtil.setValue('info', description);
}
function clearData() {
DWRUtil.setValue('info', '');
}
</script>
<title>個人著/譯作</title>
</head>
<body>
<div id="ajax" onmouseover="getBookData(this);" onmouseout="clearData();">
<a href="http://www.gotop.com.tw/waweb2004/home/home.aspx?pg=HM010X&bn=AXP011800">
<small><img style="border: 0px solid ; width: 80px; height: 110px; float: left;" alt="Ajax in action 中文版" title="Ajax in action 中文版" src="http://www.gotop.com.tw/Waweb2004/WawebImages/BookL/XP011800.jpg" hspace="10" vspace="2">
</small>
</a>
</div>
<div id="spring" onmouseover="getBookData(this);" onmouseout="clearData();">
<a href="http://www.gotop.com.tw/waweb2004/home/home.aspx?pg=HM010X&bn=ACL021000">
<small><img style="border: 0px solid ; width: 80px; height: 110px; float: left;" alt="Spring 技術手冊" title="Spring 技術手冊" src="http://www.gotop.com.tw/Waweb2004/WawebImages/BookL/CL021000.jpg" hspace="10" vspace="2">
</small></a>
</div>
<div id="java" onmouseover="getBookData(this);" onmouseout="clearData();">
<a href="http://www.gotop.com.tw/waweb2004/home/home.aspx?pg=HM010X&bn=ACL020931">
<small><img style="border: 0px solid ; width: 80px; height: 110px; float: left;" alt="Java 學習筆記" title="Java 學習筆記" src="http://www.gotop.com.tw/Waweb2004/WawebImages/BookL/CL020931.jpg" hspace="10" vspace="2">
</small></a>
</div>
<br><br><br><br><br><br>
<div id="info"></div>
</body>
</html>

book.properties

java=Java 學習筆記的介紹…BlaBla...
spring=Spring 技術手冊的介紹…BlaBla...
ajax=Ajax in action 中文版的介紹…BlaBla...

dwr.xml中加入

<create creator="new" javascript="Book">
<param name="class" value="com.dwr.test.book"/>
</create>

2.简单聊天室功能 

chat.html

<html>
<head>
<script type='text/javascript' src='dwr/interface/Chat.js'></script>
<script type='text/javascript' src='dwr/engine.js'></script>
<script type='text/javascript' src='dwr/util.js'></script>
<script language="javascript">

function sendMessage()
{
var text1 = DWRUtil.getValue("text");
DWRUtil.setValue("text", "");
Chat.addMessage(gotMessages, text1);
}

function gotMessages(messages)
{
var chatlog = "";
for (var data in messages)
{
chatlog = "<div>" + messages[data].text +"</div>" + chatlog;
}
DWRUtil.setValue("chatlog", chatlog);

refresh();
}

//自动刷新
function refresh(){
setTimeout("queryMessage()", 2000);
}

function queryMessage() {
Chat.getMessages(gotMessages);
}

//CRRL+ENTER发送消息功能的实现
function replayTopic(){
if(document.all.text.value==null || document.all.text.value=="")
{
alert("不能发送空消息! ");
document.all.text.focus();
return false;
}
return true;
}

function ctlent(eventObj) {
if(eventObj.ctrlKey && eventObj.keyCode == 13) {
if(replayTopic()) {
sendMessage();
}
}
}

</script>
<body onload="refresh();">
<p>Messages:</p>
<div id="chatlog"></div>
<p>
Your Message:
<input id="text" type="text" onKeyDown="ctlent(event)"/>
<input type="button" value="Send" onclick="sendMessage()"/>
</p>
</body>
</html>

chat.java

package com.dwr.test;

import java.util.List;
import java.util.LinkedList;

public class Chat
{
static LinkedList messages =new LinkedList();

public List addMessage(String text)
{
if (text != null &&
text.trim().length() > 0)
{
messages.addFirst(new Message(text));
while (messages.size() > 10)
{
messages.removeLast();
}
}

return messages;
}

public List getMessages()
{
return messages;
}

}

Message.java

package com.dwr.test;

public class Message
{
public Message(String newtext)
{
text = newtext;
if (text.length() > 256)
{
text = text.substring(0, 256);
}
text = text.replace('<', '[');
text = text.replace('&', '_');
}

public long getId()
{
return id;
}

public String getText()
{
return text;
}

long id = System.currentTimeMillis();
String text;
}

dwr.xml中加入

<create creator="new" javascript="Chat">
<param name="class" value="com.dwr.test.Chat"/>
</create>
<convert converter="bean" match="com.dwr.test.Message"/>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: