前后台数据交换,printwriter、jsonobject、jsonarray、ajax请求,数据交换

时间:2022-03-17 17:30:09

  后台代码:

public void findByIDEquipment() {
getResponse().setCharacterEncoding("UTF-8");
getResponse().setContentType("text/html;charset=UTF-8");
PrintWriter out;
JSONObject obj = new JSONObject();
JSONArray jarr = new JSONArray();
ArrayList<Equipment> eq_array_list = new ArrayList<Equipment>();
SimpleDateFormat sdf = new SimpleDateFormat();
try {
out = getResponse().getWriter(); System.out.println(eq_id); long id = Long.valueOf(getRequest().getParameter("eq_id"));
System.out.println("id is" + id);
Equipment eq = eq_service.findEquipmentById(id);
Equipment eq1 = eq;
eq_array_list.add(eq);
if (eq != null) {
/*
* getSession().setAttribute("eq_name", eq.getName());
* getSession().setAttribute("eq_comment", eq.getComment());
* getSession().setAttribute("eq_pn", eq.getPn());
* getSession().setAttribute("eq_sn", eq.getSn());
* getSession().setAttribute("eq_inDate", eq.getInDate());
* getSession().setAttribute("eq_alarmDay",
* eq.getAlarmTimeDay());
* getSession().setAttribute("eq_overTime", eq.getOverTime());
* getSession().setAttribute("eq_username",
* eq.getEq_user().getUsername());
*/
obj.put("eq1", eq1);
obj.put("indate", sdf.format(eq1.getInDate()));
obj.put("overdate", sdf.format(eq1.getOverTime()));
jarr.add(obj); out.print(jarr);
out.flush(); } else {
out.println(false);
} } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }

前台代码:

/* 详情动作 */
$('.action_div')
.click(
function() {
var click_num = $('.action_div').index(this)
console.log(click_num)
var eq_id = $('.action_div').eq(click_num).attr('org')
console.log("select item id is" + eq_id) $.ajax({
type : "POST",
url : "findByIDEquipmentAction?fresh="
+ Math.random(),
data : {
"eq_id" : eq_id
},
cache : false,
async : true,
dataType : "json",
success : function(res) {
console.log(res)
console.log(res[0].eq1.name) if (res) { $('.getname').text(res[0].eq1.name)
$('.getcomment').text(res[0].eq1.comment)
$('.getpn').text(res[0].eq1.pn)
$('.getsn').text(res[0].eq1.sn)
$('.getindate').text(
res[0].indate.toString().substring(
0, 8))
$('.getalarmday').text(
res[0].eq1.alarmTimeDay)
$('.getoverdate').text(
res[0].overdate.toString()
.substring(0, 8))
$('.getusername').text(
res[0].eq1.eq_user.username) } else {
alert("请求异常1");
} },
error : function(json) {
alert("请求异常2");
}
}) $('#mask2')
.css(
{
"opacity" : "1",
"z-index" : "100",
'-webkit-transition' : 'opacity 2s ease-in,z-index 1s ease-in',
'-moz-transition' : 'opacity 2s ease-in,z-index 1s ease-in',
'-ms-transition' : 'opacity 2s ease-in,z-index 1s ease-in',
'-o-transition' : 'opacity 2s ease-in,z-index 1s ease-in',
'transition' : 'opacity 2s ease-in,z-index 1s ease-in' }) })

  

public void findByIDEquipment() {getResponse().setCharacterEncoding("UTF-8");getResponse().setContentType("text/html;charset=UTF-8");PrintWriter out;JSONObject obj = new JSONObject();JSONArray jarr = new JSONArray();ArrayList<Equipment> eq_array_list = new ArrayList<Equipment>();SimpleDateFormat sdf = new SimpleDateFormat();try {out = getResponse().getWriter();
System.out.println(eq_id);
long id = Long.valueOf(getRequest().getParameter("eq_id"));System.out.println("id is" + id);Equipment eq = eq_service.findEquipmentById(id);Equipment eq1 = eq;eq_array_list.add(eq);if (eq != null) {/* * getSession().setAttribute("eq_name", eq.getName()); * getSession().setAttribute("eq_comment", eq.getComment()); * getSession().setAttribute("eq_pn", eq.getPn()); * getSession().setAttribute("eq_sn", eq.getSn()); * getSession().setAttribute("eq_inDate", eq.getInDate()); * getSession().setAttribute("eq_alarmDay", * eq.getAlarmTimeDay()); * getSession().setAttribute("eq_overTime", eq.getOverTime()); * getSession().setAttribute("eq_username", * eq.getEq_user().getUsername()); */obj.put("eq1", eq1);obj.put("indate", sdf.format(eq1.getInDate()));obj.put("overdate", sdf.format(eq1.getOverTime()));jarr.add(obj);
out.print(jarr);out.flush();
} else {out.println(false);}
} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}
}