在Eclipse中开发基于DWR的AJAX的Web应用Struts5.docx
- 文档编号:24275397
- 上传时间:2023-05-25
- 格式:DOCX
- 页数:23
- 大小:258.54KB
在Eclipse中开发基于DWR的AJAX的Web应用Struts5.docx
《在Eclipse中开发基于DWR的AJAX的Web应用Struts5.docx》由会员分享,可在线阅读,更多相关《在Eclipse中开发基于DWR的AJAX的Web应用Struts5.docx(23页珍藏版)》请在冰豆网上搜索。
在Eclipse中开发基于DWR的AJAX的Web应用Struts5
实现实时补全
1、提供用户信息补全的业务层组件
(1)接口InfoCompleteInterface,包名称为com.px1987.dwrexampl.model
设计该接口
packagecom.px1987.dwrexampl.model;
importjava.util.List;
publicinterfaceInfoCompleteInterface
{
publicListgetUserNameInfo(StringuserNamePart);
}
(2)实现类InfoCompleteImpl,包名称为com.px1987.dwrexampl.model
将产生出下面的状态
(3)编程该实现类
packagecom.px1987.dwrexampl.model;
importjava.util.*;
importcom.px1987.dwrexampl.dao.*;
publicclassInfoCompleteImplimplementsInfoCompleteInterface
{
publicInfoCompleteImpl()
{
//TODO自动生成构造函数存根
}
publicListgetUserNameInfo(StringuserNamePart)
{
StringselectSQL="selectuserNamefromUserInfowhereuserNamelike'%"+userNamePart+"%'";
OperateDBInterfaceoperateDBBeanID=newOperateDBBean();
java.sql.ResultSetresultSet=operateDBBeanID.SelectDBData(selectSQL);
ListallUserNameInfo=newArrayList();
try
{
while(resultSet.next())
{
allUserNameInfo.add(resultSet.getString("userName"));
}
}
catch(java.sql.SQLExceptione)
{
e.printStackTrace();
System.out.println("异常内容为:
"+e.getMessage());
}
finally
{
operateDBBeanID.closeDBCon();
}
returnallUserNameInfo;
}
}
2、将该业务组件类发布为远程组件-------在dwr-timeBean.xml中添加下面的信息
xmlversion="1.0"encoding="UTF-8"?
>
DOCTYPEdwrPUBLIC"-//GetAheadLimited//DTDDirectWebRemoting1.0//EN""http:
//www.getahead.ltd.uk/dwr/dwr10.dtd">
[CDATA[
importjava.util.List;
importjava.lang.String;
importcom.px1987.dwrexampl.model.UserInfoManageBean;
importcom.px1987.dwrexampl.model.UserInfoVO;
List
List
]]>
3、在线查看该远程组件
(1)启动服务器并输入http:
//localhost:
8080/DWRWebTest/dwr/index.html
(2)点击上面的infoCompleteBean链接,将出现下面的内容
所要包含的*.js文件为
(3)对getUserNameInfo方法进行在线测试
注意:
保证数据库服务正确启动,并且按照UserInfo数据库表中的实际数据来输入!
4、添加一个infoComplete.jsp页面
(1)在userManager目录中新建infoComplete.jsp页面
(2)设计其内容
<%@pagelanguage="java"pageEncoding="gb2312"%>
<%@tagliburi="http:
//struts.apache.org/tags-bean"prefix="bean"%>
<%@tagliburi="http:
//struts.apache.org/tags-html"prefix="html"%>
<%@tagliburi="http:
//struts.apache.org/tags-logic"prefix="logic"%>
<%@tagliburi="http:
//struts.apache.org/tags-tiles"prefix="tiles"%>
<%
StringwebContext=request.getContextPath();
%>
DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
htmllang="true">
base/>
--
-->
formaction="/infoComplete.do"method="post"> textproperty="userName"onkeyup="showUserNameInfo(this.value)"/>
none"bordercolorlight="#4DA6FF"bordercolordark="#ECF5FF"bgcolor="#F0F8FF"> selectproperty="allUserNameInfo"onchange="userName.value=options[selectedIndex].value;"> select>
form>
html>
5、在webapp.js中添加showUserNameInfo函数
functionshowUserNameInfo(inputedUserName)
{
if(inputedUserName==""||inputedUserName.length==0)
{
$("dataTable").style.display="none";//将表格行隐藏
return;//当没有输入时不发出请求
}
infoCompleteBean.getUserNameInfo(inputedUserName,showAllUserNameCallBackFun);
}
functionshowAllUserNameCallBackFun(returnSomeUserNameInfoList)//代表返回值的数组
{
if(returnSomeUserNameInfoList.length==0)//有匹配的数据吗---表示没有匹配的数据
{
$("dataTable").style.display="none";//将表格行隐藏
return;
}
varuserNameString="";
$("dataTable").style.display="";
DWRUtil.removeAllOptions("allUserNameInfo");
DWRUtil.addOptions('allUserNameInfo',returnSomeUserNameInfoList);
}
6、添加ActionForm
com.px1987.dwrexampl.actionform.InfoCompleteActionForm、infoCompleteActionForm
privateStringuserName;
privateStringallUserNameInfo;
/*
*GeneratedbyMyEclipseStruts
*Templatepath:
templates/java/JavaClass.vtl
*/
packagecom.px1987.dwrexampl.actionform;
importjavax.servlet.http.HttpServletRequest;
importorg.apache.struts.action.ActionErrors;
importorg.apache.struts.action.ActionForm;
importorg.apache.struts.action.ActionMapping;
/**
*MyEclipseStruts
*Creationdate:
09-28-2007
*
*XDocletdefinition:
*@struts.formname="infoCompleteActionForm"
*/
publicclassInfoCompleteActionFormextendsActionForm{
/*
*Generatedfields
*/
/**userNameproperty*/
privateStringuserName;
privateStringallUserNameInfo;
/*
*GeneratedMethods
*/
/**
*Methodvalidate
*@parammapping
*@paramrequest
*@returnActionErrors
*/
publicActionErrorsvalidate(ActionMappingmapping,
HttpServletRequestrequest){
//TODOAuto-generatedmethodstub
returnnull;
}
/**
*Methodreset
*@parammapping
*@paramrequest
*/
publicvoidreset(ActionMappingmapping,HttpServletRequestrequest){
//TODOAuto-generatedmethodstub
}
/**
*ReturnstheuserName.
*@returnString
*/
publicStringgetUserName(){
returnuserName;
}
/**
*SettheuserName.
*@paramuserNameTheuserNametoset
*/
publicvoidsetUserName(StringuserName){
this.userName=userName;
}
publicStringgetAllUserNameInfo(){
returnallUserNameInfo;
}
publicvoidsetAllUserNameInfo(StringallUserNameInfo){
this.allUserNameInfo=allUserNameInfo;
}
}
7、添加Action
com.px1987.dwrexampl.action.InfoCompleteAction、/infoComplete
编程Action类
packagecom.px1987.dwrexampl.action;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importorg.apache.struts.action.Action;
importorg.apache.struts.action.ActionForm;
importorg.apache.struts.action.ActionForward;
importorg.apache.struts.action.ActionMapping;
importcom.px1987.dwrexampl.actionform.InfoCompleteActionForm;
publicclassInfoCompleteActionextendsAction
{
publicActionForwardexecute(ActionMappingmapping,ActionFormform,
HttpServletRequestrequest,HttpServletResponseresponse){
InfoCompleteActionForminfoCompleteActionForm=(InfoCompleteActionForm)form;//TODOAuto-generatedmethodstub
returnmapping.findForward("showInfo");
}
}
8、添加返回的页面showInfo.jsp
<%@pagelanguage="java"pageEncoding="gb2312"%>
<%@tagliburi="http:
//struts.apache.org/tags-bean"prefix="bean"%>
<%@tagliburi="http:
//struts.apache.org/tags-html"prefix="html"%>
<%@tagliburi="http:
//struts.apache.org/tags-logic"prefix="logic"%>
<%@tagliburi="http:
//struts.apache.org/tags-tiles"prefix="tiles"%>
DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
htmllang="true">
base/>
--
-->
您所查询的结果如下:
html>
9、添加ForWard
输入showInfo
10、再部署并执行本应用(http:
//localhost:
8080/DWRWebTest/userManager/infoComplete.jsp)
并且在控制台中能够看到下面的执行结果-----多次向后台的业务方法进行请求调用。
选择其中的某个名称项目,将自动实现输入
对于中文,也同样能够自动补全
将实时补全与用户注册关联
1、修改userRegister.jsp页面中的内容
<%@pagelanguage="java"pageEncoding="GB18030"%>
<%@tagliburi="http:
//struts.apache.org/tags-bean"prefix="bean"%>
<%@tagliburi="http:
//struts.apache.org/tags-html"prefix="html"%>
<%@tagliburi="http:
//strut
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Eclipse 开发 基于 DWR AJAX Web 应用 Struts5
![提示](https://static.bdocx.com/images/bang_tan.gif)