step1: 下载DWR包
step2: 构建数据库
step2: 构建数据库
sql 代码
- if exists (select 1
- from sysobjects
- where id = object_id('AJAX_NEW')
- and type = 'U')
- drop table AJAX_NEW
- go
- /*==============================================================*/
- /* Table: AJAX_NEW */
- /*==============================================================*/
- create table AJAX_NEW (
- ID integer not null,
- NEW_TITLE VARCHAR(100) null,
- NEW_AUTHER VARCHAR(30) null,
- NEW_TYPE VARCHAR(30) null,
- NEW_CONTENT VARCHAR(500) null,
- constraint PK_AJAX_NEW primary key (ID)
- )
- go
step3 搭建Struts,Hibernate框架,生成Hibernae配置文件以及po类:
java 代码
- package com.fzfx88.conf;
- /**
- * AbstractAjaxNew generated by MyEclipse - Hibernate Tools
- * po 抽象类
- * auther huguoqing
- */
- public abstract class AbstractAjaxNew implements java.io.Serializable {
- // Fields
- private Integer id;
- private String newTitle;
- private String newAuther;
- private String newType;
- private String newContent;
- // Constructors
- /** default constructor */
- public AbstractAjaxNew() {
- }
- /** full constructor */
- public AbstractAjaxNew(String newTitle, String newAuther, String newType, String newContent) {
- this.newTitle = newTitle;
- this.newAuther = newAuther;
- this.newType = newType;
- this.newContent = newContent;
- }
- // Property accessors
- public Integer getId() {
- return this.id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getNewTitle() {
- return this.newTitle;
- }
- public void setNewTitle(String newTitle) {
- this.newTitle = newTitle;
- }
- public String getNewAuther() {
- return this.newAuther;
- }
- public void setNewAuther(String newAuther) {
- this.newAuther = newAuther;
- }
- public String getNewType() {
- return this.newType;
- }
- public void setNewType(String newType) {
- this.newType = newType;
- }
- public String getNewContent() {
- return this.newContent;
- }
- public void setNewContent(String newContent) {
- this.newContent = newContent;
- }
- }
PO类,MyEclipse 5.1.1生成:
java 代码
- package com.fzfx88.conf;
- // Generated by MyEclipse - Hibernate Tools
- /**
- * AjaxNew generated by MyEclipse - Hibernate Tools
- * auther huguoqing
- */
- public class AjaxNew extends AbstractAjaxNew implements java.io.Serializable {
- // Constructors
- /** default constructor */
- public AjaxNew() {
- }
- /** full constructor */
- public AjaxNew(String newTitle, String newAuther, String newType, String newContent) {
- super(newTitle, newAuther, newType, newContent);
- }
- }
Hibernate AjaxNew.hbm.xml配置文件
xml 代码
- <!--sp-->xml version="1.0"?>
- <!--CTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" </sp-->
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <!-- </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">-->
- <hibernate-mapping>
- <class name="com.fzfx88.conf.AjaxNew" table="AJAX_NEW" schema="dbo" catalog="pepsi">
- <id name="id" type="java.lang.Integer">
- <column name="ID" />
- <generator class="identity" />
- id>
- <property name="newTitle" type="java.lang.String">
- <column name="NEW_TITLE" length="100" />
- property>
- <property name="newAuther" type="java.lang.String">
- <column name="NEW_AUTHER" length="30" />
- property>
- <property name="newType" type="java.lang.String">
- <column name="NEW_TYPE" length="30" />
- property>
- <property name="newContent" type="java.lang.String">
- <column name="NEW_CONTENT" length="500" />
- property>
- class>
- hibernate-mapping>
step:4 构建 Action 类:
java 代码
- /**
- * news system
- */
- package com.fzfx88.base.action;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.struts.action.ActionForm;
- import org.apache.struts.action.ActionForward;
- import org.apache.struts.action.ActionMapping;
- import com.fzfx88.base.form.AjaxNewsSystemForm;
- import com.fzfx88.base.service.AjaxNewsSystemService;
- import com.fzfx88.common.base.BaseAction;
- import com.fzfx88.conf.AjaxNew;
- /**
- * @author huguoqing
- *
- */
- public class AjaxNewsSystemAction extends BaseAction {
- AjaxNewsSystemService nService = new AjaxNewsSystemService();
- /**
- * 初始化
- * @param mapping
- * @param form
- * @param request
- * @param response
- * @return
- */
- public ActionForward init(ActionMapping mapping,ActionForm form,
- HttpServletRequest request,HttpServletResponse response){
- AjaxNewsSystemForm nForm = (AjaxNewsSystemForm)form;
- AjaxNew news = nForm.getNews();
- return mapping.findForward("init");
- }
- /**
- * 新建
- * @param mapping
- * @param form
- * @param request
- * @param response
- * @return
- */
- public ActionForward save(ActionMapping mapping,ActionForm form,
- HttpServletRequest request,HttpServletResponse response){
- AjaxNewsSystemForm nForm = (AjaxNewsSystemForm)form;
- AjaxNew news = nForm.getNews();
- if(nService.createNews(news)){
- return mapping.findForward("success");
- }else{
- return mapping.findForward("false");
- }
- }
- }
step 5: 构建数据层访问类:
java 代码
- /**
- *
- */
- package com.fzfx88.base.service;
- import java.util.ArrayList;
- import java.util.List;
- import org.hibernate.Hibernate;
- import org.hibernate.HibernateException;
- import org.hibernate.Query;
- import org.hibernate.Session;
- import org.hibernate.Transaction;
- import com.fzfx88.conf.AjaxNew;
- import com.fzfx88.util.HibernateUtil;
- /**
- * @author huguoqing
- *
- */
- public class AjaxNewsSystemService {
- /**
- * 新建news
- * @param news
- */
- public boolean createNews(AjaxNew news){
- Session session=HibernateUtil.currentSession();
- Transaction tran=session.beginTransaction();
- try{
- session.save(news);
- tran.commit();
- session.flush();
- } catch (HibernateException e) {
- if(tran!=null){
- tran.rollback();
- }
- e.printStackTrace();
- return false;
- }finally {
- HibernateUtil.closeSession();
- }
- return true;
- }
- /**
- * 获得新闻列表
- * @param newType
- * @return
- */
- public List queryStoreList(String newType){
- List storeList =new ArrayList();
- Session session=HibernateUtil.currentSession();
- Transaction tran=session.beginTransaction();
- String sql="from AjaxNew where newType=:newType ";
- try {
- Query query=session.createQuery(sql);
- query.setParameter("newType",newType);
- storeList=query.list();
- } catch (HibernateException e) {
- if(tran!=null){
- tran.rollback();
- }
- e.printStackTrace();
- }finally {
- HibernateUtil.closeSession();
- }
- return storeList;
- }
- /**
- * 根据id获得AjaxNew对象
- * @param id
- * @return
- */
- public AjaxNew retrieveAjaxNew(String newId){
- Integer id = Integer.parseInt(newId);
- Session session=HibernateUtil.currentSession();
- String sql="from AjaxNew w where w.id=:id ";
- AjaxNew item=null;
- try {
- Query query=session.createQuery(sql);
- query.setParameter("id",id);
- item=(AjaxNew)query.uniqueResult();
- if(Hibernate.isInitialized(item)){
- Hibernate.initialize(item);
- }
- } catch (HibernateException e) {
- e.printStackTrace();
- }finally {
- HibernateUtil.closeSession();
- }
- return item;
- }
- }
step 6: 构建Struts Form .................AjaxNewsSystemForm.java
step 7 :Struts 配置文件:
xml 代码
- <!--sp-->xml version="1.0" encoding="UTF-8"?>
- <!--CTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"</sp-->>
- <struts-config>
- <data-sources/>
- <form-beans>
- <form-bean name="AjaxNewsSystemForm" type="com.fzfx88.base.form.AjaxNewsSystemForm" />
- form-beans>
- <global-exceptions />
- <global-forwards>
- <forward name="success" path="/regSuccess.htm"/>
- <forward name="false" path="/false.htm"/>
- global-forwards>
- <action-mappings>
- <action name="AjaxNewsSystemForm"
- path="/news"
- type="com.fzfx88.base.action.AjaxNewsSystemAction"
- scope="request">
- <forward name="init" path="/base/news.jsp"/>
- action>
- action-mappings>
- <message-resources parameter="resource.ApplicationResources" />
- struts-config>
-
step:8 配置 dwr
(一)、配置 web.xml 文件,添加 DwrServlet
xml 代码- <!--sp-->xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns="http://java.sun.com/xml/ns/j2ee" >
- <servlet>
- <servlet-name>dwr-invokerservlet-name>
- <servlet-class>
- org.directwebremoting.servlet.DwrServlet
- servlet-class>
- <init-param>
- <param-name>debugparam-name>
- <param-value>trueparam-value>
- init-param>
- <init-param>
- <param-name>classes param-name>
- <param-value>java.lang.Objectparam-value>
- init-param>
- <load-on-startup>100load-on-startup>
- servlet>
- <servlet-mapping>
- <servlet-name>dwr-invokerservlet-name>
- <url-pattern>/dwr/*url-pattern>
- servlet-mapping>
- <session-config>
- <session-timeout>30session-timeout>
- session-config>
- <welcome-file-list>
- <welcome-file>index.jspwelcome-file>
- welcome-file-list>
- web-app>
(二)、配置 dwr.xml,添加需要能够远程调用的类以及公开客户端调用的类方法:
xml 代码- <!--sp-->xml version="1.0" encoding="UTF-8"?>
- <dwr>
- <allow>
- <create creator="new" javascript="news" scope="application">
- <param name="class" value="com.fzfx88.base.service.AjaxNewsSystemService"/>
- <include method="queryStoreList"/>
- <include method="retrieveAjaxNew"/>
- create>
- <convert converter="bean" match="com.fzfx88.conf.AjaxNew">
- <param name="include" value="id,newTitle, newAuther, newType, newContent"/>
- convert>
- allow>
- dwr>
step9:构建页面css 代码- [html]
- [head]
- [meta http-equiv="Content-Type" content="text/html; charset=gbk" /]
- [link href="reg/style.css" type="text/css" rel="stylesheet"]
- <!--js是dwr自动生成,命名要和dwr.xml中<span class="attribut-->javascript=news相符->
- [script type='text/javascript' src='../dwr/interface/news.js']
- <!--wr 中的js需引-->
- [script type='text/javascript' src='../dwr/engine.js']
- <!--wr 中的js需引-->
- [script type='text/javascript' src='../dwr/util.js'/]
- <!--定义js文-->
- [script type='text/javascript' src='../jsfloder/ajaxNews.js'/]
- [title]news[/title]
- [/head]
- [body]
- [div id="tabs5"]
- [ul][li id="current"][a href="javaScript:queryNewsList('1');"][span]国内[/span][/a][/li]
- [li][a href="javaScript:queryNewsList('2');"][span]国际[/span][/a][/li]
- [li][a href="javaScript:queryNewsList('3');"][span]时事政治[/span][/a][/li]
- [li][a href="javaScript:queryNewsList('4');"][span]影视娱乐[/span][/a][/li]
- [li][a href="javaScript:queryNewsList('5');"][span]读书频道[/span][/a][/li]
- [li][a href="javaScript:queryNewsList('6');"][span]美容护肤[/span][/a][/li]
- [li][a href="javaScript:queryNewsList('7');"][span]生活琐事[/span][/a][/li]
- [/ul]
- [/div]
- [TABLE class="toptable grid" width="100%" cellSpacing="0" cellPadding="5" align="center" border="1"]
- [TBODY id="knew"]
- [/TBODY]
- [/TABLE]
- [div id="snow" style="width:500px;border:1px solid darkcyan;padding:5px;display:none;"][/div]
- [/body]
- [/html]
step10: 构建 js文件
js 代码- function queryNewsList(id){
- var loadinfo = "正在载入中,请稍等...........";
- var newType = id;
- try{
- //news为dwr.xml文件中定义java类的别名,调用该类的queryStoreList方法,
- //获取新闻信息
- news.queryStoreList(newType,queryNewList);
- DWRUtil.useLoadingMessage(loadinfo);
- }catch(e){
- }
- }
- //将查询结果返回的list,添加到页面表格中
- function queryNewList(data){
- document.getElementById( "snow" ).style.display = "none";
- var cellfuncs = [
- function(data){
- var newId = data.id;
- var ahive = document.createElement(");
- ahive.setAttribute("alink","fuchsia");
- var oTextNode = document.createTextNode(data.newTitle);
- ahive.appendChild(oTextNode);
- return ahive;
- },
- function(data){
- var newId = data.id;
- var ahive = document.createElement(");
- ahive.setAttribute("alink","#FFFFFF");
- var delButton = document.createElement("img");
- delButton.setAttribute("src","../images/ico_down.gif");
- delButton.setAttribute("border","1");
- ahive.appendChild(delButton);
- return ahive;
- }
- ];
- DWRUtil.removeAllRows('knew');
- DWRUtil.addRows('knew', data,cellfuncs,{
- rowCreator:function(options) {
- var row = document.createElement("tr");
- row.setAttribute("id",options.rowData.id);
- row.setAttribute("height","20");
- return row;
- },
- cellCreator:function(options) {
- var td = document.createElement("td");
- td.setAttribute("align","left");
- return td;
- }
- });
- }
- //根据id获得一条新闻的具体信息
- function showContent(id){
- var news_id = id;
- var loadinfo = "正在载入中,请稍等...........";
- try{
- news.retrieveAjaxNew(news_id,showAnew);
- DWRUtil.useLoadingMessage(loadinfo);
- }catch(e){
- }
- }
- //显示信息内容
- function showAnew(data){
- var pp = document.getElementById("snow");
- document.getElementById( "snow" ).style.display = "";
- if (pp.hasChildNodes()) {
- for (i=0; i
- var currentNode = pp.childNodes[i];
- pp.removeChild(currentNode);
- }
- }
- var titleText = document.createTextNode(data.newTitle);
- var context = document.createTextNode(data.newContent);
- var tspan = document.createElement("p");
- tspan.setAttribute("style","font-weight:normal;");
- tspan.appendChild(titleText);
- var cspan = document.createElement("p");
- cspan.appendChild(context);
- var vbr = document.createElement("br");
- pp.appendChild(tspan);
- pp.appendChild(vbr);;
- pp.appendChild(cspan);
- }