关于Struts2 + spring + hibernate进Q群: 130529143交流。
有偿技术支持Q群:398162181
1.在后台将数据封装到json中:
List<Product> productList = productService.getProductList(1,typeId);
StringBuffer stringb = new StringBuffer("[");
for(int i = 0; i < productList.size(); i++)
{
stringb.append("{");
stringb.append("id:\"" + productList.get(i).getId() + "\"," );
stringb.append("typeId:\"" + productList.get(i).getTypeId() + "\"," );
stringb.append("name:\"" + productList.get(i).getName() + "\"," );
stringb.append("presentation:\"" + productList.get(i).getPresentation() + "\"," );
stringb.append("path:\"" + productList.get(i).getPath() + "\"," );
stringb.append("status:\"" + productList.get(i).getStatus() + "\"" );
stringb.append("}");
if(i < productList.size() - 1)
{
stringb.append(",");
}
}
stringb.append("]");
try
{
//将封装好的json格式的字符串传到前台页面
getResponse().getWriter().print(stringb.toString());
}
catch (Exception e)
{
e.printStackTrace();
}
2.在前台页面通过js使用json格式的字符串:
/*
flag="delete":删除productId的产品
flag="add":新增typeId类的产品
flag="select":查询typeId的所有产品
*/
function showProductList(flag, productId, typeId){
//为全局mainTypeId赋值
mainTypeId = "typeList" + typeId;
var proName = "";
if(flag == "add"){
proName = $("#proName").val();
if(proName == ""){
alert("请输入需添加的产品名称 !");
return false;
}
}
$.ajax({
type : "POST",
url : "manage_product_list.action",
data : "flag=" + flag + "&typeId=" + typeId + "&proId=" + productId + "&proName=" + proName,
dataType : "text",
success : function(data) {
//alert(data);
//将字符串转为JSON对象
var obj = eval('(' + data + ')');
var length = obj.length;
//拼html
var _html = "<ul>";
for(var i = 0; i < length; i++){
_html += "<li class='productList link' id=\"" + obj[i].id+ "\">" + (i+1) + "." +obj[i].name + "</li>" +
"<li class='productDelBtn link' οnclick='showProductList(" + "\"delete\"," +obj[i].id + "," + obj[i].typeId+ ");'>✘</li>" ;
}
_html += "<li id='addProductList'><input id='proName' type='text'></li>" +
"<li class='productDelBtn link' οnclick='showProductList(" + "\"add\"," + "0," + typeId + ");'>✔</li>"
_html += "</ul>";
//将_html加入div
$("#list").children("div").html(_html);
$(".productList").each(function(){
$(this).click(function(){
$("#content").css("display", "block");
$("#list").css("display", "none");
//id=product_img的iframe的src赋值
$("#product_img").attr("src","manage_product_img.action?productId=" + $(this).attr("id"));
var productId = $(this).attr("id");
$("#productId").attr("value",productId);
for(var i = 0; i < length; i++){
var id = obj[i].id;
if(productId == id){
var name = obj[i].name;
var presentation = obj[i].presentation;
var status = obj[i].status;
//alert(status);
$("#productType option[value = " + typeId + "]").attr("selected",true);
$("#productName").val(name);
$("#content input[type = 'radio'][value=" + status + "]").attr("checked",true);
$("#presentation").val(presentation);
}
}
});
});
}
});
}
关于Struts2 + spring + hibernate进Q群: 130529143交流。
有偿技术支持Q群:398162181