EasyUI框架 使用Ajax提交注冊信息的實現(xiàn)代碼
EasyUI框架 使用Ajax提交注冊信息的實現(xiàn)代碼
一、服務(wù)器代碼:
@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;// 每頁顯示的記錄數(shù) private String page;// 當(dāng)前第幾頁 private Map<String, Object> josnMap = new HashMap<>(); // 查詢出所有學(xué)生信息 public String list() throws Exception { return "list"; } public String regUI() throws Exception { return "regUI"; } // 查詢出所有學(xué)生信息 public String listContent() throws Exception { List<Student> list = studentService.getStudentList(page, rows); System.out.println("list==" + list); toBeJson(list, studentService.getStudentTotal()); return "toJson"; } // 轉(zhuǎn)化為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 class="easyui-layout"> <div data-options="region:'north',split:true" style="height:100px;">aa</div> <!-- <div data-options="region:'south',split:true" style="height:100px;">bb</div>--> <div data-options="region:'east',title:'East',split:true" style="width:200px;">cc</div> <div data-options="region:'west',title:'West',split:true" style="width:200px;">dd</div> <div data-options="region:'center',title:'center title'" style="padding:5px;background:#eee;">kk</div> <div class="easyui-dialog" 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" name="name"/></td> </tr> <tr> <td>登陸密碼:</td> <td><input type="password" name="password"/></td> </tr> </table> </div> <div id="easyui_regDialog" class="easyui-dialog" 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 id="easyui_regForm" method="post"> <table> <tr> <td>登陸名稱:</td> <td><input type="text" name="loginName" class="easyui-validatebox" data-options="required:true,missingMessage:'用戶名稱不能為空'"/></td> </tr> <tr> <td>登陸密碼:</td> <td><input id="reg_pwd" type="password" name="password" class="easyui-validatebox" data-options="required:true,missingMessage:'用戶密碼不能為空'"/></td> </tr> <tr> <td>確定密碼:</td> <td><input type="password" name="repassword" class="easyui-validatebox" data-options="required:true,missingMessage:'確認(rèn)密碼不能為空',validType:'equals[\'#reg_pwd\']'" /></td> </tr> </table> </form> </div> </body> </html>
四、struts2.xml配置
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!-- 配置為開發(fā)模式 --> <constant name="struts.devMode" value="true" /> <!-- 配置擴展名為action --> <constant name="struts.action.extension" value="action" /> <!-- 配置主題 --> <constant name="struts.ui.theme" value="simple" /> <package name="default" namespace="/" extends="json-default"> <interceptors> <!-- 聲明一個攔截器 --> <interceptor name="checkePrivilege" class="cn.oppo.oa.interceptor.CheckPrivilegeInterceptor"></interceptor> <!-- 重新定義defaultStack攔截器棧,需要先判斷權(quán)限 --> <interceptor-stack name="defaultStack"> <interceptor-ref name="checkePrivilege" /> <interceptor-ref name="defaultStack" /> </interceptor-stack> </interceptors> <!-- 配置全局的Result --> <global-results> <result name="loginUI">/WEB-INF/jsp/user/loginUI.jsp</result> <result name="noPrivilegeError">/noPrivilegeError.jsp</result> </global-results> <!-- 測試用的action,當(dāng)與Spring整合后,class屬性寫的就是Spring中bean的名稱 --> <action name="test" class="testAction"> <result name="success">/test.jsp</result> </action> <action name="*_*" class="{1}Action" method="{2}"> <result name="{2}">/WEB-INF/jsp/{1}/{2}.jsp</result> <!-- 跳轉(zhuǎn)到添加與修改頁面 --> <result name="saveUI">/WEB-INF/jsp/{1}/saveUI.jsp</result> <!-- 返回list頁 --> <result name="toList" type="redirectAction">{1}_list?parentId=${parentId}</result> <!-- 返回主頁 --> <result name="toIndex" type="redirect">/index.jsp</result> <!-- 返回論壇主題 --> <result name="toShow" type="redirectAction">topic_show?id=${id}</result> <result name="toTopicShow" type="redirectAction">topic_show?id=${topicId}</result> <!-- json解析 --> <result name="toJson" type="json"> <param name="root">josnMap</param> </result> <result name="reg">/easyui.jsp</result> </action> </package> </struts>
如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
jQuery中需要注意的細(xì)節(jié)問題小結(jié)
jQuery中需要注意的細(xì)節(jié)問題小結(jié),使用jquery的朋友可以參考下。2011-12-12基于JQuery和CSS3實現(xiàn)仿Apple TV海報背景視覺差特效源碼分享
這是一款效果非常炫酷的jQuery和CSS3仿Apple TV海報背景視覺差特效。該視覺差特效在使用鼠標(biāo)在屏幕上下左右移動的時候,海報中的各種元素以不同的速度運動,形成視覺差效果,并且還帶有一些流光特效。2015-09-09jQuery動態(tài)加載css文件實現(xiàn)方法
使用jQuery來加載一個外部的 css 文件,首先創(chuàng)建一個 link 元素,并將它添加到 標(biāo)記中即可。那么基于jquery代碼如何實現(xiàn)呢?下面小編給大家介紹jQuery動態(tài)加載css文件實現(xiàn)方法,需要的朋友參考下吧2016-06-06jquery ui dialog實現(xiàn)彈窗特效的思路及代碼
這篇文章介紹了jquery ui dialog實現(xiàn)彈窗特效的思路及代碼,有需要的朋友可以參考一下2013-08-08jQuery實現(xiàn)簡單的下拉菜單導(dǎo)航功能示例
這篇文章主要介紹了jQuery實現(xiàn)簡單的下拉菜單導(dǎo)航功能,涉及jQuery針對頁面元素的遍歷與節(jié)點修改相關(guān)操作技巧,需要的朋友可以參考下2017-12-12jQuery實現(xiàn)帶滾動線條導(dǎo)航效果的方法
這篇文章主要介紹了jQuery實現(xiàn)帶滾動線條導(dǎo)航效果的方法,分析了帶滾動條導(dǎo)航效果的實現(xiàn)原理與相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-01-01關(guān)于jquery中動態(tài)增加select,事件無效的快速解決方法
下面小編就為大家?guī)硪黄P(guān)于jquery中動態(tài)增加select,事件無效的快速解決方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-08-08