DWR查询与填充Table

来源:互联网 发布:linux dracut 编辑:程序博客网 时间:2024/05/29 06:57

DWR查询与填充Table

首先去dwr官网下载一个dwr包.版本不一样.Web.xml中的配置也不一样(如下)此处为version: 2.0.M3

         <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>

下面JS回调方法(bean与联接数据库代码不在贴出.pubs库的employee表)

package com.guo.project.ajaxtm;

import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.*;

import com.guo.project.ajaxtm.Conn.GetConn;import com.guo.project.ajaxtm.po.Employee;

public class FillUserInfo {  public Collection getInfo(String se) {  System.out.println(se.equals("")+"se"+se.equals(null));  Collection list = new Vector();  String sql="";  if(!se.equals("")){  sql="select * from employee where minit=?";}  else{  sql="select * from employee";}  PreparedStatement ps=null;  ResultSet rs=null;     try {   ps=GetConn.getconn().prepareStatement(sql);   if(!se.equals("")){   ps.setString(1, se);   }   rs=ps.executeQuery();   Employee emp=null;   while(rs.next())   {    emp=new Employee();    emp.setEmp_id(rs.getString("emp_id"));    emp.setFname(rs.getString("fname"));    emp.setHire_date(rs.getString("hire_date"));    emp.setJob_id(rs.getString("job_id"));    emp.setLname(rs.getString("lname"));    emp.setMinit(rs.getString("minit"));    emp.setPub_id(rs.getString("pub_id"));    list.add(emp);   }     } catch (SQLException e) {   // TODO Auto-generated catch block   e.printStackTrace();  } catch (ClassNotFoundException e) {   // TODO Auto-generated catch block   e.printStackTrace();  }  System.out.println(list.size());  return list; }} 接下来是dwr.xml配置如下<dwr> <allow>  <create creator="new" javascript="FillUserInfo" scope="page">   <param name="class"    value="com.guo.project.ajaxtm.FillUserInfo" />    <include method="getInfo"></include>  </create>  <convert converter="bean"   match="com.guo.project.ajaxtm.po.Employee">  </convert> </allow></dwr>页面如下

<%@ page language="java" pageEncoding="GBK"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head>  <title></title>  <!-- <link rel="stylesheet" type="text/css" href="styles.css"> -->  <script type="text/javascript" src="dwr/interface/FillUserInfo.js"></script>  <script type="text/javascript" src="dwr/engine.js"></script>  <script type="text/javascript" src="dwr/util.js"></script>  <script type="text/javascript">    function getmessage()    {    var se=this.frm.txt.value;    this.frm.txt.value="";    FillUserInfo.getInfo(getdata,se);    }    function getdata(data)    {    ///DWRUril.setValue("info",dataUserName);    var cellFuncs=[    function(data){return data.emp_id;},    function(data){return data.fname;},    function(data){return data.minit;},    function(data){return data.lname;},    function(data){return data.job_id;},    function(data){return data.pub_id},    function(data){return data.hire_date}///      function(data) { return "<input type='button' value='Test' onclick='alert(/"Hi/");'/>";}    ];    DWRUtil.removeAllRows("demo1");///删除数据    DWRUtil.addRows("demo1",data,cellFuncs);///添加数据到TABLE    }         </script> </head> <body>  <table>  <tr>  <td>  <form name="frm" method="post">  <table>  <tr>  <td>标志查询:</td><td><input type="text" name="txt"/></td><td><input type="button" value="提交查询内容" onclick="getmessage();"/></td>  </tr>  </table>  </form>  </td>  </tr>   <tr>    <td>     <table width="545" height="32" border="1">      <thead>       <tr>        <th>         员工号        </th>        <th>         姓        </th>        <th>         标志        </th>        <th>         名        </th>        <th>         工号        </th>        <th>         出版号        </th>        <th>         日期        </th>       </tr>      </thead>      <tbody id="demo1">      </tbody>     </table>    </td>   </tr>  </table> </body></html>

 

原创粉丝点击