EasyUI框架 使用Ajax提交注册信息的实现代码
一、服务器代码:
@Controller @Scope("prototype") public class StudentAction extends BaseAction<Student> { private static final long serialVersionUID = -2612140283476148779L; private Logger logger = Logger.getLogger(StudentAction.class); private String rows;// 每页显示的记录数 private String page;// 当前第几页 private Map<String, Object> josnMap = new HashMap<>(); // 查询出所有学生信息 public String list() throws Exception { return "list"; } public String regUI() throws Exception { return "regUI"; } // 查询出所有学生信息 public String listContent() throws Exception { List<Student> list = studentService.getStudentList(page, rows); System.out.println("list==" + list); toBeJson(list, studentService.getStudentTotal()); return "toJson"; } // 转化为Json格式 public void toBeJson(List<Student> list, int total) throws Exception { josnMap.put("total", total); josnMap.put("rows", list); JSONParser.writeJson(josnMap);// 自定义的工具类 } public String reg(){ logger.error("kkk"); try { studentService.save(model); josnMap.put("success", true); josnMap.put("msg", "注册成功!"); } catch (Exception e) { 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 (IOException e) { e.printStackTrace(); } return "toJson"; } public void setRows(String rows) { this.rows = rows; } public void setPage(String page) { this.page = page; } public Map<String, Object> getJosnMap() { return josnMap; } public void setJosnMap(Map<String, Object> josnMap) { this.josnMap = josnMap; } }
二、BaseAction代码:
import java.lang.reflect.ParameterizedType; import javax.annotation.Resource; import org.apache.struts2.ServletActionContext; import cn.oppo.oa.service.DepartmentService; import cn.oppo.oa.service.ForumService; import cn.oppo.oa.service.PrivilegeService; import cn.oppo.oa.service.RoleService; import cn.oppo.oa.service.StudentService; import cn.oppo.oa.service.UserService; import com.alibaba.fastjson.JSON; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; public abstract class BaseAction<T> extends ActionSupport implements ModelDriven<T> { /** * */ private static final long serialVersionUID = 1L; @Resource protected RoleService roleService; @Resource protected DepartmentService departmentService; @Resource protected UserService userService; @Resource protected PrivilegeService privilegeService; @Resource protected ForumService forumService; @Resource protected StudentService studentService; protected T model; @SuppressWarnings("unchecked") public BaseAction() { try { // 得到model的类型信息 ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass(); Class<T> clazz = (Class<T>) pt.getActualTypeArguments()[0]; // 通过反射生成model的实例 model = (T) clazz.newInstance(); } catch (Exception e) { throw new RuntimeException(e); } } public void writeJson(Object object){ try { String json = JSON.toJSONStringWithDateFormat(object, "yyyy-MM-dd HH: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 (Exception e) { e.printStackTrace(); } } public T getModel() { return model; } }
三、页面代码:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title>EasyUI框架</title> <%@ include file="/WEB-INF/jsp/public/common.jspf" %> <script type="text/javascript"> $(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('验证失败'); } }); </script> </head> <body> <div data-options="region:'north',split:true">aa</div> <!-- <div data-options="region:'south',split:true">bb</div>--> <div data-options="region:'east',title:'East',split:true">cc</div> <div data-options="region:'west',title:'West',split:true">dd</div> <div data-options="region:'center',title:'center title'">kk</div> <div data-options="title:'登陆', modal:true, closable:false, toolbar:[{ text:'Edit', iconCls:'icon-edit', handler:function(){alert('edit')} },{ text:'Help', iconCls:'icon-help', handler:function(){alert('help')} }], buttons:[{ text:'登陆', handler:function(){alert('登陆')} },{ text:'注册', handler:function(){ $('#easyui_regForm input').val(''); $('#easyui_regDialog').dialog('open'); } }]" > <table> <tr> <td>登陆名称:</td> <td><input type="text"/></td> </tr> <tr> <td>登陆密码:</td> <td><input type="password"/></td> </tr> </table> </div> <div data-options="title:'注册', modal:true, closable:true, closed:true, buttons:[{ text:'注册', handler:function(){ $('#easyui_regForm').form('submit',{ url : '${pageContext.request.contextPath}/student_reg.action', success : function(data) { var obj = jQuery.parseJSON(data); if (obj.success) { $('#easyui_regDialog').dialog('close'); } $.messager.show({ title : '提示', msg : obj.msg }); } }); } },{ text:'取消', handler:function(){alert('注册')} }]" > <form method="post"> <table> <tr> <td>登陆名称:</td> <td><input type="text" data-options="required:true,missingMessage:'用户名称不能为空'"/></td> </tr> <tr> <td>登陆密码:</td> <td><input type="password" data-options="required:true,missingMessage:'用户密码不能为空'"/></td> </tr> <tr> <td>确定密码:</td> <td><input type="password" data-options="required:true,missingMessage:'确认密码不能为空',validType:'equals[\'#reg_pwd\']'" /></td> </tr> </table> </form> </div> </body> </html>
四、struts2.xml配置