dwr的ajax实现

前端之家收集整理的这篇文章主要介绍了dwr的ajax实现前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

记性太差,做个笔记、、、

第一步:

下载dwr的jar包放到lib文件夹下;

点击打开链接

第二步:

在web.xml配置DWR;

<servlet>
    <servlet-name>dwr-invoker</servlet-name>
    <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
    <init-param>
      <param-name>debug</param-name>
      <param-value>true</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>dwr-invoker</servlet-name>
    <url-pattern>/dwr/*</url-pattern>
  </servlet-mapping>
第三步:

创建DWR配置文件dwr.xml

<dwr>
  <allow>
         
    <create creator="new" javascript="AjaxBean">    
      <param name="class" value="com.test.ajax.AjaxBean"/>
    </create>-->
    
    <create creator="new" javascript="AjaxMonitor">    
      <param name="class" value="com.anthony.struts2.action.MonitorAction"/>
    </create>
    
    <!-- 配置DWR表单验证器 -->    
	<create javascript="validator" creator="new">
	    <param name="class"
	        value="org.apache.struts2.validators.DWRValidator" />
	</create>
	<convert converter="bean"
	    match="com.opensymphony.xwork2.ValidationAwareSupport" />
  </allow>
  <signatures>
	    <![CDATA[
	    import java.util.Map;
	    import org.apache.struts2.validators.DWRValidator;
	
	    DWRValidator.doPost(String,String,Map<String,String>);
	    ]]>
  </signatures>  
</dwr>
第四步:

访问http://localhost:8080/PoliceMap02/dwr,也就是自己项目的后面加个dwr,获取如下资源

<script src="./dwr/engine.js"></script>  
<script src="./dwr/util.js"></script> 
<script src="./dwr/interface/AjaxMonitor"></script>
再将上述的资源放到页面的头部

第五步:

测试:

页面提交请求

//设置短信中心号码
	function SetPhoneSMS(){
		var key = onlineList.options[onlineList.selectedIndex].value;
		var num = document.getElementById('setphonenum').value;
		window.alert(num);
		AjaxMonitor.setPhoneNum(key,num,processSMS);
	}

后台处理:
	public class MonitorAction {
public String setPhoneNum(String key,String num){
		
		int type = OrderSet.SMSCENTERNUM;
		SendSetSMSCenterNumber centerNumber = new SendSetSMSCenterNumber();
		centerNumber.setSMSNum(num);
		byte[] bytes = centerNumber.toBytes();
		HandleSend.parSEOrder(type,key,bytes);
		return null;
	}

猜你在找的Ajax相关文章