Ajax-DWR框架使用简介

梦秋雨 2012-06-04

Ajax-DWR框架使用简介

一般步骤:

1。将dwr.xml配置文件放在Web应用的WEB-INF目录下;

<dwr>

<allow>

<createcreator="spring"scope="page"javascript="imageInfoService">

<paramname="beanName"value="imageInfoService"/>

<includemethod="avalidateAddBizImage"/>

<includemethod="isWaterMarkPolicy"/>

</create>

</allow>

</dwr>

2.JSP文件的JS代码中调用DWR:

<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<%@pagepageEncoding="UTF-8"contentType="text/html;charset=UTF-8"%>

<%@includefile="/commons/taglibs.jsp"%>

<htmlxmlns="http://www.w3.org/1999/xhtml">

<head>

<metahttp-equiv="Content-Type"content="text/html;charset=UTF-8"/>

<metahttp-equiv="pragma"content="no-cache"/>

<metahttp-equiv="Cache-Control"content="no-cache,must-revalidate"/>

<metahttp-equiv="expires"content="Wed,26Feb199708:21:57GMT"/>

<linkhref="${ctx}/styles/imageRepository/common.css"type="text/css"rel="stylesheet"/>

<scripttype="text/javascript"src="${ctx}/dwr/util.js"></script>

<scripttype="text/javascript"src="${ctx}/dwr/engine.js"></script>

<scripttype="text/javascript"src="${ctx}/dwr/interface/imageInfoService.js"></script>

<script>

<c:iftest="${categoryIsNotExist==true}">

alert("该图片库分类已经不存在!");

</c:if>

functionbatchAddWaterMark(){

varchkbxNum=document.getElementsByName('categoryId');

varisChecked=false;

for(vari=0;i<chkbxNum.length;i++){

if(chkbxNum[i].checked==true){

isChecked=true;

break;

}

}

if(!isChecked){

alert("请选择要添加水印的图片库分类!");

returnfalse;

}

<!--JavaScript代码调用AJAX框架DWR-->

imageInfoService.isWaterMarkPolicy("1",function(result){

if(result=='isWaterMarkPolicy'){

if(confirm("您确定进行此操作吗,将会把图片库中所有未加过水印的图片全部添加水印!")){

document.categoryForm.action="${ctx}/imageRepository.do?method=batchAddWaterMark";

document.categoryForm.submit();

}

}else{

alert("无法添加水印,请先到企业设置――图片水印参数设置中进行设置!");

returnfalse;

}

});

}

</script>

<divalign="center"><inputname="buttonDelete22"type="button"value="批量添加水印"class="buttonL"onclick="batchAddWaterMark();"/></div>

<divclass="font_gray"style="margin-top:10px;">提示:上传图片前请先在“默认上传分类设置”中绑定图片库分类</div>

</div>

</body>

</html>

3.业务类

(1)接口类:

packagecom.sitechasia.ebiz.imagerepository.service;

importjava.util.List;

importjava.util.Map;

importcom.sitechasia.ebiz.imagerepository.vo.ImageInfoVO;

importcom.sitechasia.webx.core.service.IValueObjectBaseService;

importcom.sitechasia.webx.core.support.Page;

publicinterfaceIImageInfoServiceextendsIValueObjectBaseService<ImageInfoVO>{

/**

*判断是否有水印策略

*@paramhasAddWaterMark

*@return

*/

publicStringisWaterMarkPolicy(StringhasAddWaterMark);

}

(2)实现类:

packagecom.sitechasia.ebiz.imagerepository.service.impl;

importjava.awt.Image;

importjava.awt.image.BufferedImage;

importjava.io.File;

importjava.io.FileNotFoundException;

importjava.io.FileOutputStream;

importjava.io.IOException;

importjava.util.ArrayList;

importjava.util.HashMap;

importjava.util.List;

importjava.util.Map;

importcom.sitechasia.ebiz.framework.web.ContextHolder;

importcom.sitechasia.ebiz.download.service.IDownloadFileCallService;

importcom.sitechasia.ebiz.filerepository.dao.IFileInfoDao;

importcom.sitechasia.ebiz.filerepository.model.FileInfoDO;

importcom.sitechasia.ebiz.filerepository.service.IFileInfoService;

importcom.sitechasia.ebiz.filerepository.vo.FileInfoVO;

importcom.sitechasia.ebiz.imagerepository.dao.IImageCategoryDao;

importcom.sitechasia.ebiz.imagerepository.dao.IImageInfoDao;

importcom.sitechasia.ebiz.imagerepository.dao.IModuleImgCategoryDao;

importcom.sitechasia.ebiz.imagerepository.model.ImageCategoryDO;

importcom.sitechasia.ebiz.imagerepository.model.ImageInfoDO;

importcom.sitechasia.ebiz.imagerepository.service.IImageInfoService;

importcom.sitechasia.ebiz.imagerepository.util.ReturnMessage;

importcom.sitechasia.ebiz.imagerepository.vo.ImageCategoryVO;

importcom.sitechasia.ebiz.imagerepository.vo.ImageInfoVO;

importcom.sitechasia.ebiz.imagewatermark.core.impl.WaterMarkPolicyGroup;

importcom.sitechasia.ebiz.imagewatermark.service.IMarkPolicyService;

importcom.sitechasia.ebiz.imagewatermark.service.IWaterMarkService;

importcom.sitechasia.webx.core.exception.BizLayerException;

importcom.sitechasia.webx.core.service.impl.BaseDOVOService;

importcom.sitechasia.webx.core.support.Page;

importcom.sitechasia.webx.core.utils.base.PopulateUtil;

importcom.sun.image.codec.jpeg.JPEGCodec;

importcom.sun.image.codec.jpeg.JPEGImageEncoder;

publicclassImageInfoServiceImplextends

BaseDOVOService<ImageInfoDO,ImageInfoVO>implementsIImageInfoService{

privateIImageInfoDaoimageInfoDao;

privateIImageCategoryDaoimageCategoryDao;

privateIFileInfoDaofileInfoDao;

privateIDownloadFileCallServicedownloadFileCallService;

privateIModuleImgCategoryDaomoduleImgCateDao;

privateIWaterMarkServicewaterMarkService;

privateIMarkPolicyServicemarkPolicyService;

privateIWaterMarkServicegifWaterMarkService;

publicStringisWaterMarkPolicy(StringhasAddWaterMark){

Stringpath="";

Stringresult="";

if(hasAddWaterMark!=null&&hasAddWaterMark.equals("1")){

StringsiteId=ContextHolder.getContext().getSiteId();

if(siteId!=null&&!siteId.equals("")){

path="./cluster/"+siteId

+"/configParameter/watermark/policy.xml";

if(!newFile(path).exists()){

result="notWaterMarkPolicy";

}else{

result="isWaterMarkPolicy";

}

}else{

logger.equals("Siteidisnotexiest");

}

}

returnresult;

}

}

4。错误分析:

注意:由于配置不对,将出现错误。

(1)第二种情况;

例如将

<dwr>

<allow>

<createcreator="spring"scope="page"javascript="imageInfoService">

<paramname="beanName"value="imageInfoService"/>

<includemethod="avalidateAddBizImage"/>

<includemethod="isWaterMarkPolicy"/>

</create>

</allow>

</dwr>写成

<dwr>

<allow>

<createcreator="spring"scope="page"javascript="imageInfoService22">

<paramname="beanName"value="imageInfoService"/>

<includemethod="avalidateAddBizImage"/>

<includemethod="isWaterMarkPolicy"/>

</create>

</allow>

</dwr>

并且在JS代码还用imageInfoService.isWaterMarkPolicy("1",function(result){

if(result=='isWaterMarkPolicy'){

if(confirm("您确定进行此操作吗,将会把图片库中所有未加过水印的图片全部添加水印!")){

document.categoryForm.action="${ctx}/imageRepository.do?method=batchAddWaterMark";

document.categoryForm.submit();

}

}else{

alert("无法添加水印,请先到企业设置――图片水印参数设置中进行设置!");

returnfalse;

}

});)

前台错误信息是:imageInfoService未定义

后台报错信息是:

18:28:28,560WARN[DefaultCreatorManager]Namesofknownclassesare:ServerTimeproductServicecustomerProccessServicepayTypeServiceproductcategoriseserviceproducttempaletServiceAdvertiseServiceshippayServicemarkservicebrandServiceinfoCategoryServicepricecomponentServicebpisServicepmcategoryServiceorderServiceimageInfoService22orderdiscountService

18:28:28,560WARN[UrlProcessor]Error:java.lang.SecurityException:Noclassbyname:imageInfoService

18:28:28,560WARN[UrlProcessor]Sent501

java.lang.SecurityException:Noclassbyname:imageInfoService

atorg.directwebremoting.impl.DefaultCreatorManager.getCreator(DefaultCreatorManager.java:167)

atorg.directwebremoting.impl.DefaultRemoter.generateInterfaceScript(DefaultRemoter.java:155)

atorg.directwebremoting.servlet.InterfaceHandler.handle(InterfaceHandler.java:45)

atorg.directwebremoting.servlet.UrlProcessor.handle(UrlProcessor.java:101)

atorg.directwebremoting.servlet.DwrServlet.doPost(DwrServlet.java:120)

atorg.directwebremoting.servlet.DwrServlet.doGet(DwrServlet.java:106)

atjavax.servlet.http.HttpServlet.service(HttpServlet.java:697)

atjavax.servlet.http.HttpServlet.service(HttpServlet.java:810)

atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)

atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)

atcom.sitechasia.ebiz.framework.web.ContextFilter.doFilterInternal(ContextFilter.java:63)

atorg.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:75)

atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)

atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)

atorg.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)

atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)

atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)

atorg.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)

atorg.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)

atorg.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:175)

atorg.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:74)

atorg.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)

atorg.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)

atorg.jboss.web.tomcat.tc5.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:156)

atorg.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)

atorg.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)

atorg.apache.jk.server.JkCoyoteHandler.invoke(JkCoyoteHandler.java:199)

atorg.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:282)

atorg.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:767)

atorg.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:697)

atorg.apache.jk.common.ChannelSocket$SocketConnection.runIt(ChannelSocket.java:889)

atorg.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)

atjava.lang.Thread.run(Thread.java:595)

(2)第二种情况;

如果将<scripttype="text/javascript"src="${ctx}/dwr/interface/imageInfoService.js"></script>写成

<scripttype="text/javascript"src="${ctx}/dwr/interface/imageInfoService33.js"></script>

并且在JS代码还用imageInfoService.isWaterMarkPolicy("1",function(result){

if(result=='isWaterMarkPolicy'){

if(confirm("您确定进行此操作吗,将会把图片库中所有未加过水印的图片全部添加水印!")){

document.categoryForm.action="${ctx}/imageRepository.do?method=batchAddWaterMark";

document.categoryForm.submit();

}

}else{

alert("无法添加水印,请先到企业设置――图片水印参数设置中进行设置!");

returnfalse;

}

});)

前台错误信息是:imageInfoService未定义

那么后台将报错误信息是:

18:38:42,457WARN[DefaultCreatorManager]Namesofknownclassesare:imageInfoServiceServerTimeproductServicecustomerProccessServicepayTypeServiceproductcategoriseserviceproducttempaletServiceAdvertiseServiceshippayServicemarkservicebrandServiceinfoCategoryServicepricecomponentServicebpisServicepmcategoryServiceorderServiceorderdiscountService

18:38:42,457WARN[UrlProcessor]Error:java.lang.SecurityException:Noclassbyname:imageInfoService33

18:38:42,457WARN[UrlProcessor]Sent501

java.lang.SecurityException:Noclassbyname:imageInfoService33

atorg.directwebremoting.impl.DefaultCreatorManager.getCreator(DefaultCreatorManager.java:167)

atorg.directwebremoting.impl.DefaultRemoter.generateInterfaceScript(DefaultRemoter.java:155)

atorg.directwebremoting.servlet.InterfaceHandler.handle(InterfaceHandler.java:45)

atorg.directwebremoting.servlet.UrlProcessor.handle(UrlProcessor.java:101)

atorg.directwebremoting.servlet.DwrServlet.doPost(DwrServlet.java:120)

atorg.directwebremoting.servlet.DwrServlet.doGet(DwrServlet.java:106)

atjavax.servlet.http.HttpServlet.service(HttpServlet.java:697)

atjavax.servlet.http.HttpServlet.service(HttpServlet.java:810)

atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)

atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)

atcom.sitechasia.ebiz.framework.web.ContextFilter.doFilterInternal(ContextFilter.java:63)

atorg.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:75)

atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)

atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)

atorg.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)

atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)

atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)

atorg.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)

atorg.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)

atorg.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:175)

atorg.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:74)

atorg.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)

atorg.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)

atorg.jboss.web.tomcat.tc5.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:156)

atorg.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)

atorg.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)

atorg.apache.jk.server.JkCoyoteHandler.invoke(JkCoyoteHandler.java:199)

atorg.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:282)

atorg.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:767)

atorg.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:697)

atorg.apache.jk.common.ChannelSocket$SocketConnection.runIt(ChannelSocket.java:889)

atorg.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)

atjava.lang.Thread.run(Thread.java:595)

*/

相关推荐