Action 代码
//查询出页面所需数据
public String findAll(){
//此处需要servlet的打印流和相应对象
HttpServletResponse response = ServletActionContext.getResponse();
//设置相应对象
response.setContentType("text/html");
//设置编码
response.setCharacterEncoding("UTF-8");
if(pr!=null){
try {
String pname = new String(pr.getPname().getBytes("ISO-8859-1"),"UTF-8");
String manufact=new String(pr.getManufact().getBytes("ISO-8859-1"),"UTF-8");
String price = new String(pr.getPrice().getBytes("ISO-8859-1"),"UTF-8");
String model=new String(pr.getModal().getBytes("ISO-8859-1"),"UTF-8");
System.out.println(pname+":"+manufact+":"+price+":"+model);
pr.setManufact(manufact);
pr.setModal(model);
pr.setPname(pname);
pr.setPrice(price);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//执行dao方法获取数据
List<Product> li = new ProductDao().findAll(page,pr);
System.out.println(li.toString());
//new 出一个json对象
JSONObject j = new JSONObject();
for(int i=0;i<li.size();i++){
JSONObject jn = new JSONObject();
//添加json格式
jn.put("id", li.get(i).getId());
jn.put("pname", li.get(i).getPname());
jn.put("modal", li.get(i).getModal());
jn.put("price", li.get(i).getPrice());
jn.put("manufact", li.get(i).getManufact());
jn.put("stockNum", li.get(i).getStockNum());
jn.put("inDate",String.valueOf(li.get(i).getInDate()));
j.put(i, jn);
}
response.setCharacterEncoding("UTF-8");
try {
PrintWriter out = response.getWriter();
out.print(j);
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("0000000000000000000000000");
return null;
}
JSP
<table id="selectable1">
<tr class="ui-widget-content">
<th>序号</th>
<th>商品名</th>
<th>型号</th>
<th>价格</th>
<th>生产厂家</th>
<th>库存数量</th>
<th>入库时间</th>
</tr>
</table>
<table id="selectable">
<s:iterator value="#li" var="it">
<tr><td><s:property value="#it.id"/></td><td><s:property value="#it.pname"/></td><td><s:property value="#it.modal"/></td><td><s:property value="#it.price"/></td><td><s:property value="#it.manufact"/></td><td><s:property value="#it.stockNum"/></td><td><s:property value="#it.inDate"/></td></tr>
</s:iterator>
</table>
</div>
JSP 不知道怎么弄了。。急!!
------解决方案--------------------
个人认为:你这个不需用json封装数据。如果你直接用struts2,定义一个list,action中有这个list,getter方法。jsp就可以直接struts2标签读取数据。
------解决方案--------------------
josn 的例子,自己看吧
- HTML code
function testxJsonAjax() { var url ="<%=urlroot%>/criterion.do?method=josns&bmid="+document.getElementById('bmid').value; new Ajax.Request(url,{onComplete:showResponsesx}); } function showResponsesx(xmlHttp) { if(xmlHttp.readyState==4) { if(xmlHttp.status==200) { var result = xmlHttp.responseText; var str = eval("("+result+")"); while(document.all.bzid.options.length>0) { document.all.bzid.options.remove(0); } var objSelect = document.getElementById('bzid'); var objOption = new Option(); for(var i=0;i<str.users.length;i++) { var objSelect = document.getElementById('bzid'); var objOption = new Option(); objOption.text= str.users[i].name; objOption.value= str.users[i].id; objSelect.options.add(objOption); } } } }