一直很迷惘,ssh框架不知怎么从前台获取后台数据,折腾了两天,终于搞清楚了 特别是关于ajax中action路径的url搞了很久
ajax 需要以下几个包,同时还需在webapp/web-inf/下建lib,在里面也放入下列包,不然会报找不到方法
<pre name="code" class="html">commons-beanutils-1.7.0.jar commons-collections-3.2.1.jar commons-httpclient-3.1.jar commons-lang-2.3.jar commons-logging-1.1.1.jar ezmorph-1.0.3.jar json-lib-2.2.3-jdk15.jar struts2-json-plugin-2.1.8.1.jar
</pre><pre code_snippet_id="1686553" snippet_file_name="blog_20160517_2_7526341" name="code" class="html"><img src="http://img.blog.csdn.net/20160517155514934?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
java代码
@Actions({@Action(value="ExpressCo",results = {@Result(name = ActionSupport.SUCCESS,location = "/EConsultApply/eConsultApply.jsp")}),})
@Namespace("") public class BasicInformationAction { public String extcute() throws Exception{ HttpServletResponse response = ServletActionContext.getResponse(); ArrayList<ExpressCo> expressColist=basicInformationServiceI.search(); JSONArray ja = JSONArray.fromObject(expressColist); //用response向前台写数据 response.getWriter().write(ja.toString()); response.getWriter().flush(); response.getWriter().close(); return null; } }
jsp代码
</pre><pre code_snippet_id="1686553" snippet_file_name="blog_20160517_5_9097287" name="code" class="html">
<pre name="code" class="html"><li><label>快递公司:</label> <select name="CA_ECID" id="CA_ECID" type="text"> </select></li>
/* jQuery.ajax({ type: "POST",url: "",// 要请求的后台页面 data: "",// 要传的参数 dataType:'json',success: function(json){ },error:function(){ alert('ajax 失败'); } }); */ $.getJSON("ExpressCo!extcute.action",function (data){ //当成功获取JSON时 <pre name="code" class="javascript">$.each(data,function(i,item){ //循环JSON列表,如果JSON数据不是列表,就没得循环了 //这里就可以直接根据list里的实体的属性去取值了,比如item.UserName //alert('ajax good'); //设置下拉列表中的值的属性 var option = document.createElement("option"); option.value = item.EC_ID; option.text= item.EC_NAME; console.log(option); //将option增加到下拉列表中。 selections.options.add(option); });
});
</pre><pre code_snippet_id="1686553" snippet_file_name="blog_20160517_5_9097287" name="code" class="html">