当前位置: 代码迷 >> Web前端 >> dwr学习札记(二)
  详细解决方案

dwr学习札记(二)

热度:29   发布时间:2012-11-10 10:48:50.0
dwr学习笔记(二)
dwr的世界真是趣味无穷,昨天晚上搞了一个DEMO版的dwr + spring整合,今天发出来大家一起研究研究,如果我的代码有什么没写好的地方,欢迎大家指正~


web.xml代码:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
 xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <servlet>
   <servlet-name>dwr-invoker</servlet-name>
   <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
   <init-param>
    <param-name>debug</param-name>
    <param-value>true</param-value>
   </init-param>
  </servlet>
  <servlet-mapping>
   <servlet-name>dwr-invoker</servlet-name>
   <url-pattern>/dwr/*</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
</web-app>


dwr.xml代码:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://www.getahead.ltd.uk/dwr/dwr20.dtd">
<dwr>
 <allow>
  <create creator="spring" javascript="TableDAO">
   <param name="beanName" value="tableDAO" />
   <include method="findItems"/>
  </create>
  <convert  converter="bean" match="com.wise.table.Item"></convert>
 </allow>
</dwr>


application.xml代码:

<?xml version="1.0" encoding="UTF-8"?>
<beans
 xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
 <bean id="dataSource"
  class="org.apache.commons.dbcp.BasicDataSource">
  <property name="driverClassName"
   value="com.mysql.jdbc.Driver">
  </property>
  <property name="url"
   value="jdbc:mysql://127.0.0.1:3306/lin?useUnicode=true&amp;characterEncoding=UTF-8">
  </property>
  <property name="username" value="root"></property>  
  <property name="password" value="lin"></property>
 </bean>
 <bean id="sessionFactory"
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="dataSource">
   <ref bean="dataSource" />
  </property>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">
     org.hibernate.dialect.MySQLDialect
    </prop>
    <prop key="hibernate.show_sql">true</prop>
   </props>
  </property>
  <property name="mappingResources">
   <list>
    <value>com/wise/table/Item.hbm.xml</value>
   </list>
  </property>
 </bean>
  <bean id="tableDAO" class="com.wise.table.TableDAO">
  <property name="sessionFactory">
   <ref local="sessionFactory" />
  </property>
 </bean> 
</beans>


Item.java代码:

package com.wise.table;

public class Item {
 private int id;
 private String name;
 private int parent;
 private String url;
 private int order;
 private int subItem;
 
 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public int getParent() {
  return parent;
 }
 public void setParent(int parent) {
  this.parent = parent;
 }
 public String getUrl() {
  return url;
 }
 public void setUrl(String url) {
  this.url = url;
 }
 public int getOrder() {
  return order;
 }
 public void setOrder(int order) {
  this.order = order;
 }
 public int getSubItem() {
  return subItem;
 }
 public void setSubItem(int subItem) {
  this.subItem = subItem;
 }
}

Item.hbm.xml(略)

TableDAO.java代码:

package com.wise.table;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class TableDAO extends HibernateDaoSupport{
 public List<Item> findItems()
 {
  List<Item> list = this.getHibernateTemplate().find("from Item");
  return list;
 }
}

index.jsp代码:

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>"/>
   
    <title>My JSP 'index.jsp' starting page</title>
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 
 <script type='text/javascript' src='dwr/util.js'></script>
 <script type='text/javascript' src='dwr/engine.js'></script>
 <script type='text/javascript' src='dwr/interface/TableDAO.js'></script>
 <script>
 function init()
 {
  TableDAO.findItems(createTable);
 }
 function createTable(data)
 {
  DWRUtil.removeAllRows("tablebody");
     DWRUtil.addRows("tablebody", data, cellFunctions);
 }
 var cellFunctions = [
    function(data){ return data.name; },
    function(data) { return data.url; },
    function(data) { return data.order; },
    function(data) {
     var btn = document.createElement("text");
     btn.innerHTML = "<a href='http://"+data.url+"'>a</a>";
     return btn;
    }
    ];
 </script>
  </head>
 
  <body onload="init();">
   <table border="1">
   <thead>
    <tr><th>Name</th><th>url</th><th>order</th><th>operation</th>
   </thead>
    <tbody id="tablebody"></tbody>
   </table>
  </body>
</html>
  相关解决方案