struts1.2 ajax jquery json

json用到的jar包有:commons-collections-3.2.jar,commons-lang-2.3.jar,ezmorph-1.0.3.jar,json-lib-2.1.jar,需要注意的是如果用到hibernate了,要把hibernate中的commons-collections.jar删掉,要不会有jar包冲突。

我写的是级联菜单:

 jsp中:

   <script type="text/javascript" src="js/jquery.js"></script>
   <script type="text/javascript">
       $(document).ready( function() {
         $("#sortid").change( function() {
             var url = '${ctx}/product/productType.do?method=typeList';
             var params = {
              sortid:$("#sortid").val()
             }
             $.post(
                 url,        //服务器要接受的url
                 params,     //传递的参数
                 function cbf(data){ //服务器返回后执行的函数 参数 data保存的就是服务器发送到客户端的数据
                    $("#typeid option").remove();
              var tempOption = document.createElement("option");
              tempOption.value = "0"
        tempOption.innerHTML  = "请选择---";
        $("#typeid").append(tempOption);
                    for(var i=0;i<data.length;i++){
                     var tempOption = document.createElement("option");
          tempOption.value = data[i].typeid;
          tempOption.innerHTML  = data[i].name;
          alert(item.name);
          $("#typeid").append(tempOption);
                    }
                 },
                'json'   //数据传递的类型  json
             );
         });
       });
       </script>

 

<table >
   <tr>
    <td width="20%">产品分类名称</td>
    <td align="left" width="30%">
     <select name="sortid" id="sortid">
      <c:forEach items="${sortMapList}" var="map" varStatus="i">
       <option value="${map.sortid}" <c:if test="${map.sortid==checkedSortid}">selected</c:if>>${map.name}</option>
      </c:forEach>
        </select>
    </td>
    <td width="20%">产品类型名称</td>
    <td align="left">
     <select name="typeid" id="typeid" >
      <option value="0">请选择---</option>
      <c:forEach items="${typeList}" var="type" varStatus="i">
       <option value="${type.typeid}"<c:if  test="${type.typeid==typeid}">selected</c:if>>${type.name}</option>
      </c:forEach>
     </select>
    </td>
   </tr>
  </table>

 

 

action中:

public ActionForward typeList(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response){
  try {
   DynaValidatorForm typeForm = (DynaValidatorForm) form;
   int productSortid = typeForm.get("sortid")==null?0:(Integer)typeForm.get("sortid");
   List<Type> typeList = this.productTypeService.TypeListBySortid(productSortid);
   List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
   for(int i=0;i< typeList.size();i++){
    Map<String,Object> map = new HashMap<String,Object>();
    int typeid = typeList.get(i).getTypeid();
    map.put("typeid", typeid);
    String name = typeList.get(i).getName();
    map.put("name", name);
    list.add(map);
   }
   JSONArray array = JSONArray.fromObject(list);
   JSONArray ja = JSONArray.fromObject(array);
   request.setAttribute("ja", ja);
   response.setCharacterEncoding("UTF-8"); 
   response.getWriter().write(ja.toString());
  } catch (IOException e) {
   e.printStackTrace();
  }
  return mapping.findForward("null");
 }

这样就行了,呵呵!

 

 

 

你可能感兴趣的:(jquery,c,Hibernate,json,Ajax)