|
|
大家知道,后台程序通常情况下无法与js进行交互,而我们一般通过组成XML的形式来传递我们想要的数据,或者说用ajax,dwr等,这里只是介绍其中的一种方法,可能不是最好的,但至少能很快的解决问题:
1.所需要取到的数据结构形为:
var object={
"strArr1":[{
"brandId":"1"},{"brandName":"三星"},{"childs":[{"phoneType":"N870"},{}]
}],
"strArr2":[{},{}]
}
2.明白了我们想要的数据结构,之后我们将在Action中对所要的数据进行拼凑,在action中我们可以建立一个Map对象:Map<Brand,List<ClientSoft>> map=new HashMap<Brand,List<ClientSoft>>
拼凑:
public String execute() throws Exception {Map<Brand, List<ClientSoft>> clientSoftMap = clientSoftManager.getClientSoftInfo();Iterator it = clientSoftMap.entrySet().iterator();StringBuffer sb = new StringBuffer();sb.append("var str={\"strArr\":[");while (it.hasNext()) {Map.Entry entry = (Map.Entry) it.next();Object key = entry.getKey();/* 对键进行处理 */Brand brand = (Brand) key;Long brandId = brand.getBrandId();String brandName = brand.getBrandName();String brandUrl = brand.getImageUrl();Object value = entry.getValue();/* 对值进行处理:子型号,版本号,手机图片,标识软件版本号,软件下载地址,大小,版本,Release号,软件描述,下载次数 */List<ClientSoft> liCs = (List<ClientSoft>) value;System.out.println("键:" + key + ";" + "值:" + value);sb.append("{");sb.append("\"brandName\":" + "\"" +brandName+"\"");sb.append(",");sb.append("\"brandId\":" +"\""+ brandId + "\"");sb.append(",");sb.append("\"brandUrl\":" + "\"" + brandUrl +"\"");sb.append(",");sb.append("\"childs\":");sb.append("[");if (liCs.size() != 0) {for (ClientSoft clientSoft : liCs) {sb.append("{");sb.append("\"phoneType\":" + "\""+ clientSoft.getPhoneType() + "\"");sb.append(",");sb.append("\"ua\":" + "\"" + clientSoft.getUa() + "\"");sb.append(",");sb.append("\"imageUrl\":" + "\"" + clientSoft.getImageUrl()+ "\"");sb.append(",");sb.append("\"clientUa\":" + "\"" + clientSoft.getClientUa()+ "\"");sb.append(",");sb.append("\"URL\":" + "\"" + clientSoft.getUrl() + "\"");sb.append(",");sb.append("\"softSize\":" + "\"" + clientSoft.getSoftSize()+ "\"");sb.append(",");sb.append("\"version\":" + "\"" + clientSoft.getVersion()+ "\"");sb.append(",");sb.append("\"release\":" + "\"" + clientSoft.getRelease()+ "\"");sb.append(",");sb.append("\"description\":" + "\""+ clientSoft.getDescription() + "\"");sb.append(",");sb.append("\"createDate\":" + "\""+ clientSoft.getCreateDate() + "\"");sb.append(",");sb.append("\"downloadCount\":" + "\""+ clientSoft.getDownloadCount() + "\"");sb.append("},");}sb = new StringBuffer(sb.toString().substring(0,sb.length() - 1));}sb.append("]");sb.append("},");}sb=new StringBuffer(sb.substring(0, sb.length() - 1));sb.append("]}");String str=sb.toString();ServletActionContext.getRequest().setAttribute("str",str);return SUCCESS;}4.到页面执行
function initBrand(){var strTmp='${requestScope.str}';eval(strTmp);phoneStr=str;for(var o = 0; o < str.strArr.length; o++){var op = document.createElement("OPTION");op.text=str.strArr[o].brandName;op.value=o;document.getElementById("vendor").options.add(op);}${"brandSelId"}.value=0;} |
|