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

来源:互联网 发布:华为海洋网络待遇怎样 编辑:程序博客网 时间:2024/05/06 17:23
 
最近闲着用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. <!--sp-->xml version="1.0"?>  
  2. <!--CTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"   </sp-->
  3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  4. <!--  </span> </li> <li class="alt"><span><span class="comments">    Mapping file autogenerated by MyEclipse - Hibernate Tools </span> </span></li> <li class=""><span><span class="comments">-->  
  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. <!--sp-->xml version="1.0" encoding="UTF-8"?>  
  2. <!--CTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"</sp-->>  
  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.   
 
原创粉丝点击