使用DWR实现自己主动补全

自己主动补全:是指用户在文本框中输入前几个字母或汉字的时候,自己主动在存放数据的文件或数据库中将全部以这些字母或汉字开头的数据提示给用户供用户选择

在日常上网过程中,我们常常使用搜索引擎。当我们输入想要检索的keyword时。搜索引擎会提示我们相关的keyword

训练要点:

掌握使用DWR框架开发Ajax程序

使用MyEclipse 10.0 + MySql5.0

新建数据库:能够手动随便新建一个測试用的

DROP TABLE IF EXISTS `books`;
CREATE TABLE `books` (
`id` varchar(100) NOT NULL,
`isbn` varchar(25) default NULL,
`title` varchar(100) default NULL,
`price` double default NULL,
`pubdate` date default NULL,
`intro` bigint(20) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

随便插入几条数据:

INSERT INTO `books` VALUES ('1', 'BH123', 'java编程思想昂', '55', '2014-06-10', '333');
INSERT INTO `books` VALUES ('2', 'BH23', 'JS实战经典', '22', '2014-06-10', '3');
INSERT INTO `books` VALUES ('3', '1231232', 'Java核心技术卷', '66', '2014-06-13', '3');
INSERT INTO `books` VALUES ('4', '2342', 'java开发实战经典', '33', '2014-06-13', '98');

使用myeclipse 切换到database视图。右击新建一个数据连接

新建web project 项目:BookShop,使用MyEclipse加入SSH支持。

先加Spring支持。选择前四个库AOP,Core,Persistence Core,JDBC 选择spring3.0;

加入dwr的jar包到lib下;

加入hibernate支持。

下一步,选择Spring configuration file

下一步,选择Existing Spring configuration file

下一步选择刚刚配置好的JDBC Driver:mysql。

下一步,不选择 创建SessionFactory;

加入Sturts2支持:

选择struts2.1。下一步记得选择Struts2 Spring包;

在web.xml配置spring 和dwr。

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <servlet>
<servlet-name>dwr</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</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>

在WEB-INF新建dwr.xml

<?

xml version="1.0" encoding="UTF-8"?

>
<!DOCTYPE dwr PUBLIC
"-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN"
"http://getahead.org/dwr/dwr30.dtd">
<dwr>
<allow>
<create javascript="BooksBiz" creator="spring" scope="application">
<param name="beanName" value="BooksBiz"></param>
<include method="findTitle" />
</create>
<convert converter="bean" match="com.books.entity.Books" />
<create creator="new" javascript="validator">
<param name="class" value="org.apache.struts2.validators.DWRValidator"></param>
</create>
<convert converter="bean" match="com.opensymphony.xwork2.ValidationAwareSupport" />
</allow>
</dwr>

将books反向project: 切换到Database Explorer模式,选择mysql 的books表右击表选择Hibernate Reverse …

在Java package新建包com.books.eneity

说明上面三部:

创建一个hibernate实体配置文件books.hbm.xml

创建一个实体类Books

创建Spring Dao

下一步:

Id策略选择 assigned。

完毕;



将类BooksDao移到com.books.dao;

新建接口BooksBiz在com.books.biz下,加入例如以下方法:

public List<Books> findTitle(String title);

新建实现类BooksBizImpl implements BooksBiz 在com.books.biz.impl下

	private BooksDAO booksDAO;

	public List<Books> findTitle(String title) {
List<Books> list = booksDAO.findTitle(title);
return list;
} public void setBooksDAO(BooksDAO booksDAO) {
this.booksDAO = booksDAO;
}

在BooksDAO下新建 findTitle方法:

	public List<Books> findTitle(String title2) {
Query query = this.getSession().createQuery("from Books b where b.title like :title order by b.pubdate desc");
query.setParameter("title", title2 + "%");
return query.list();
}

配置 applicationContext:

头部加入tx和aop支持

	xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
">

在</beans>前加入:

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:advice transaction-manager="transactionManager" id="txAdvice">
<tx:attributes>
<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
<tx:method name="*" propagation="REQUIRED" read-only="false" />
</tx:attributes>
</tx:advice> <aop:config>
<aop:pointcut expression="execution( * com.books.biz.*.*(..))" id="pointcut" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut" />
</aop:config> <bean id="BooksBiz" class="com.books.biz.impl.BooksBizImpl">
<property name="booksDAO" ref="BooksDAO"></property>
</bean>

Index.jsp页面

在head以下加:

	<script type='text/javascript' src='dwr/engine.js'></script>
<script type='text/javascript' src='dwr/interface/BooksBiz.js'></script>
<script type='text/javascript' src='dwr/util.js'></script>
<style type="text/css">
#sel{
position: absolute;
top: 34px;
left:90px;
visibility: hidden; }
</style>
<script type="text/javascript">
function getTitle(){
var title=dwr.util.getValue('title');
if(title==''){
$('sel').style.visibility="hidden";
return ;
}
BooksBiz.findTitle(title,callback);
}
function callback(list){
if(list.length==0){
$('sel').style.visibility="hidden";
return ;
}else{
$('sel').style.visibility="visible";
}
$('sel').size=list.length;
dwr.util.removeAllOptions('sel');
dwr.util.addOptions('sel',list,"title","title");
}
function getValue(){
$('title').value=$('sel').options[$('sel').selectedIndex].innerHTML;
$('sel').style.visibility="hidden";
}
</script> <body>
图书搜索:<input type="text" id="title" onkeyup="getTitle()">
<select id="sel" ondblclick="getValue()" multiple="multiple"></select>
<input type="button" value="搜 索">
</body>
</html>

部署执行的时候,会有包冲突。

打开部署的文件夹F:\Java\apache-tomcat-6.0.37\webapps\BookShop\WEB-INF\lib,将里面的jar包拷贝出来 反复的antlr删掉最低版本号的。然后将项目停掉吧我们之前工具导入的jar库都remove。再将拷贝出来的jar们拷贝到lib下;



部署,訪问主页,输入数据库数据 的首个字就有相应数据显示;

源代码下载:

http://pan.baidu.com/s/1eQtJhiq

dwr jar下载:http://pan.baidu.com/s/1o61Auee

使用DWR实现自己主动补全 相似百度搜索框的自己主动显示效果的更多相关文章

  1. 使用DWR实现自动补全 类似百度搜索框的自动显示效果

    使用DWR实现自动补全 自动补全:是指用户在文本框中输入前几个字母或汉字的时候,自动在存放数据的文件或数据库中将所有以这些字母或汉字开头的数据提示给用户供用户选择 在日常上网过程中,我们经常使用搜索引 ...

  2. 你知道为什么Xcode6中Swift没有智能提示和自己主动补全功能吗 ?

    你知道为什么Xcode6中Swift没有智能提示和自己主动补全功能吗 ? 长沙戴维营教育将为你解开这个巨大的谜团大BUG! http://www.ubuntucollege.cn/course/29/ ...

  3. Archlinux YouCompleteMe+syntastic vim自己主动补全插件,显示缩进和状态栏美化,爽心悦目的vim

    Archlinux 安装和配置vim补全插件YouCompleteMe的过程. 參考: https://github.com/Valloric/YouCompleteMe https://github ...

  4. 辛星深入分析vim的自己主动补全功能以及vim的映射

    曾经对于vim的自己主动补全功能,都是须要的时候从网上下载点配置项,然后复制到自己的vimrc上去,自己也不知道是什么意思.结果发现搜索到的非常多自己主动补全的方式都非常另类,有的喜欢在补全大括号的时 ...

  5. linux下让irb实现代码自己主动补全的功能

    我不知道其它系统上irb是否有此功能,可是在ubuntu上ruby2.1.2自带的irb默认是没有代码自己主动补全功能的,这多少让人认为有所不便.事实上加上也非常easy,就是在irb里载入一个模块: ...

  6. jquery autocomplete文本自己主动补全

    文本自己主动补全功能确实非常有用. 先看下简单的效果:(样式不咋会写) 以下介绍几种: 1:jqery-actocomplete.js 这个网上有个写好的实例,上面挺具体的,能够下来执行下就清楚了就不 ...

  7. Linux下新手怎样将VIM配置成C++编程环境(能够STL自己主动补全)

    ~ 弄拉老半天,最终弄的几乎相同啦,果然程序猿还是须要有点折腾精神啊. 首先你要安装vim,命令:sudo apt-get install vim vim它仅仅是一个编辑器,它不是IDE(比方code ...

  8. 更改Scala的代码自己主动补全快捷键code completion

    使用Eclipse的代码补全快捷键alt+/已经习惯了.可是IntelJIDEA中这个快捷键组合没有效果,那么我们来改动之,按ctrl+alt+s打开设置界面 之后在keymaps中选择Eclipse ...

  9. [Android] AutoCompleteTextView:自己主动完毕输入内容的控件(自己主动补全)

    AutoCompleteTextView是EditText的直接子类,与普通EditText的最大不同就是.在用户输入的过程中,能够列出可供选择的输入项.方便使用者. AutoCompleteText ...

随机推荐

  1. 17,时间模块 time,random模块

    表示时间的三种方式 在python中,通常有着三种方式来表示时间:时间戳,元祖,格式化的时间字符串: 1,时间戳(timestamp):通常来说时间戳表示的是从1970年1月1日00:00:00开始按 ...

  2. luogu3755 [CQOI2017]老C的任务

    扫描线水题. #include <algorithm> #include <iostream> #include <cstdio> using namespace ...

  3. 九度oj 题目1139:最大子矩阵

    题目描述: 已知矩阵的大小定义为矩阵中所有元素的和.给定一个矩阵,你的任务是找到最大的非空(大小至少是1 * 1)子矩阵. 比如,如下4 * 4的矩阵 0 -2 -7 0 9 2 -6 2 -4 1 ...

  4. Cache技术――OSCache(转-全)

    OSCache使用指南 一.下载安装 OSCache是一个基于web应用的组件,他的安装工作主要是对web应用进行配置,大概的步骤如下: 1. 下载.解压缩OSCache 从http://www.op ...

  5. 【shell】shell编程(四)-循环语句

    上篇我们学习了shell中条件选择语句的用法.接下来本篇就来学习循环语句.在shell中,循环是通过for, while, until命令来实现的.下面就分别来看看吧. for for循环有两种形式: ...

  6. Redis集群模式配置

    redis集群部署安装: https://blog.csdn.net/huwh_/article/details/79242625 https://www.cnblogs.com/mafly/p/re ...

  7. 转 STL之vector的使用

    http://www.cnblogs.com/caoshenghe/archive/2010/01/31/1660399.html 第一部分 使用入门 vector可用于代替C中的数组,或者MFC中的 ...

  8. sgu 102模拟欧拉函数

    感觉自己弱爆了,做做SGU吧... #include<iostream> #include<cmath> //欧拉函数 using namespace std; int eul ...

  9. SPOJ 1479 +SPOJ 666 无向树最小点覆盖 ,第二题要方案数,树形dp

    题意:求一颗无向树的最小点覆盖. 本来一看是最小点覆盖,直接一下敲了二分图求最小割,TLE. 树形DP,叫的这么玄乎,本来是线性DP是线上往前\后推,而树形DP就是在树上,由叶子结点状态向根状态推. ...

  10. HDu1241 DFS搜索

    #include<iostream> #include<cstring> using namespace std; int a[105][105]; int d[8][2]={ ...