`

DWR 应用实例(一) 新闻发布系统,多种新闻类型之间切换无刷新

    博客分类:
  • AJAX
阅读更多
最近闲着用dwr做了个类似 163 新闻导航,在多种新闻类型下切换,无须刷新:
step1: 下载DWR包
step2: 构建数据库
sql 代码
  1. if exists (select 1   
  2.             from  sysobjects   
  3.            where  id = object_id('AJAX_NEW')   
  4.             and   type = 'U')   
  5.    drop table AJAX_NEW   
  6. go   
  7.   
  8.   
  9. /*==============================================================*/   
  10. /* Table: AJAX_NEW                                              */   
  11. /*==============================================================*/   
  12. create table AJAX_NEW (   
  13.    ID                   integer              not null,   
  14.    NEW_TITLE            VARCHAR(100)         null,   
  15.    NEW_AUTHER           VARCHAR(30)          null,   
  16.    NEW_TYPE             VARCHAR(30)          null,   
  17.    NEW_CONTENT          VARCHAR(500)         null,   
  18.    constraint PK_AJAX_NEW primary key  (ID)   
  19. )   
  20. go   

step3 搭建Struts,Hibernate框架,生成Hibernae配置文件以及po类:

java 代码
  1. package com.fzfx88.conf;   
  2.   
  3. /**  
  4.  * AbstractAjaxNew generated by MyEclipse - Hibernate Tools  
  5.  * po 抽象类  
  6.  * auther huguoqing  
  7.  */  
  8.   
  9. public abstract class AbstractAjaxNew  implements java.io.Serializable {   
  10.   
  11.     // Fields       
  12.      private Integer id;   
  13.      private String newTitle;   
  14.      private String newAuther;   
  15.      private String newType;   
  16.      private String newContent;   
  17.   
  18.     // Constructors   
  19.   
  20.     /** default constructor */  
  21.     public AbstractAjaxNew() {   
  22.     }   
  23.   
  24.        
  25.     /** full constructor */  
  26.     public AbstractAjaxNew(String newTitle, String newAuther, String newType, String newContent) {   
  27.         this.newTitle = newTitle;   
  28.         this.newAuther = newAuther;   
  29.         this.newType = newType;   
  30.         this.newContent = newContent;   
  31.     }   
  32.   
  33.       
  34.     // Property accessors   
  35.   
  36.     public Integer getId() {   
  37.         return this.id;   
  38.     }   
  39.        
  40.     public void setId(Integer id) {   
  41.         this.id = id;   
  42.     }   
  43.   
  44.     public String getNewTitle() {   
  45.         return this.newTitle;   
  46.     }   
  47.        
  48.     public void setNewTitle(String newTitle) {   
  49.         this.newTitle = newTitle;   
  50.     }   
  51.   
  52.     public String getNewAuther() {   
  53.         return this.newAuther;   
  54.     }   
  55.        
  56.     public void setNewAuther(String newAuther) {   
  57.         this.newAuther = newAuther;   
  58.     }   
  59.   
  60.     public String getNewType() {   
  61.         return this.newType;   
  62.     }   
  63.        
  64.     public void setNewType(String newType) {   
  65.         this.newType = newType;   
  66.     }   
  67.   
  68.     public String getNewContent() {   
  69.         return this.newContent;   
  70.     }   
  71.        
  72.     public void setNewContent(String newContent) {   
  73.         this.newContent = newContent;   
  74.     }   
  75. }   

PO类,MyEclipse 5.1.1生成:

java 代码
  1. package com.fzfx88.conf;   
  2. // Generated by MyEclipse - Hibernate Tools   
  3.   
  4. /**  
  5.  * AjaxNew generated by MyEclipse - Hibernate Tools  
  6.  * auther huguoqing  
  7.  */  
  8. public class AjaxNew extends AbstractAjaxNew implements java.io.Serializable {   
  9.   
  10.     // Constructors   
  11.   
  12.     /** default constructor */  
  13.     public AjaxNew() {   
  14.     }   
  15.   
  16.        
  17.     /** full constructor */  
  18.     public AjaxNew(String newTitle, String newAuther, String newType, String newContent) {   
  19.         super(newTitle, newAuther, newType, newContent);           
  20.     }   
  21.       
  22. }  

Hibernate  AjaxNew.hbm.xml配置文件

xml 代码
  1. <!---->xml version="1.0"?>  
  2. <!---->
  3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  4. <!---->  
  5. <hibernate-mapping>  
  6.     <class name="com.fzfx88.conf.AjaxNew" table="AJAX_NEW" schema="dbo" catalog="pepsi">  
  7.         <id name="id" type="java.lang.Integer">  
  8.             <column name="ID" />  
  9.             <generator class="identity" />  
  10.         id>  
  11.         <property name="newTitle" type="java.lang.String">  
  12.             <column name="NEW_TITLE" length="100" />  
  13.         property>  
  14.         <property name="newAuther" type="java.lang.String">  
  15.             <column name="NEW_AUTHER" length="30" />  
  16.         property>  
  17.         <property name="newType" type="java.lang.String">  
  18.             <column name="NEW_TYPE" length="30" />  
  19.         property>  
  20.         <property name="newContent" type="java.lang.String">  
  21.             <column name="NEW_CONTENT" length="500" />  
  22.         property>  
  23.     class>  
  24. hibernate-mapping>  

 step:4  构建 Action 类:

java 代码
  1. /**  
  2.  * news system  
  3.  */  
  4. package com.fzfx88.base.action;   
  5.   
  6.   
  7. import javax.servlet.http.HttpServletRequest;   
  8. import javax.servlet.http.HttpServletResponse;   
  9.   
  10. import org.apache.struts.action.ActionForm;   
  11. import org.apache.struts.action.ActionForward;   
  12. import org.apache.struts.action.ActionMapping;   
  13.   
  14. import com.fzfx88.base.form.AjaxNewsSystemForm;   
  15. import com.fzfx88.base.service.AjaxNewsSystemService;   
  16. import com.fzfx88.common.base.BaseAction;   
  17. import com.fzfx88.conf.AjaxNew;   
  18.   
  19. /**  
  20.  * @author huguoqing  
  21.  *  
  22.  */  
  23. public class AjaxNewsSystemAction extends BaseAction {   
  24.     AjaxNewsSystemService nService = new AjaxNewsSystemService();   
  25.     /**  
  26.      * 初始化  
  27.      * @param mapping  
  28.      * @param form  
  29.      * @param request  
  30.      * @param response  
  31.      * @return  
  32.      */  
  33.     public ActionForward init(ActionMapping mapping,ActionForm form,   
  34.             HttpServletRequest request,HttpServletResponse response){   
  35.         AjaxNewsSystemForm nForm = (AjaxNewsSystemForm)form;   
  36.         AjaxNew news = nForm.getNews();   
  37.         return mapping.findForward("init");   
  38.     }   
  39.     /**  
  40.      * 新建  
  41.      * @param mapping  
  42.      * @param form  
  43.      * @param request  
  44.      * @param response  
  45.      * @return  
  46.      */  
  47.     public ActionForward save(ActionMapping mapping,ActionForm form,   
  48.             HttpServletRequest request,HttpServletResponse response){   
  49.         AjaxNewsSystemForm nForm = (AjaxNewsSystemForm)form;   
  50.         AjaxNew news = nForm.getNews();   
  51.         if(nService.createNews(news)){   
  52.             return mapping.findForward("success");   
  53.         }else{   
  54.             return mapping.findForward("false");   
  55.         }   
  56.     }   
  57.        
  58. }   

step 5: 构建数据层访问类:

java 代码
  1. /**  
  2.  *   
  3.  */  
  4. package com.fzfx88.base.service;   
  5.   
  6. import java.util.ArrayList;   
  7. import java.util.List;   
  8.   
  9. import org.hibernate.Hibernate;   
  10. import org.hibernate.HibernateException;   
  11. import org.hibernate.Query;   
  12. import org.hibernate.Session;   
  13. import org.hibernate.Transaction;   
  14.   
  15. import com.fzfx88.conf.AjaxNew;   
  16. import com.fzfx88.util.HibernateUtil;   
  17.   
  18. /**  
  19.  * @author huguoqing  
  20.  *  
  21.  */  
  22. public class AjaxNewsSystemService {   
  23.     /**  
  24.      * 新建news  
  25.      * @param news  
  26.      */  
  27.     public boolean createNews(AjaxNew news){   
  28.         Session session=HibernateUtil.currentSession();   
  29.         Transaction tran=session.beginTransaction();       
  30.         try{       
  31.             session.save(news);   
  32.             tran.commit();   
  33.             session.flush();   
  34.         } catch (HibernateException e) {   
  35.             if(tran!=null){   
  36.                 tran.rollback();   
  37.             }   
  38.             e.printStackTrace();   
  39.             return false;   
  40.         }finally {   
  41.             HibernateUtil.closeSession();   
  42.         }   
  43.         return true;   
  44.     }   
  45.     /**  
  46.      * 获得新闻列表  
  47.      * @param newType  
  48.      * @return  
  49.      */  
  50.     public List queryStoreList(String newType){   
  51.         List storeList =new ArrayList();   
  52.         Session session=HibernateUtil.currentSession();   
  53.         Transaction tran=session.beginTransaction();   
  54.         String sql="from AjaxNew where newType=:newType ";   
  55.         try {   
  56.             Query query=session.createQuery(sql);   
  57.             query.setParameter("newType",newType);   
  58.             storeList=query.list();   
  59.         } catch (HibernateException e) {   
  60.             if(tran!=null){   
  61.                 tran.rollback();   
  62.             }   
  63.             e.printStackTrace();   
  64.         }finally {   
  65.             HibernateUtil.closeSession();   
  66.         }   
  67.         return storeList;   
  68.     }   
  69.     /**  
  70.      * 根据id获得AjaxNew对象  
  71.      * @param id  
  72.      * @return  
  73.      */  
  74.     public AjaxNew retrieveAjaxNew(String newId){   
  75.         Integer id = Integer.parseInt(newId);   
  76.         Session session=HibernateUtil.currentSession();   
  77.         String sql="from AjaxNew w where w.id=:id ";   
  78.         AjaxNew item=null;   
  79.         try {   
  80.             Query query=session.createQuery(sql);   
  81.             query.setParameter("id",id);   
  82.                
  83.             item=(AjaxNew)query.uniqueResult();   
  84.             if(Hibernate.isInitialized(item)){   
  85.                  Hibernate.initialize(item);   
  86.             }   
  87.         } catch (HibernateException e) {   
  88.             e.printStackTrace();   
  89.         }finally {   
  90.             HibernateUtil.closeSession();   
  91.         }   
  92.         return item;   
  93.     }   
  94. }   

step 6: 构建Struts Form .................AjaxNewsSystemForm.java

step 7 :Struts 配置文件:

xml 代码
  1. <!---->xml version="1.0" encoding="UTF-8"?>  
  2. <!---->>  
  3.   
  4. <struts-config>  
  5.   <data-sources/>  
  6.   <form-beans>  
  7.      <form-bean name="AjaxNewsSystemForm" type="com.fzfx88.base.form.AjaxNewsSystemForm" />        
  8.   form-beans>  
  9.   <global-exceptions />  
  10.   
  11.   <global-forwards>  
  12.     <forward name="success" path="/regSuccess.htm"/>  
  13.     <forward name="false" path="/false.htm"/>  
  14.   global-forwards>  
  15.   <action-mappings>  
  16.     <action name="AjaxNewsSystemForm"    
  17.               path="/news"    
  18.               type="com.fzfx88.base.action.AjaxNewsSystemAction"  
  19.               scope="request">  
  20.          <forward name="init" path="/base/news.jsp"/>  
  21.     action>  
  22.   action-mappings>  
  23.   <message-resources parameter="resource.ApplicationResources" />  
  24. struts-config>  
  25.   

 step8 : 点击进入下一页

分享到:
评论
1 楼 spiritfrog 2007-10-15  
数据库脚本不全啊,请补上吧。

相关推荐

    DWR 应用实例(一) 新闻发布系统,多种新闻类型之间切换无刷新

    在本实例中,"DWR 应用实例(一) 新闻发布系统,多种新闻类型之间切换无刷新",我们将探讨如何使用DWR构建一个新闻发布系统,这个系统可以在用户选择不同新闻类型时,动态加载对应的内容,而无需重新加载整个页面。...

    dwr实例dwr实例dwr实例

    dwr实例dwr实例dwr实例dwr实例dwr实例dwr实例dwr实例dwr实例dwr实例dwr实例dwr实例dwr实例dwr实例dwr实例dwr实例dwr实例dwr实例dwr实例dwr实例dwr实例dwr实例dwr实例dwr实例dwr实例dwr实例dwr实例dwr实例dwr实例dwr...

    dwr实现实时刷新实例

    在这个“dwr实现实时刷新实例”中,我们将深入探讨DWR如何实现无页面跳动的刷新机制,并关注线程管理和数据库更新。 首先,DWR的核心功能是提供异步通信,使得前端可以实时获取服务器端的变化,而无需刷新整个页面...

    dwr经典实例(9个常用实例)

    DWR (Direct Web Remoting) 是一个开源JavaScript库,它允许Web应用程序与服务器端Java对象进行实时交互,实现异步更新页面,无需刷新整个页面。这个压缩包文件"dwrtest"很可能包含了9个经典的DWR实例,这些实例通常...

    dwr应用实例

    Direct Web Remoting (DWR) 是一个开源的Java库,它允许JavaScript在浏览器端与服务器端的Java对象进行交互,从而实现动态Web应用程序。DWR通过AJAX技术提供了一种简单的方法来创建富客户端界面,使得用户可以在不...

    dwr、ajax 无刷新技术

    "MyDwr"可能是一个示例项目,展示了如何在实际应用中使用DWR进行无刷新开发。 综上所述,DWR和AJAX的结合为Web开发者提供了强大的工具,以实现无刷新的交互体验。通过学习和应用这些技术,开发者可以创建出更现代、...

    dwr+spring实例

    本实例"DWRSpring实例"是一个使用DWR与Spring框架结合的消息发布系统,包含了基础的CRUD(Create、Read、Update、Delete)操作。通过这个例子,我们可以深入理解DWR和Spring如何协同工作,以及它们在实际开发中的...

    dwr,dojo框架应用实例

    DWR 是一个开源JavaScript库,允许Web应用程序在客户端和服务器之间进行实时通信,即实现Ajax(异步JavaScript和XML)功能。它简化了JavaScript与Java后端服务的交互,让开发者能够调用服务器上的方法,就像它们是...

    dwr工程实例,基本于myeclipse.

    DWR(Direct Web Remoting)是一种Java库,用于在Web应用程序中实现JavaScript和服务器端Java对象之间的双向通信。它使得动态、实时的交互成为可能,极大地增强了用户体验。在这个"dwr工程实例"中,我们将探讨如何...

    dwr实例教程 很好的入门实例

    ### dwr实例教程知识点解析 #### 一、DWR简介及功能特点 DWR(Direct Web Remoting)是一种简化...无论是简单的登录验证还是复杂的菜单刷新功能,DWR都能很好地胜任,是学习和开发Web应用程序的一个非常实用的工具。

    dwr 实例 Dwr实例

    自己写的dwr实例。方便大家学习,包括jar文件,希望对大家有帮助

    Spring DWR配置实例

    Spring Direct Web Remoting (DWR) 是一个Java库,它允许Web应用程序轻松地在客户端JavaScript和服务器端Java之间进行交互。这个技术的核心功能是提供了一种机制,使得动态的、实时的Web应用成为可能,无需刷新整个...

    dwr框架实现无刷新分页

    DWR(Direct Web Remoting)是一种JavaScript和Java之间的远程调用技术,它允许Web应用在不刷新整个页面的情况下与服务器进行交互,从而实现动态更新和实时反馈的效果。在DWR中,JavaScript对象可以直接调用后台的...

    DWR 开发实例是用来学习DWR的一个实例

    DWR(Direct Web Remoting)是一种Java库,它允许JavaScript在客户端与服务器端进行直接的交互,无需Ajax的页面刷新。这个"DWR开发实例"是一个专门为学习DWR技术准备的实践项目,旨在帮助开发者理解DWR的核心功能和...

    dwr框架的应用实例

    DWR (Direct Web Remoting) 是一个开源的Java框架,用于在浏览器和服务器之间实现动态、实时的Web应用交互。这个框架允许JavaScript直接调用服务器端的Java方法,就像它们是本地函数一样,极大地简化了AJAX...

    Ajax框架:简单的dwr实例

    通过实践一个简单的DWR实例,你可以更好地理解如何在实际项目中使用它来构建高效、响应式的Web应用。随着对DWR的深入理解和应用,你会发现它不仅可以提升开发效率,还能显著提升应用的用户体验。

    ajax_dwr实现页面无刷新加载

    DWR(Direct Web Remoting)是Java后端与JavaScript前端之间进行实时通信的一种框架,它简化了Ajax应用的开发,让开发者可以像调用本地方法一样调用服务器端的方法。 **DWR的介绍** DWR允许JavaScript在客户端直接...

    dwr.demo dwr实例

    DWR (Direct Web Remoting) 是一个开源的Java库,它允许Web应用程序在客户端和服务器之间进行实时的、异步的通信,无需刷新整个页面。这个"**dwr.demo dwr实例**"很可能是用来展示如何在实际项目中集成和使用DWR的...

    ajax的dwr框架实例

    DWR(Direct Web Remoting)是一个开源的Java框架,它允许JavaScript在客户端与服务器端进行直接的、异步的数据交换,极大地增强了Web应用的用户体验。在这个"ajax的dwr框架实例"中,我们将探讨如何利用DWR与MySQL...

Global site tag (gtag.js) - Google Analytics