Ext中实现EditorGridPanel的增删改操作(2)
public ActionForward showStudent(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
try {
//设置编码
response.setContentType("text/html;charset=gbk");
PrintWriter out=response.getWriter();
//获取分页的pageIndex和pageSize的值
int pageIndex=Integer.parseInt(request.getParameter("start").toString());
int pageSize=Integer.parseInt(request.getParameter("limit").toString());
//获取总的记录条数
int pageCount=this.iservice.getCountByHQL("from Student");
Student stu=null;
//获取分页后的所有的学生的信息
List<Student> list=this.iservice.showByDetachedCriteria(DetachedCriteria.forClass(Student.class), pageIndex,pageSize);
String json="{totalProperty:"+pageCount+",root:[";
for (int i = 0; i < list.size(); i++) {
stu=(Student)list.get(i);
json+="{stuid:"+stu.getStuid()+",stuname:'"+stu.getStuname()+"',stuage:"+stu.getStuage()+",gradename:'"+stu.getGrade().getGradename()+"',teacher:'"+stu.getGrade().getTeacher()+"',address:'"+stu.getAddress()+"'},";
}
//截取json
json=json.substring(0,json.length()-1);
json+="]}";
out.write(json);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public ActionForward addStudent(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
StudentForm studentForm = (StudentForm) form;
try {
response.setCharacterEncoding("gbk");
PrintWriter out=response.getWriter();
this.iservice.save(studentForm.getStu());
out.write("{success:true,msg:'添加学生信息成功!'}");
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
//批量修改学生的信息
public ActionForward updateStudent(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
try {
response.setCharacterEncoding("gbk");
PrintWriter out=response.getWriter();
String json=request.getParameter("stuJson").toString();
json="{'data':"+json+"}";
//将json中的值遍历出来
//格式为"{"data":[{"id":"1"},{"id:"2"}]},
JSONObject jsonObject=new JSONObject(json);
//获得json数组
JSONArray jsonArray=jsonObject.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jo=jsonArray.getJSONObject(i);
//获取班级的编号
Long stuid=Long.valueOf(jo.get("stuid").toString());
Student stu=(Student)this.iservice.get(Student.class,stuid);
//获取其他的值
String stuname=jo.getString("stuname").toString();
Long stuage=jo.getLong("stuage");
String gradename=jo.getString("gradename");
String teacher=jo.getString("teacher");
String address=jo.getString("address");
//将值添加设置给stu
stu.setStuname(stuname);
stu.setStuage(stuage);
stu.setAddress(address);
stu.getGrade().setGradename(gradename);
stu.getGrade().setTeacher(teacher);
//调用修改的方法
this.iservice.update(stu);
}
out.write("修改学生信息成功!");
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//右键菜单中修改学生的信息
public ActionForward updateOneStudent(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
PrintWriter out=null;
try {
response.setContentType("text/html;charset=gbk");
out=response.getWriter();
//获取传递过来的json字符串数组===============>此数组返回的是修改后的字段值
String modified=request.getParameter("array");
String modifieStr[]=modified.split("\\|\\|");
for (int i = 0; i <modifieStr.length; i++) {
String json="{";//保存构造后的json
//在将字符串分隔
String modifiedS[]=modifieStr[i].split("=>");
modifiedS[1]=modifiedS[1].substring(1);
json+=modifiedS[0]+","+modifiedS[1];
//将构造的json字符串转换成JSONObject对象
JSONObject jso=new JSONObject(json);
//获取班级中的信息
Long stuId=jso.getLong("stuid");
//获取学生对象
Student stu=(Student)this.iservice.get(Student.class,stuId);
//判断jso中是否有该键值
if(jso.has("stuname")){
stu.setStuname(jso.getString("stuname"));
}
if(jso.has("stuage")){
stu.setStuage(jso.getLong("stuage"));
}
if(jso.has("gradename")){
stu.getGrade().setGradename(jso.getString("gradename"));
}
if(jso.has("teacher")){
stu.getGrade().setTeacher(jso.getString("teacher"));
}
if(jso.has("address")){
stu.setAddress(jso.getString("address"));
}
//修改学生的信息
this.iservice.update(stu);
}
out.write("{msg:true}");
} catch (Exception e) {
out.write("{msg:false}");
e.printStackTrace();
}finally{
out.flush();
out.close();
}
return null;
}
public ActionForward delStudent(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
Long id=Long.valueOf(request.getParameter("id").toString());
Student stu=(Student)this.iservice.get(Student.class, id);
this.iservice.delete(stu);
return null;
}