您的位置:首页 > 其它

AJAX框架 jMaki (三) – 用日历(Calendar)和表格(Table)实现的Blog首页

2007-12-18 22:38 441 查看
下面使用jMaki中的Yahoo Calendar和Yahoo DataTable 来建立一个迷你Blog首页。这个小程序可以在选择日历的某个日期后,Data Table中只列出这个日期所发表的文章。
jMaki中的Yahoo Calendar和Yahoo DataTable 都是Yahoo UI Library 中的控间, jMaki的作用只是在现有的控件之上作了一些包装(wrapper), 这样,这些Widget可以通过jMaki框架进行通讯,通过很少的代码或者配置就可以完成一个功能丰富的应用程序,也就是通常所说的mashup.
使用NetBeans[u]开发迷你Blog[u]首页的步骤[/u][/u]
1. NetBeans 菜单”文件”-”新建项目”.在出现的”新建项目”窗口中,”类别”选择”Web”, 项目选择”Web应用程序”.点击”下一步”2. 在”新建项目窗口中”, “项目名称”中输入”jMakiCalendarTable”项目. 点击”下一步”3. 在”框架”窗口中,选择”jMaki Ajax Framework” . 点击”完成”.4. NetBeans 中出现 jMakiCalendarTable 项目的同时,也会出现在”组件面板”中出现jMaki的Widgets, 包括Dojo, Flickr, Google, Yahoo等中的一些Widgets. 如果没有出现”组件面板”, 选择菜单”窗口”-> “组件面板”.5. 在netBeans中, 用鼠标直接把”jMaki Yahoo”下的Calendar拖到 jMakiCalendarDemo 项目中的index.jsp文件中. 通常日历会放在左侧栏或者右侧栏中. 这个例子中放在中.把“jMaki Yahoo“下的”Data Table”拖到“Main Content Area”下。


6. 修改index.jsp 中”yahoo.dataTable”的内容
value="{columns : [
{ label : 'Post Date', id : 'postdate'},
{ label : 'Title', id : 'title'},
{ label : 'Author', id : 'author'}
],
rows : [
{ postdate : '2007/09/07', title : 'NetBeans New and Cool', author : 'Tim Boudreau'},
{ postdate : '2007/09/07', title : 'NetBeans Mobility', author : 'Ada Li'},
{ postdate : '2007/09/07', title : 'Creating Web 2.0 Rich Internet Applications', author : 'Michael Li'},
{ postdate : '2007/09/08', title : 'AJAX and JSF', author : 'Ada Li'},
{ postdate : '2007/09/09', title : 'Ruby on Rails in the Enterprise', author : 'Liang Ye'},
{ postdate : '2007/09/09', title : 'Beans Binding and the Swing Application Framework', author : 'Joey Shen'}
]
}" />
7. 初始运行jMakiCalendarTable 结果如下:


8. 要完成这个迷你Blog首页,要做以下几部分工作 获得Calendar的日期

日期发送给服务器端

服务器端返回给客户端列表,列表只包含对应日期的信息

返回的列表信息更新到DataTable

要获得Calendar的日期,根据之前介绍的“发布/订阅机制(publish/subscribe machnism)”, 在index.jsp 的calendar Widget下加入 以下代码代码


                


                    Sidebar Content Here


                    


                    





                                        


                
 因为在web/resources/yahoo/calendar/compenent.js中已经发布(publish)了”/yahoo/calendar/onSelect”, 因此在这段JavaScript中可以直接订阅(subscribe)“/yahoo/calendar/onSelect” topic, 这样在选择Calendar的某个日期时,调用函数calendarListener().
函数calendarListener()完成两件事情, 一个是得到Calendar下显示选中的日期,日期格式为“yyyy/mm/dd”,并显示在定义好的中,一个是向服务器端发送AJAX请求, 是用 jmaki.doAjax()函数实现的.
SelectCallBack()函数是AJAX请求返回的回调函数, 得到服务器端返回的信息后, 发布(publish) “"/table/topic/clear"和”/table/topic/addRows”, 并传递了相关参数. 相应地在 yahoo.dataTable 中订阅(subscribe)这些”
topic”
修改dataTable Widget为:


                    

                    value="{columns : [


                    { label : 'Post Date', id : 'postdate'},


                    { label : 'Title', id : 'title'},


                    { label : 'Author', id : 'author'}


                    ],


                    rows : []


                    }"


                /> 
DataTable中 rows内容为空,下面要把这部分信息放在服务器端的Servlet中,9. 修改后的index.jsp的源代码如下: 


<%@ taglib prefix="a" uri="http://jmaki/v1.0/jsp" %>




"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">







    


        


        Page Title


        


    


    


        


            


            


                Application Name



                


                


                    


                    



                        Feedback |


                        Site Map |


                        Home


                    



                    


                
 


            
 




            


                


                    Sidebar Content Here


                    


                    



                                        


                 




                


                    


                    Main Content Area




                    

                    value="{columns : [




                    ...{ label : 'Post Date', id : 'postdate'},




                    ...{ label : 'Title', id : 'title'},




                    ...{ label : 'Author', id : 'author'}


                    ],


                    rows : []


                    }"




                />


                 


        


             


         


    





 
10. 新建BlogServlet, 作为服务器端接受,处理信息以及返回信息的类. 鼠标右键盘选择项目"jMakiCalendarTable", 选择"新建"->"Servlet",
类名:BlogServlet
包:com.sun.sdn.demo.jmaki
点击"下一步"



下一个"配置Servlet部署"窗口不做修改, 点击”完成”
修改BlogServlet中protected void processRequest()方法, 内容如下
 


    protected void processRequest(HttpServletRequest request, HttpServletResponse response)




    throws ServletException, IOException ...{




        String [][] blogList = ...{




            ...{"2007/09/07", "NetBeans New and Cool","Tim Boudreau"},




            ...{"2007/09/07","NetBeans Mobility","Ada Li"},




            ...{"2007/09/07","Creating Web 2.0 Rich Internet Applications","Michael Li"},




            ...{"2007/09/08","AJAX and JSF","Ada Li"},




            ...{"2007/09/09","Ruby on Rails in the Enterprise","Liang Ye"},




            ...{"2007/09/09","Beans Binding and the Swing Application Framework","Joey Shen"}


        };


        


        String dateParam = request.getParameter("date");


        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");


        boolean listAll = false;


        Date date = null;




        if (dateParam == null || "".equals(dateParam))...{


            date = Calendar.getInstance().getTime();


            listAll = true;




        }else ...{




            try ...{


                date = sdf.parse(dateParam);




            } catch (ParseException ex) ...{


                ex.printStackTrace();


            }


        }


        String dateStr = sdf.format(date);


        


        response.setContentType("text/html;charset=UTF-8");


        PrintWriter out = response.getWriter();


        


        out.println("[");


        


        boolean first = true;


        




        for (int i=0; i

            String[] blogItem = blogList[i];




            if (listAll || blogItem[0].equals(dateStr))...{ //Date match




                if (!first)...{


                    out.println(",");


                }


                out.println("{");


                out.println("postdate: '" + blogItem[0] + "', ");


                out.println("title: '" + blogItem[1] + "', ");


                out.println("author: '" + blogItem[2] + "' ");


                out.println("}");


                first = false;


            }//End if


        }//End i blogList loop


        


        out.println("]");//end rows


        out.flush();


        out.close();


    }processRequest()主要作用是根据日期,筛选显示内容, 并以JSON格式返回给客户端.

11. 最终运行结果如下:



最后需要提到的是, jMaki还在发展中, 请到https://ajax.dev.java.net/下载最新的包和plug-in.

参考:
https://ajax.dev.java.net/samples/samples.htmlhttps://ajax.dev.java.net/http://wiki.java.net/bin/view/Projects/jMakiDataModelshttp://developer.yahoo.com/yui/datatable/http://blogs.sun.com/arungupta/entry/jmaki_filters_runners_mashuphttp://blogs.sun.com/patriceg/ http://www.javapassion.com/   

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1783783
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐