1. commUtil 類html
public static <T> String convert2Json(List<T> tempList, String idMethod, String valMethod){
if(tempList == null || tempList.isEmpty() || StringUtil.isNullOrEmpty(idMethod) || StringUtil.isNullOrEmpty(valMethod)){
return "";
}
String arr = "[";
for (T obj : tempList){
Method method;
try {
arr += "{\"id\":\"";
method = obj.getClass().getMethod(idMethod);
arr += Long.parseLong(method.invoke(obj).toString()) + "\",";
arr += "\"name\":\"";
method = obj.getClass().getMethod(valMethod);
arr += method.invoke(obj).toString() + "\"},";
} catch (Exception e) {
e.printStackTrace();
}
}
int len = arr.length();
if (len > 1){
arr.substring(len - 1);
}
arr += "]";
return arr;
}ajax
2. controller 類app
@RequestMapping(params = "method=findUseEmsClampTypes")
@ResponseBody
public String findUseEmsClampTypes(HttpServletRequest request, HttpServletResponse response){
Long assemblyTypeId = RequestUtil.parseLong(request, "assemblyTypeId");
List<EmsClampUseType> useList = emsClampUseTypeManager.findUseEmsClampTypes(assemblyTypeId);
return CommUtil.convert2Json(useList, "getUseTypeId", "getUseType");
}url
3. js htm
$.ajax({
type : "POST",
url : "${ctx}/ems/emsClampUtil.html?method=findUseEmsClampTypes",
data : "assemblyTypeId=" + assemblyTypeId,
success : function(data) {
var obj = eval(data);
$.each(obj,function(i){
if (obj[i].id == selectedUseTypeId){
$("#useTypeId").append("<option value='" + obj[i].id + "' checked>" + obj[i].name + "</option>");
} else {
$("#useTypeId").append("<option value='" + obj[i].id + "'>" + obj[i].name + "</option>");
}
$("#useTypeId").render();
});
}
});get