前台jsp ajax请求数据:
function updateActivity(id){
$("#saveUpdateTitle").html("编辑活动");
var h = $(document).height();
$('.screen').css({ 'height': h });
$('.screen').show();
$('.active_name').center();
$('.active_name').fadeIn();
$.ajax({
type:"post",
url:"SalesPosActivityAction!toUpdateSeriesActivity.action",
dataType: "json",
data:{"salesPosActivityId":id},
async:false,
success:function(data){
$("#salesPosActivityName").val(data.name);
$("#salesPosActivityRebate").val(data.rebate);
$("#startDate").val(data.startDate);
$("#endDate").val(data.endDate);
$("#salesPosActivityId").val(id);
if(data.validity){
$("#validity").val("true")
}else{
$("#validity").val("false")
}
$.each(data.seriesStyles, function (i, item) {
var style = item.id;
$("input[value='"+style+"']").attr("checked",true);
});
}
});
}
后台方法:
public void toUpdateSeriesActivity() throws Exception{
JSONObject json = new JSONObject();
try {
SalesPosActivityFace salesPosActivityFace=this.getBeanByBeanClass(SalesPosActivityFace.class);
salesPosActivity = salesPosActivityFace.findById(salesPosActivityId);
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
json.put("id", salesPosActivity.getId());
json.put("name", salesPosActivity.getName());
json.put("rebate", salesPosActivity.getRebate());
json.put("startDate", sdf.format(salesPosActivity.getStartDate()));
json.put("endDate", sdf.format(salesPosActivity.getEndDate()));
json.put("validity", salesPosActivity.isValidity());
Set<PosStyle> styles = salesPosActivity.getPosStyles();
JSONArray array = new JSONArray();
for (PosStyle style : styles) {
JSONObject ps = new JSONObject();
ps.put("id", style.getId());
ps.put("name", style.getName());
ps.put("code", style.getCode());
array.add(ps);
}
json.put("seriesStyles", array);
} catch (Exception e) {
e.printStackTrace();
}
this.response.setContentType("text/html;charset=GBK");
this.response.getWriter().print(json);
}