DWR是一个开源的服务器端ajax框架,js通过直接调用普通java类中的方法实现服务器端数据处理。原理:框架根据配置在加载页面的时候自动生成ajax代码供前端调用。
该框架只需一个jar包 csdn有下载:dwr(免费)
web.xml
<listener><!--配置监听器 默认启动时加载dwr.xml(后面有配置) 路径和web.xml一致--> <listener-class>org.directwebremoting.servlet.DwrListener</listener-class> </listener> <servlet><!-- 配置servlet和映射路径 下面有相关参数 比如fileUploadMaxBytes是允许文件上传的大小--> <servlet-name>dwr-invoker</servlet-name> <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class> <init-param> <param-name>fileUploadMaxBytes</param-name> <param-value>25000000000000</param-value> </init-param> <init-param> <param-name>debug</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>accessLogLevel</param-name> <param-value>runtimeexception</param-value> </init-param> <init-param> <param-name>activeReverseAjaxEnabled</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>initApplicationScopeCreatorsAtStartup</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>jsonRpcEnabled</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>jsonpEnabled</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>preferDataUrlSchema</param-name> <param-value>false</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dwr-invoker</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping>dwr.xml(与web.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 creator="new" javascript="mytest"><!-普通java类--> <param name="class" value="org.ITschool.dwr.utilcontroller.mytest"/> </create> <!-- this is a bad idea for live, but can be useful in testing --> <!-- <create creator="spring" javascript="User_Service"> <param name="BeanName" value="User_Service" /> </create>--> <create javascript="User_Service" creator="spring"><!--使用Spring配置中的bean--> <param name="beanName" value="User_Service"/> </create> <create creator="new" javascript="Uploadprocess"> <param name="class" value="org.ITschool.dwr.utilcontroller.Uploadprocess"/> </create> <create creator="new" javascript="uploadFile"> <param name="class" value="org.ITschool.dwr.utilcontroller.uploadFile"/> </create> <convert converter="exception" match="java.lang.Exception"/> <convert converter="bean" match="java.lang.StackTraceElement"/> </allow> </dwr>现在看下上面mytest.java文件的代码(以我项目中的代码为例)
package org.ITschool.dwr.utilcontroller; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.InputStreamReader; import org.directwebremoting.WebContext; import org.directwebremoting.WebContextFactory; import net.sf.json.JSON; import net.sf.json.JSONObject; public class mytest { public JSON getInfo(String info1){ JSONObject json=new JSONObject(); json.put("name", "XXXX"); json.put("name1", info1); return json; }}最后看下前端调用了(引入必要的js)
<script type='text/javascript' src='dwr/engine.js'></script><!--必须引入的 js代码由框架自动生成-->
<script type='text/javascript' src='dwr/util.js'></script><!--必须引入的 js代码由框架自动生成-->
<script type='text/javascript' src='dwr/interface/mytest.js'></script><!---dwr.xml中配置的mytest java类->
再看调用
<script type='text/javascript'>
mytest.getInfo(“方法参数”,function(data){//前一个是方法参数 后面是回调函数
alert(data.name)
});
</script>
运行结果:弹出 XXXX内容的提示框架。