1、首先需要引入dwr.jar包到工程中的lib中;2、在web.xml中配置dwr,如下:<!—— DWR配置 ——>
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
<init-param>
<param-name>scriptCompressed</param-name>
<param-value>false</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
3、然後在WEB-INF下創建一個dwr.xml,並在裡面將調用處理類配置,例如:<dwr> <allow> <convert match="com.po.OneSearchDto" converter="bean"> <param name="include" value="resname,resid"></param> </convert>
<create javascript="GetDataBaseRes" creator="new"> <param name="class" value="com.util.GetDataBaseRes"></param> <include method="getDataBaseRes"/> </create>
</allow> </dwr>
4、然後在需要調用的jsp中,將調用處理js類引入,如下:<% String contextPath = request.getContextPath();%> <script type='text/javascript' src='<%=contextPath %>/dwr/interface/GetDataBaseRes.js'></script> <script type='text/javascript' src='<%=contextPath %>/dwr/engine.js'></script> <script type='text/javascript' src='<%=contextPath %>/dwr/util.js'></script>
調用的js例子如下:function query(){ var keyWord = document.getElementById("keyWord")。value;var dataStr = "";var arr = document.getElementsByName("checkboxData");for(var i=0;i<arr.length;i++){ if(arr[i].checked){ dataStr+=","+arr[i].value;} GetDataBaseRes.getDataBaseRes(keyWord,dataStr,disPoseRes);
}
function disPoseRes(str){ alert(str);}
5、如果需要在dwr調用類中獲得session,或者request,如下:org.directwebremoting.WebContext wc = org.directwebremoting.WebContextFactory.get();HttpSession session = wc.getSession();HttpServletRequest request = wc.getHttpServletRequest();
作者 sdtarena