首先需要引入dwrjar包到工程中的lib中在webxml中配置dwr如下<!—— DWR配置 ——>
<servlet>
<servletname>dwrinvoker</servletname>
<servletclass>orgdirectwebremotingservletDwrServlet</servletclass>
<initparam>
<paramname>scriptCompressed</paramname>
<paramvalue>false</paramvalue>
</initparam>
</servlet>
<servletmapping>
<servletname>dwrinvoker</servletname>
<urlpattern>/dwr/*</urlpattern>
</servletmapping>
然後在WEBINF下創建一個dwrxml並在裡面將調用處理類配置例如<dwr> <allow> <convert match=compoOneSearchDto converter=bean> <param name=include value=resnameresid></param> </convert>
<create javascript=GetDataBaseRes creator=new> <param name=class value=comutilGetDataBaseRes></param> <include method=getDataBaseRes/> </create>
</allow> </dwr>
然後在需要調用的jsp中將調用處理js類引入如下<% String contextPath = requestgetContextPath()%> <script type=text/javascript src=<%=contextPath %>/dwr/interface/GetDataBaseResjs></script> <script type=text/javascript src=<%=contextPath %>/dwr/enginejs></script> <script type=text/javascript src=<%=contextPath %>/dwr/utiljs></script>
調用的js例子如下function query(){ var keyWord = documentgetElementById(keyWord)valuevar dataStr = var arr = documentgetElementsByName(checkboxData)for(var i=i<arrlengthi++){ if(arr[i]checked){ dataStr+=+arr[i]value} GetDataBaseResgetDataBaseRes(keyWorddataStrdisPoseRes)
}
function disPoseRes(str){ alert(str)}
如果需要在dwr調用類中獲得session或者request如下orgdirectwebremotingWebContext wc = orgdirectwebremotingWebContextFactoryget()HttpSession session = wcgetSession()HttpServletRequest request = wcgetHttpServletRequest()
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/19678.html