JAVAEE工程筆記6---miniui單表新增保存

HTMLjavascript

<!-- 標識頁面是查看(query)、修改(edit)、新增(add) -->	
<div id="addDiv" class="mini-window" style="width:650px; height:250px;" title: "新增業務地區">
     <input name="pageType" class="mini-hidden"/>
        <div id="dataform1" style="padding-top:10px;">
            <!-- hidden域 -->
            <input class="mini-hidden"/>
            <table style="width:100%; height:100%;" class="mini-form-table" id="addTable">
                <tr>
                	 <input class="mini-hidden" name="id" required="true" />
                    <td>
                        編號:           
                    </td>
                    <td colspan="1">
                        <input class="mini-textbox" name="code" required="true"/>
                    </td>
               
          
                    <td>
                        所屬類別名稱:
                    </td>
                    <td colspan="1">
                        <input class="mini-combobox"  name="typeId" required="true" url="/dictEntry.do?method=getDictEntryByDictTypeId&dictTypeId=YW_BOX_TYPE" textField="dictName" valueField="dictId" value ="1"  />
                    </td>
            </tr>
            <tr>
                    <td>
                        容積:
                    </td>
                    <td colspan="1">
                        <input class="mini-textbox" name="volume" required="true"/> m³
                    </td>
                    <td>
                        顏色:
                    </td>
                    <td colspan="1">
                        <input class="mini-combobox" value="1"  name="color1" required="true" url="/dictEntry.do?method=getDictEntryByDictTypeId&dictTypeId=YW_COLOR" textField="dictName" valueField="dictId"/>
                    </td>
                </tr>
                <tr>
                    <td>
                         備註:
                    </td>
                    <td colspan="6">
                        <textarea class="mini-textarea" name="remark" style="width:100%;"></textarea>
                    </td>
                 </tr>
                
            </table>
            <div class="mini-toolbar" style="padding:0px;" borderStyle="border:0;">
                <table width="100%">
                    <tr>
                        <td style="text-align:center;" colspan="4">
                            <a class="mini-button" iconCls="icon-save" onclick="onOk()">
                                保存
                            </a>
                            <span style="display:inline-block;width:25px;">
                            </span>
                            <a class="mini-button" iconCls="icon-cancel" onclick="onCancel()">
                                取消
                            </a>
                        </td>
                    </tr>
                </table>
            </div>
        </div>
</div>

JShtml

//保存  
function onOk(){
	saveData();
}

//新增和編輯保存
function saveData() {

    form.validate();
    if (form.isValid() == false) return;
     var data = form.getData();//獲取主表數據
     var jsonData = mini.encode(data);   //序列化成JSON 
	//根據id是否有值判斷是否爲新增的數據
     if(data.id){//編輯
    	 $.ajax({
 			type: "POST",
 			url: "/box.do?method=edit",
 			data: { submitData: jsonData,flag:"save" },
 			success:function (res){
 			 res=eval("("+res+")");
 			 if(res.success){
 				mini.alert("保存成功", "系統提示");
 				addDiv.hide();
 	            grid.reload();
 			 }else{
 				mini.alert("保存失敗", "系統提示");
 		     }
 			}
 		});	
     }else{//新增
		$.ajax({
			type: "POST",
			url: "/box.do?method=save",
			data: { submitData: jsonData,flag:"save" },
			success:function (res){
			 res=eval("("+res+")");
			 if(res.success){
				mini.alert("保存成功", "系統提示");
				addDiv.hide();
	            grid.reload();
			 }else{
				mini.alert("保存失敗", "系統提示");
		     }
			}
		});	
     }
}

controllerjava

/**
	 * @title 保存食品箱管理列表數據
	 * @author wangyu
	 * @data 2017-03-10
	 */
	@Override
	public ModelAndView save(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		User user = (User) request.getSession().getAttribute(
				Constants.KEY_LOGIN_USER);

		if (user == null) {
			// 返回到登錄界面
			return new ModelAndView("login.html");

		}
		
		String submitJSON = request.getParameter("submitData");// 新增數據
		String flag = request.getParameter("flag");
		IJsonObject data = JsonUtil.toJsonObject(submitJSON);// 主表數據

		String id = data.getString("id") != null ? data.getString("id")
				.toString() : "";
		String code = data.getString("code") != null ? data.getString(
				"code").toString() : "";
		String boxNumber = data.getString("boxNumber") != null ? data
				.getString("boxNumber").toString() : "";
		String typeId = data.getString("typeId") != null ? data.getString(
				"typeId").toString() : "";
		String volume = data.getString("volume") != null ? data.getString(
				"volume").toString() : "";
		String color1 = data.getString("color1") != null ? data.getString(
				"color1").toString() : "";
		String remark = data.getString("remark") != null ? data.getString(
				"remark").toString() : "";
		String status = "";
		
// 根據flag判斷是保存和提交
		if (flag.equalsIgnoreCase("save")) {
			status = "1";// 保存
		} else {// 提交
			status = "203";// 提交---審批中
			// todod 2017-03-07 流程不啓用,則不需審批,直接通
		}

		// 保存主表
		boolean isNew = StringUtils.isBlank(id) ? true : false; // 判斷id是否爲空
		Box box = null;
		if (isNew) {
			box = new Box();
			id = UUID.randomUUID().toString(); 
			box.setCreateTime(DateUtil.getDateTime());
			box.setCreateUser(user.getLoginName());
			box.setCreateUserId(user.getId().toString());

			box.setModifyTime(DateUtil.getDateTime());
			box.setModifyUser(user.getLoginName());
			box.setModifyUserId(user.getId().toString());

			box.setStatus(status);

		} else {
			box = boxManager.get(id);
			box.setModifyTime(DateUtil.getDateTime());
			box.setModifyUser(user.getLoginName());
			box.setModifyUserId(user.getId().toString());
		}
		
		box.setId(id);
		box.setCode(code);
		//box.setBoxNumber(boxNumber);
		box.setTypeId(typeId);

		box.setVolume(volume);
		box.setColor1(color1);
		box.setRemark(remark);

		boxManager.save(box);
		this.renderText(response, "{\"success\":true}");
		return null;
	}
相關文章
相關標籤/搜索