EasyUI框架 使用Ajax提交注册信息的实现代码
EasyUI框架使用Ajax提交注册信息的实现代码
一、服务器代码:
@Controller @Scope("prototype") publicclassStudentActionextendsBaseAction{ privatestaticfinallongserialVersionUID=-2612140283476148779L; privateLoggerlogger=Logger.getLogger(StudentAction.class); privateStringrows;//每页显示的记录数 privateStringpage;//当前第几页 privateMap josnMap=newHashMap<>(); //查询出所有学生信息 publicStringlist()throwsException{ return"list"; } publicStringregUI()throwsException{ return"regUI"; } //查询出所有学生信息 publicStringlistContent()throwsException{ List list=studentService.getStudentList(page,rows); System.out.println("list=="+list); toBeJson(list,studentService.getStudentTotal()); return"toJson"; } //转化为Json格式 publicvoidtoBeJson(List list,inttotal)throwsException{ josnMap.put("total",total); josnMap.put("rows",list); JSONParser.writeJson(josnMap);//自定义的工具类 } publicStringreg(){ logger.error("kkk"); try{ studentService.save(model); josnMap.put("success",true); josnMap.put("msg","注册成功!"); }catch(Exceptione){ e.printStackTrace(); josnMap.put("success",false); josnMap.put("msg","注册失败!"); } try{ ServletActionContext.getResponse().setContentType("text/html;charset=utf-8"); ServletActionContext.getResponse().setCharacterEncoding("utf-8"); ServletActionContext.getResponse().getWriter().print(JSON.toJSONString(josnMap)); }catch(IOExceptione){ e.printStackTrace(); } return"toJson"; } publicvoidsetRows(Stringrows){ this.rows=rows; } publicvoidsetPage(Stringpage){ this.page=page; } publicMap getJosnMap(){ returnjosnMap; } publicvoidsetJosnMap(Map josnMap){ this.josnMap=josnMap; } }
二、BaseAction代码:
importjava.lang.reflect.ParameterizedType; importjavax.annotation.Resource; importorg.apache.struts2.ServletActionContext; importcn.oppo.oa.service.DepartmentService; importcn.oppo.oa.service.ForumService; importcn.oppo.oa.service.PrivilegeService; importcn.oppo.oa.service.RoleService; importcn.oppo.oa.service.StudentService; importcn.oppo.oa.service.UserService; importcom.alibaba.fastjson.JSON; importcom.opensymphony.xwork2.ActionSupport; importcom.opensymphony.xwork2.ModelDriven; publicabstractclassBaseActionextendsActionSupportimplementsModelDriven { /** * */ privatestaticfinallongserialVersionUID=1L; @Resource protectedRoleServiceroleService; @Resource protectedDepartmentServicedepartmentService; @Resource protectedUserServiceuserService; @Resource protectedPrivilegeServiceprivilegeService; @Resource protectedForumServiceforumService; @Resource protectedStudentServicestudentService; protectedTmodel; @SuppressWarnings("unchecked") publicBaseAction(){ try{ //得到model的类型信息 ParameterizedTypept=(ParameterizedType)this.getClass().getGenericSuperclass(); Class clazz=(Class )pt.getActualTypeArguments()[0]; //通过反射生成model的实例 model=(T)clazz.newInstance(); }catch(Exceptione){ thrownewRuntimeException(e); } } publicvoidwriteJson(Objectobject){ try{ Stringjson=JSON.toJSONStringWithDateFormat(object,"yyyy-MM-ddHH:mm:ss"); ServletActionContext.getResponse().setContentType("text/html;charset=utf-8"); ServletActionContext.getResponse().setCharacterEncoding("utf-8"); ServletActionContext.getResponse().getWriter().write(json); ServletActionContext.getResponse().getWriter().flush(); ServletActionContext.getResponse().getWriter().close(); }catch(Exceptione){ e.printStackTrace(); } } publicTgetModel(){ returnmodel; } }
三、页面代码:
<%@pagelanguage="java"import="java.util.*"pageEncoding="utf-8"%>EasyUI框架 <%@includefile="/WEB-INF/jsp/public/common.jspf"%>$(function(){ if(${"#easyui_regForm"}.form('validate')){ $.ajax({ url:'${pageContext.request.contextPath}/student_reg.action', data:${"#easyui_regForm"}.serialize(), dataType:'json', success:function(obj,status,jqXHR){ if(obj.success){ $("#easyui_regDialog").dialog('close'); } $.message.show({ title:'提示', msg:obj.msg }); } }); }else{ alert('验证失败'); } }); aa