日期:2014-05-16  浏览次数:20697 次

dwr框架,有个不明白的地方,请高手来看看,代码己贴出。
这里是用DWR框架,实现AJAX写的一段代码,主要代码贴出来了,在JSP文件里红色部分的
KeyWrod.getName(key, callback);callback为什么当参数传进getName函数里,JAVA部分getName函数只有一个参数啊,是用这个框架与后台交互都要这么写么?
HTML code

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <title>Struts 2 Ajax Google</title>

        <script type='text/javascript' src='/struts2ajaxgoogle/dwr/engine.js'></script>
        <script type='text/javascript'
            src='/struts2ajaxgoogle/dwr/interface/KeyWrod.js'></script>
        <script type='text/javascript' src='/struts2ajaxgoogle/dwr/util.js'></script>

        <script type="text/javascript" src="./jq/jquery-1.6.2.js"></script>
        <script type="text/javascript">
    $(document).ready(function() {
    
        $("#search").keyup(function() {
            var key = $("#search").val();
            [color=#FF0000]KeyWrod.getName(key, callback);[/color]        });
        function callback(date) {
            var key = "";
            for (i = 0; i < date.length; i++) {
                key = key + date[i][1]+ "</br>";
            }
            $("#result").html("<br>"+key+"</br>");
        }
        });
</script>
    </head>
    <body>
        <center>
            <img style="padding-top: 58px;" src="img/1.jpg">
            <br />
            <br />
            <input type="text" id="search"
                style="width: 600px; height: 38px; font-size: 20px; font-weight: bold;" />
            <input type="button" id="sub" value="Google Search"
                style="height: 40px;" />
            <br />
            <div id="result"></div>
        </center>
    </body>
</html>




Java code

package action;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionSupport;

import dao.Db;

public class KeyAction extends ActionSupport {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public List getName(String key) throws Exception {

        Db db = new Db();

        ResultSet rs = db.getKeyWord(key);
        List list = new ArrayList();
        List tempList = new ArrayList();
        ResultSetMetaData md = rs.getMetaData();
        int columnCount = md.getColumnCount();
        while (rs.next()) {

            for (int i = 1; i <= columnCount; i++) {
                tempList.add(rs.getObject(i));
            }
            list.add(tempList);
        }

        return list;
    }

}




------解决方案--------------------
这个dwr中定义的方式,最后一个参数表示回调函数。