SSM+BJUI实现添加和编辑共用一个页面
場景
SSM+BJUI在對記錄進(jìn)行添加或者編輯時可以共用一個jsp頁面,然后再進(jìn)行保存。
實(shí)現(xiàn)
1.主頁面添加兩個按鈕,一個是添加按鈕一個是編輯按鈕。
<!-- 添加按鈕--><shiro:hasPermission name="partnerAdd"><a href="${ctx}/sys/cooperativePartnersManageAction/toAdd" class="btn btn-green" data-icon="plus" data-toggle="dialog" data-width="800" data-height="400" data-id="dialog-user" data-mask="true">添加</a></shiro:hasPermission><!-- 編輯按鈕--><shiro:hasPermission name="partnerEdit">< BR>?????<ahref="${ctx}/sys/cooperativePartnersManageAction/toEdit?id={#bjui-selected}" class="btn btn-blue" data-icon="edit" data-toggle="dialog" data-width="800" data-height="400" data-id="dialog-user" data-mask="true">編輯</a></shiro:hasPermission>三個頁面:
partnerMain就是當(dāng)前主頁面,帶有添加以及編輯的頁面。
partnerEditDlg是添加和編輯共用的頁面。
添加功能實(shí)現(xiàn)
1.現(xiàn)在點(diǎn)擊添加按鈕,來到對應(yīng)的action
@RequestMapping(value="/toAdd")public ModelAndView toAdd(){ModelAndView mv = null;try {//業(yè)務(wù)部分代碼省略//傳遞當(dāng)前的操作類型的標(biāo)識,左邊是op常量參數(shù),表示操作類型,右邊是add常量參數(shù),表示是添加操作mv.addObject(ModelAndViewConstants.OPERATION_KEY,ModelAndViewConstants.OPERATION_VALUE_ADD);//傳遞到共用頁面的標(biāo)題參數(shù),左邊是title常量參數(shù),右邊是要顯示的標(biāo)題的內(nèi)容mv.addObject(ModelAndViewConstants.TITLE_KEY, "添加合作商戶信息");//跳轉(zhuǎn)的具體的頁面mv.setViewName(ModelAndViewConstants.PARTNER_EDIT_VIEW);LogService.getInstance(this).error("進(jìn)入合作伙伴添加功能成功:" +ModelAndViewConstants.PARTNER_EDIT_VIEW);}catch(Exception ex) {LogService.getInstance(this).error("進(jìn)入合作伙伴添加失敗:" + ex.getMessage(), ex);mv = new ModelAndView("redirect:/error.jsp");}return mv;}2.通過上面的action可以傳遞要執(zhí)行的操作類型為add,并跳轉(zhuǎn)到共用的頁面。
?
主要功能點(diǎn);
在共享頁面添加隱藏input,來接受傳過來的op參數(shù),即操作類型,這里是add
因?yàn)槭翘砑铀詡鬟^來的實(shí)體類partner沒有ID等屬性,但是要在編輯要用到,所以這里也添加一個hidden。
這里只顯示一個字段,其他的省略。
3.點(diǎn)擊保存時,BJUI會提交當(dāng)前form,此form會提交到:${ctx}/sys/cooperativePartnersManageAction/doSave,然后執(zhí)行保存的操作。
@ResponseBody@RequestMapping(value="/doSave")public Map<String, Object> doSave(SysPartners entity, String op) {Map<String, Object> jsonResult = null;try {//正式業(yè)務(wù)部分開始//與配置權(quán)限管理中相對應(yīng),實(shí)現(xiàn)添加或編輯完實(shí)時更新String tabid = tabid(ModelAndViewConstants.PARTNER_SYS_ID);//如果是測試簽約if(entity.getContractStatus()==0) {entity.setTestContractTime(new Date());entity.setContractStatusName("測試簽約");}//如果是正式簽約if(entity.getContractStatus()==1) {entity.setFormalContractTime(new Date());entity.setContractStatusName("正式簽約");}entity.setRecordTime(new Date());//獲得當(dāng)前用戶的id和name,加入客戶對象中ShiroUser currentUser = (ShiroUser)SecurityUtils.getSubject().getPrincipal();entity.setModifyUserId(currentUser.getUserId());entity.setModifyUserName(currentUser.getAccount());//正式業(yè)務(wù)部分結(jié)束//如果op標(biāo)志參數(shù)為空,或者為add 那么就執(zhí)行添加操作if (null == op || ModelAndViewConstants.OPERATION_VALUE_ADD.equals(op)) {//此字段表示數(shù)據(jù)未被更改entity.setStatus("0");//執(zhí)行插入數(shù)據(jù)操作,方法是有代碼生成工具自動生成int count = this.sysPartnersService.insert(entity);if (count > 0) {LogService.getInstance(this).debug("添加客戶信息成功!");}}else {//如果不是,則就是編輯后的保存操作//獲取現(xiàn)存數(shù)據(jù),也是通過代碼生成工具生成的方法SysPartners currentSysPartner = this.sysPartnersService.getByPrimaryKey(entity);int count = this.sysPartnersService.updateByPrimaryKey(entity);if (count > 0) {LogService.getInstance(this).debug("編輯合作商戶信息成功!");}}Integer statusCode = 200;String msg = "合作商戶信息保存成功";jsonResult = JsonResult.jsonReturn(statusCode, msg, tabid);}catch(Exception ex) {LogService.getInstance(this).error("保存合作商戶信息失敗:" + ex.getMessage(), ex);String msg =? "保存合作商戶信息失敗:" + ex.getMessage();jsonResult = JsonResult.jsonReturnErr(msg);}return jsonResult;}編輯功能實(shí)現(xiàn)
1.現(xiàn)在點(diǎn)擊主頁面的編輯按鈕
<!-- 編輯按鈕--><shiro:hasPermission name="customerEdit"><a href="${ctx}/sys/sysCustomerAction/toEdit?id={#bjui-selected}" class="btn btn-blue" data-icon="edit" data-toggle="dialog" data-width="800" data-height="400" data-id="dialog-user" data-mask="true">編輯</a></shiro:hasPermission>2.點(diǎn)擊編輯按鈕會跳到后臺action,并將當(dāng)前BJUI集成的能獲取選中項(xiàng)的ID傳過去。
現(xiàn)在來到后臺的toEdit
@RequestMapping(value="/toEdit")public ModelAndView toEdit(Integer id) {ModelAndView mv = null;try {//根據(jù)ID先查詢要編輯的數(shù)據(jù)SysPartners partner = this.sysPartnersService.getByPrimaryKey(id);//業(yè)務(wù)部分代碼開始//獲取簽約狀態(tài),此處是通過關(guān)聯(lián)碼表來實(shí)現(xiàn)Map<String, Object> param = new HashMap<String, Object>();//0 表示正常數(shù)據(jù)param.put("status", "0");param.put("codeType", "contractStatus");//根據(jù)參數(shù)將相關(guān)碼表內(nèi)容查詢出來List<SysCode> contractStatus = codeService.getByParam(param);mv = new ModelAndView();//傳遞簽約狀態(tài)mv.addObject("contractStatus", contractStatus);//業(yè)務(wù)部分代碼結(jié)束//傳遞操作類型,這里是編輯操作mv.addObject(ModelAndViewConstants.OPERATION_KEY,ModelAndViewConstants.OPERATION_VALUE_EDIT);//jsp頁面要顯示的標(biāo)題titlemv.addObject(ModelAndViewConstants.TITLE_KEY, "修改合作商戶信息");//將查詢到的實(shí)體Model類傳遞mv.addObject(ModelAndViewConstants.PARTMER, partner);//跳轉(zhuǎn)到編輯界面mv.setViewName(ModelAndViewConstants.PARTNER_EDIT_VIEW);}catch(Exception ex) {LogService.getInstance(this).error("進(jìn)入客戶編輯失敗:" + ex.getMessage(), ex);mv = new ModelAndView(ModelAndViewConstants.ERROR_VIEW);}return mv;}關(guān)鍵點(diǎn):
通過傳過來的ID獲取要編輯的Model實(shí)體類的數(shù)據(jù),然后執(zhí)行相應(yīng)的業(yè)務(wù),
關(guān)鍵是通過:
mv.addObject(ModelAndViewConstants.OPERATION_KEY,ModelAndViewConstants.OPERATION_VALUE_EDIT);
來傳遞操作類型,左邊是常量參數(shù)op,右邊是常量參數(shù)edit。
然后傳遞要顯示的標(biāo)題,傳遞獲取的model數(shù)據(jù),跳轉(zhuǎn)到共用的編輯頁面。
3.剩余操作與添加功能一樣,不過是編輯時將數(shù)據(jù)傳遞過去,通過input的value來賦值,如果是添加時value為空所以不顯示,如果是編輯則會將傳遞的數(shù)據(jù)進(jìn)行顯示,添加與編輯都是執(zhí)行同一個save方法。
?
?
?
總結(jié)
以上是生活随笔為你收集整理的SSM+BJUI实现添加和编辑共用一个页面的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SSM实现根据后台控制禁用select
- 下一篇: Vux+Cordova打包的安卓App实