dwr学习

[color=darkred]<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
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>
  <script type="text/javascript" src="/dwr01/dwr/interface/adminDAO.js"></script>
  <script type="text/javascript" src="/dwr01/dwr/util.js"></script>
  <script type="text/javascript" src="/dwr01/dwr/engine.js"></script>
  <script type="text/javascript">
    function findAdmin()
    {
      adminDAO.findOneAdmin(showMessage);
      function showMessage(data)
      {
       var msg="编号:"+data.adminId+"\n姓名:"+data.adminName;
       alert(msg);  
      }
    }
   
    /////////这里是利用字符串的形式做成的
    function findList()
    {
     adminDAO.findAllAdmin(showMessage);
     function showMessage(data)
     {
      var list = new Array();
      list=data;
      table="<table>";
         table+="<tr>";
               table+="<th>编号</th>";
               table+="<th>姓名</th>";
               table+="<th>密码</th>";
         table+="</tr>";
         for(var i=0;i<list.length;i++)
         {
         table+="<tr>";
               table+="<td>"+list[i].adminId+"</td>";
               table+="<td>"+list[i].adminName+"</td>";
               table+="<td>"+list[i].adminPwd+"</td>";
         table+="</tr>";
         }
         ////alert(document.getElementById("show"));
         document.getElementById("show").innerHTML=table;
     }
  }
  /////这里是利用javascript的对表格的操作做成的
  function findList1()
  {
    adminDAO.findAllAdmin(showMessage);
    function showMessage(data)
    {
      var list = new Array();
      list=data;
      var mytable= document.createElement("table");
      for(var i=0;i<list.length;i++)
        {  
          var tr=mytable.insertRow(i);
           var td=tr.insertCell();
           td.innerText=list[i].adminId;
           var td1=tr.insertCell();
           td1.innerText=list[i].adminName;
           var td2=tr.insertCell();
           td2.innerText=list[i].adminPwd;
           var td3=tr.insertCell();
           td3.innerText=list[i].adminPost;
        }
        $("show").appendChild(mytable);
    }
  }
  function findAllAdmin1()
  {
   adminDAO.findAll(selectAllAdmin);
   function selectAllAdmin(data)
   {
     alert(data);
     DWRUtil.addOptions(sel,data);
   }
  }
 
  </script>
  </head>
 
  <body>
      <input type="button" value="查看对Admin对象的操作" onclick="findAdmin()"/>
      <input type="button" value="查看对AdminList对象的操作" onclick="findList1()"/>
      <input type="button" value="查看对select对象的操作" onclick="findAllAdmin1()"/>
        <div id="show">
   </div>
   <select id="sel">
   <option>---请选择---</option>
   </select>
  </body>
</html>
----------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<dwr>
   <allow>
      <create creator="new" javascript="adminDAO">
        <param name="class" value="org.hua.dao.AdminDAO" />
      </create>
      <convert converter="bean" match="org.hua.bean.Admin"></convert>
   </allow>
</dwr>
----------------------------------------------------------------------
package org.hua.dao;

import java.util.ArrayList;
import java.util.List;

import org.hua.bean.Admin;

public class AdminDAO {

//查找一个Admin
public Admin findOneAdmin()
{
Admin admin = new Admin();
admin.setAdminId(001);
admin.setAdminName("化永生");
admin.setAdminPwd("123456");
admin.setAdminSex("male");
admin.setAdminPost("本科");
return admin;
}
//查找所有的管理员
@SuppressWarnings("unchecked")
public List findAllAdmin()
{
List list = new ArrayList();
for(int i=0;i<5;i++)
{
Admin admin = new Admin();
admin.setAdminId(001);
admin.setAdminName("化永生"+i);
admin.setAdminPwd("123456");
admin.setAdminSex("male");
admin.setAdminPost("本科");
list.add(admin);
}
return list;
}
//这里返回的是一个数组
public String[] findAll()
{
String[] admin={"化永生","永生化","生永化","永化生"};
return admin;
}
}
[/color]

你可能感兴趣的:(JavaScript,DAO,xml,bean,DWR)