在我们做Web开发中,用到Ajax的地方可谓是随处可见。在Webx的官方文档中,对于Ajax没有明确的支持说明,但是我们可以用另外的两种请求方式来变相实现这种效果。一种是.do请求,一种是.json请求,另一种还是json请求,一种是.htm请求。下面我们来对这四种请求方式做个说明。
<script type="text/javascript" src="static/js/jquery-2.1.4.min.js"></script> 第一种Ajax方式 <select name="provinceEname_fir"> <option>请选择</option> #foreach($province in $!{provinceChina}) <option value="$!{province.ename}">$!{province.cname}</option> #end </select> <select name="cityEname_fir"> <option>请选择</option> </select> <input type="hidden" value="getCityByProvinceEname.do" name="urlName_fir"> <br/>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"> </span><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">添加onchange事件</span>
<pre name="code" class="html"><script> $(function(){ <!--城市二级联动--> $("select[name^='provinceEname']").each(function(j){ $(this).on('change',function(){ var cityVal = $(this).val(); var uri = $($("input[name^='urlName']")[j]).val(); $.ajax({ type:"post", data:{"cityEname":cityVal}, dataType:"json", url:uri, success:function(msg){ if(msg != "{}"){ $($("select[name^='cityEname']")[j]).empty(); for(var i=0;i<msg.length;i++){ $($("select[name^='cityEname']")[j]).append("<option value='"+msg[i].id+"'>"+msg[i].name+"</option>"); } }else{ } }, error:function(){ alert("请求错误!"); } }); }) }); } ); </script>
package com.alibaba.webx.MyWebxTest.myWebX.module.screen; import java.io.OutputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.alibaba.citrus.turbine.Context; import com.alibaba.citrus.turbine.dataresolver.Param; import com.alibaba.webx.MyWebxTest.base.BaseScreenAction; import com.alibaba.webx.MyWebxTest.myWebX.module.dao.object.CityChinaDO; /** * * @author zkn 2016-05-15 * */ public class GetCityByProvinceEname extends BaseScreenAction { public void execute(@Param("cityEname") String cityEname, Context context){ List<CityChinaDO> listCity = mainService.getAddressService().getAllCityByEname(cityEname); List<Map<String, String>> list = new ArrayList<Map<String, String>>(); OutputStream os = null; for (CityChinaDO city : listCity) { Map<String, String> map = new HashMap<String, String>(); map.put("id", city.getEcityName()); map.put("name", city.getCcityName()); list.add(map); } String str = listToJson(list); closeOutStream(os, str); } }
<pre name="code" class="html">第二种Ajax方式 <select name="provinceEname_sec"> <option>请选择</option> #foreach($province in $!{provinceChina}) <option value="$!{province.ename}">$!{province.cname}</option> #end </select> <select name="cityEname_sec"> <option>请选择</option> </select> <input type="hidden" value="get_city_by_province_name/china_city.json" name="urlName_sec"> <br/>
<pre name="code" class="html"><script> $(function(){ <!--城市二级联动--> $("select[name^='provinceEname']").each(function(j){ $(this).on('change',function(){ var cityVal = $(this).val(); var uri = $($("input[name^='urlName']")[j]).val(); $.ajax({ type:"post", data:{"cityEname":cityVal}, dataType:"json", url:uri, success:function(msg){ if(msg != "{}"){ $($("select[name^='cityEname']")[j]).empty(); for(var i=0;i<msg.length;i++){ $($("select[name^='cityEname']")[j]).append("<option value='"+msg[i].id+"'>"+msg[i].name+"</option>"); } }else{ } }, error:function(){ alert("请求错误!"); } }); }) }); } ); </script>
package com.alibaba.webx.MyWebxTest.myWebX.module.screen; import java.io.OutputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.alibaba.citrus.turbine.Context; import com.alibaba.citrus.turbine.dataresolver.Param; import com.alibaba.webx.MyWebxTest.base.BaseScreenAction; import com.alibaba.webx.MyWebxTest.myWebX.module.dao.object.CityChinaDO; /** * * @author zkn 2016-05-15 * */ public class GetCityByProvinceName extends BaseScreenAction { public void doChinaCity(@Param("cityEname") String cityEname, Context context){ List<CityChinaDO> listCity = mainService.getAddressService().getAllCityByEname(cityEname); List<Map<String, String>> list = new ArrayList<Map<String, String>>(); OutputStream os = null; for (CityChinaDO city : listCity) { Map<String, String> map = new HashMap<String, String>(); map.put("id", city.getEcityName()); map.put("name", city.getCcityName()); list.add(map); } String str = listToJson(list); closeOutStream(os, str); } }在json中我们需要注意的有三点: 1、json请求对应的类要放到screen下面;2、json请求对应的类中不可以用execute方法,因为在找类的处理适配器的时候回出现错误;
第三种Ajax方式 <select name="provinceEname_thir"> <option>请选择</option> #foreach($province in $!{provinceChina}) <option value="$!{province.ename}">$!{province.cname}</option> #end </select> <select name="cityEname_thir"> <option>请选择</option> </select> <input type="hidden" value="get_city_by_province_name_thir.json" name="urlName_thir"> <br/>
<script> $(function(){ <!--城市二级联动--> $("select[name^='provinceEname']").each(function(j){ $(this).on('change',function(){ var cityVal = $(this).val(); var uri = $($("input[name^='urlName']")[j]).val(); $.ajax({ type:"post", data:{"cityEname":cityVal}, dataType:"json", url:uri, success:function(msg){ if(msg != "{}"){ $($("select[name^='cityEname']")[j]).empty(); for(var i=0;i<msg.length;i++){ $($("select[name^='cityEname']")[j]).append("<option value='"+msg[i].id+"'>"+msg[i].name+"</option>"); } }else{ } }, error:function(){ alert("请求错误!"); } }); }) }); } ); </script>后台代码如下:
package com.alibaba.webx.MyWebxTest.myWebX.module.screen; import java.io.OutputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.alibaba.citrus.turbine.Context; import com.alibaba.citrus.turbine.dataresolver.Param; import com.alibaba.webx.MyWebxTest.base.BaseScreenAction; import com.alibaba.webx.MyWebxTest.myWebX.module.dao.object.CityChinaDO; /** * * @author zkn * */ public class GetCityByProvinceNameThir extends BaseScreenAction { public void execute(@Param("cityEname") String cityEname, Context context){ List<CityChinaDO> listCity = mainService.getAddressService().getAllCityByEname(cityEname); List<Map<String, String>> list = new ArrayList<Map<String, String>>(); OutputStream os = null; for (CityChinaDO city : listCity) { Map<String, String> map = new HashMap<String, String>(); map.put("id", city.getEcityName()); map.put("name", city.getCcityName()); list.add(map); } String str = listToJson(list); closeOutStream(os, str); } }说明:在上例中我们发送了一个get_city_by_province_name_thir.json的请求,我们需要在screen下面创建一个对应的类GetCityByProvinceNameThir.java其实和第一种方
第四种Ajax方式 <select name="provinceEname_four"> <option>请选择</option> #foreach($province in $!{provinceChina}) <option value="$!{province.ename}">$!{province.cname}</option> #end </select> <select name="cityEname_four"> <option>请选择</option> </select> <input type="hidden" value="get_city_by_province_name_four.htm" name="urlName_four">
<script> $(function(){ <!--城市二级联动--> $("select[name^='provinceEname']").each(function(j){ $(this).on('change',function(){ var cityVal = $(this).val(); var uri = $($("input[name^='urlName']")[j]).val(); $.ajax({ type:"post", data:{"cityEname":cityVal}, dataType:"json", url:uri, success:function(msg){ if(msg != "{}"){ $($("select[name^='cityEname']")[j]).empty(); for(var i=0;i<msg.length;i++){ $($("select[name^='cityEname']")[j]).append("<option value='"+msg[i].id+"'>"+msg[i].name+"</option>"); } }else{ } }, error:function(){ alert("请求错误!"); } }); }) }); } ); </script>后台代码如下:
package com.alibaba.webx.MyWebxTest.myWebX.module.screen; import java.io.OutputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.alibaba.citrus.turbine.Context; import com.alibaba.citrus.turbine.dataresolver.Param; import com.alibaba.webx.MyWebxTest.base.BaseScreenAction; import com.alibaba.webx.MyWebxTest.myWebX.module.dao.object.CityChinaDO; /** * * @author zkn * */ public class GetCityByProvinceNameFour extends BaseScreenAction { public void execute(@Param("cityEname") String cityEname, Context context){ List<CityChinaDO> listCity = mainService.getAddressService().getAllCityByEname(cityEname); List<Map<String, String>> list = new ArrayList<Map<String, String>>(); OutputStream os = null; for (CityChinaDO city : listCity) { Map<String, String> map = new HashMap<String, String>(); map.put("id", city.getEcityName()); map.put("name", city.getCcityName()); list.add(map); } String str = listToJson(list); closeOutStream(os, str); } }对于.htm请求和我们常见的.htm请求基本上没什么区别,我们只需要把我们需要的值通过response输出到浏览器端就可以了。