posts - 165, comments - 198, trackbacks - 0, articles - 1
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

页面嵌入在线聊天 (prototype)

Posted on 2007-06-13 10:22 G_G 阅读(1427) 评论(3)  编辑  收藏 所属分类: javascript
问题 : 提供给B/S 结构一个实时的运行展现
解决:   prototype 的 Ajax.PeriodicalUpdater 更新
            prototype 的    Ajax.Request 语句提交
            action 语句的组织展现

http://www.blogjava.net/Files/Good-Game/take.rar

    没有lib 大家可以就加 struct 相关 lib 就可以了

说明: 
      

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  
<head>
          
<SCRIPT language="Javascript" src="prototype1.5.0.js"></SCRIPT>
  
</head>
  
  
<body onload="Said()"> 
      
<INPUT id='name1' type="text" size="10" value="请输入名字" onfocus="if($('name1').value=='请输入名字') $('name1').innerText='' " />
      
<INPUT id='text1' type="text" onkeydown="if(event.keyCode==13){event.keyCode=0; onb();}"/>
      
<INPUT id='button1' type="button" value="test" onclick='onb()' />
  
</body>
  
<div id='div1'/>
</html>
<script type="text/javascript"  >
        //语句异步提交给服务器 Ajax.Request
    
function onb(){                                         
      
if($('name1').value=='请输入名字' || $('name1').value==''){
          window.alert('请输入名字');
          
return 
      }

      
      
var url = 'http://192.168.1.130:7000/xxs/mySaid.do?name='+$F('name1')+'&take='+$F('text1');                
      new Ajax.Request(
        url,
        
{
            method: 'get',
            onComplete: action              
        }
);
        
        $('name1').readOnly 
= true
        $('name1').style.background 
= 'lightblue'
        $('text1').innerText
='' 
    }

       //局部刷新 并提取 语句
    
function Said(){
        
var url = 'http://192.168.1.130:7000/xxs/mySaid.do';  
        new Ajax.PeriodicalUpdater(
            'div1',
            url,
            
{
                method: 'get',
                onComplete: SumSaid,
                evalScripts: 
true,
                frequency: 
1,
                decay: 
1
            }

        );
    }

       //展现
    
function SumSaid(req){
        $('div1').innerHTML 
= req.responseText;
    }


    
function action(req){
    }

    
</script>
    

Action
   private static int SIZE = 30 ;
     private List list = new ArrayList();

    
public ActionForward execute(
        ActionMapping mapping,
        ActionForm form,
        HttpServletRequest request,
        HttpServletResponse response) 
{
        String take 
= request.getParameter("take") ;
        String name 
= request.getParameter("name") ;
        response.addHeader(
"Cache-Control","no-cache"); 
        
        request.setAttribute(
"said",DueiLie(name,take));

        
try {
            request.setCharacterEncoding(
"gb2312");
            response.setCharacterEncoding(
"gb2312");
        }
 catch (Exception e) {
        }


        
return mapping.findForward("list");
    }

     
        //语句的组织
    
public String DueiLie(String name,String take){
        
        StringBuffer strb  
= new StringBuffer();
        
if(name==null||take==null);
        
else{
            strb.append( name ).append(
" : ").append(take).append("</br>") ;
            
if(list.size()==SIZE){
                list.remove(list.get(
0));
                list.add(strb.toString() );
            }
else{
                list.add( strb.toString() );
            }

        }

        StringBuffer takes 
= new StringBuffer();
        Iterator it 
= list.iterator();
        
while(it.hasNext()){
            takes.insert( 
0,(String)it.next() );
        }

        
return takes.toString();
    }

jsp ( list ) 中文处理 jsp 就一句话
<%= new String(((String)request.getAttribute("said")).getBytes("iso8859-1"),"GBK") %>

心得:别人做到了 我也能做到
         要学的东西很多 多看书 多留意 多保护好身体

评论

# re: 页面嵌入在线聊天 (prototype)[未登录]  回复  更多评论   

2007-06-13 13:30 by 刘明
可以给个Demo演示吗?本人对struct不太懂。

# re: 页面嵌入在线聊天 (prototype)  回复  更多评论   

2007-06-13 14:57 by G_G
@刘明
上头下载的 就是 不过没 lib :( 有点大
你可以看 .classpath 放lib 我的 .classpath 有点乱
你加 struct 的 lib 就可以了
{ 有 动态建表(2) bean[] 直接到建表 (扩展也很好) 的文档的 lib 在里面 }
对不起大家了
有问题 到我
liukaiyi@gmail.com 留言 谢谢

# re: 页面嵌入在线聊天 (prototype)  回复  更多评论   

2007-06-13 17:16 by IT进行时
还是用smack吧,简单又“正统”。

只有注册用户登录后才能发表评论。


网站导航: