本产品中调用存储过程,前端属性绑定到只需要知道Model对象的属性名称。引用时直接使用属性名会导致程序中出现很多harcode代码。可以为每个Model类定义一个ModelField类,ModelField类定义了一些返回Model类属性名称的方法,这样可以防止hardcode代码,对以后的维护也很有帮助。本文将介绍如何定义和使用ModelField,包括”定义所有ModelField类的父类“、”具体的ModelField继承了BaseModelField“、”使用ModelField“。
1、定义所有ModelField类的父类。
BaseModelField是所有ModelField类的父类,定义了一些获取公共属性的方法:
查看代码package com.bx.erp.model;
public class BaseModelField {
protected String FIELD_NAME_ID;
public String getFIELD_NAME_ID() {
return "ID";
}
protected String FIELD_NAME_listSlave1;
public String getFIELD_NAME_listSlave1() {
return "listSlave1";
}
protected String FIELD_NAME_listSlave2;
public String getFIELD_NAME_listSlave2() {
return "listSlave2";
}
protected String FIELD_NAME_listSlave3;
public String getFIELD_NAME_listSlave3() {
return "listSlave3";
}
protected String FIELD_NAME_createDatetime;
public String getFIELD_NAME_createDatetime() {
return "createDatetime";
}
protected String FIELD_NAME_updateDatetime;
public String getFIELD_NAME_updateDatetime() {
return "updateDatetime";
}
protected String FIELD_NAME_syncDatetime;
public String getFIELD_NAME_syncDatetime() {
return "syncDatetime";
}
protected String FIELD_NAME_syncType;
public String FIELD_NAME_syncType() {
return "syncType";
}
protected String FIELD_NAME_iOrderBy;
public String getFIELD_NAME_iOrderBy() {
return "iOrderBy";
}
protected String FIELD_NAME_isASC;
public String getFIELD_NAME_isASC() {
return "isASC";
}
protected String FIELD_NAME_ErrorMessage;
public String getFIELD_NAME_ErrorMessage() {
return "errorMessage";
}
protected String FIELD_NAME_pageIndex;
public String getFIELD_NAME_pageIndex() {
return "pageIndex";
}
protected String FIELD_NAME_pageSize;
public String getFIELD_NAME_pageSize() {
return "pageSize";
}
protected String FIELD_NAME_returnObject;
public String getFIELD_NAME_returnObject() {
return "returnObject";
}
protected String FIELD_NAME_queryKeyword;
public String getFIELD_NAME_queryKeyword() {
return "queryKeyword";
}
protected String FIELD_NAME_fieldToCheckUnique;
public String getFIELD_NAME_fieldToCheckUnique() {
return "fieldToCheckUnique";
}
protected String FIELD_NAME_date1;
public String getFIELD_NAME_date1() {
return "date1";
}
protected String FIELD_NAME_date2;
public String getFIELD_NAME_date2() {
return "date2";
}
protected String FIELD_NAME_iPageIndex;
public String getFIELD_NAME_iPageIndex() {
return "iPageIndex";
}
protected String FIELD_NAME_iPageSize;
public String getFIELD_NAME_iPageSize() {
return "iPageSize";
}
protected String FIELD_NAME_uniqueField;
public String getFIELD_NAME_uniqueField() {
return "uniqueField";
}
protected String FIELD_NAME_operatorStaffID;
public String getFIELD_NAME_operatorStaffID() {
return "operatorStaffID";
}
public String FIELD_NAME_returnSalt;
public String getFIELD_NAME_returnSalt() {
return "returnSalt";
}
protected String FIELD_NAME_posID;
public String getFIELD_NAME_posID() {
return "posID";
}
protected String FIELD_NAME_syncSequence;
public String getFIELD_NAME_syncSequence() {
return "syncSequence";
}
protected String FIELD_NAME_memberCardID;
public String getFIELD_NAME_memberCardID() {
return "memberCardID";
}
}
2、具体的ModelField继承了BaseModelField。
例如:StaffField:
查看代码public class StaffField extends BaseAuthenticationModelField {
protected String FIELD_NAME_involvedResigned;
public String getFIELD_NAME_involvedResigned() {
return "involvedResigned";
}
protected String FIELD_NAME_forModifyPassword;
public String getFIELD_NAME_forModifyPassword() {
return "forModifyPassword";
}
protected String FIELD_NAME_createNewStaff;
public String getFIELD_NAME_createNewStaff() {
return "createNewStaff";
}
protected String FIELD_NAME_isLoginFromPos;
public String getFIELD_NAME_isLoginFromPos() {
return "isLoginFromPos";
}
protected String FIELD_NAME_roleID;
public String getFIELD_NAME_roleID() {
return "roleID";
}
protected String FIELD_NAME_confirmNewPassword;
public String getFIELD_NAME_confirmNewPassword() {
return "confirmNewPassword";
}
protected String FIELD_NAME_confirmResetNewPassword;
public String getFIELD_NAME_confirmResetNewPassword() {
return "confirmResetNewPassword";
}
protected String FIELD_NAME_resetNewPassword;
public String getFIELD_NAME_resetNewPassword() {
return "resetNewPassword";
}
protected String FIELD_NAME_phone;
public String getFIELD_NAME_phone() {
return "phone";
}
protected String FIELD_NAME_name;
public String getFIELD_NAME_name() {
return "name";
}
protected String FIELD_NAME_weChat;
public String getFIELD_NAME_weChat() {
return "weChat";
}
protected String FIELD_NAME_openid;// 用户关注公众号的唯一标识
public String getFIELD_NAME_openid() {
return "openid";
}
protected String FIELD_NAME_unionid;// 只有将公众号绑定到微信开放平台帐号后,才会出现该字段。
public String getFIELD_NAME_unionid() {
return "unionid";
}
protected String FIELD_NAME_ICID;
public String getFIELD_NAME_ICID() {
return "ICID";
}
protected String FIELD_NAME_passwordExpireDate;
public String getFIELD_NAME_passwordExpireDate() {
return "passwordExpireDate";
}
protected String FIELD_NAME_isFirstTimeLogin;
public String getFIELD_NAME_isFirstTimeLogin() {
return "isFirstTimeLogin";
}
protected String FIELD_NAME_shopID;
public String getFIELD_NAME_shopID() {
return "shopID";
}
protected String FIELD_NAME_departmentID;
public String getFIELD_NAME_departmentID() {
return "departmentID";
}
protected String FIELD_NAME_status;
public String getFIELD_NAME_status() {
return "status";
}
protected String FIELD_NAME_oldPassword;
public String getFIELD_NAME_oldPassword() {
return "oldPassword";
}
protected String FIELD_NAME_newPassword;
public String getFIELD_NAME_newPassword() {
return "newPassword";
}
protected String FIELD_NAME_oldMD5;
public String getFIELD_NAME_oldMD5() {
return "oldMD5";
}
protected String FIELD_NAME_newMD5;
public String getFIELD_NAME_newMD5() {
return "newMD5";
}
protected String FIELD_NAME_operator;
public String getFIELD_NAME_operator() {
return "operator";
}
}
3、使用ModelField。
在Model类创建ModelField对象:
public class Staff extends BaseAuthenticationModel {
private static final long serialVersionUID = -4511056097709074881L;
public static final StaffField field = new StaffField();
调用ModelField对象的方法:
@Override
public Map<String, Object> getCreateParam(int iUseCaseID, final BaseModel bm) {
checkParameterInput(bm);
Map<String, Object> params = new HashMap<String, Object>();
Staff s = (Staff) bm;
switch (iUseCaseID) {
case BaseBO.INVALID_CASE_ID:
case BaseBO.CASE_BxStaffCreateShopAndPreSaleStaffAndBossAccount:
params.put(field.getFIELD_NAME_phone(), s.getPhone() == null ? "" : s.getPhone());
params.put(field.getFIELD_NAME_name(), s.getName() == null ? "" : s.getName());
params.put(field.getFIELD_NAME_ICID(), s.getICID() == "" ? null : s.getICID());
params.put(field.getFIELD_NAME_weChat(), s.getWeChat() == "" ? null : s.getWeChat());
params.put(field.getFIELD_NAME_salt(), s.getSalt() == null ? "" : s.getSalt());
params.put(field.getFIELD_NAME_passwordExpireDate(), s.getPasswordExpireDate());
params.put(field.getFIELD_NAME_isFirstTimeLogin(), s.getIsFirstTimeLogin());
params.put(field.getFIELD_NAME_shopID(), s.getShopID());
params.put(field.getFIELD_NAME_departmentID(), s.getDepartmentID());
params.put(field.getFIELD_NAME_roleID(), s.getRoleID());
params.put(field.getFIELD_NAME_status(), s.getStatus());
params.put(field.getFIELD_NAME_returnSalt(), s.getReturnSalt());
return params;
default:
throw new RuntimeException("未定义的CASE!");
}
}
访问数据库,调用存储过程需要传递hashmap存储参数,hashmap的key使用了field.getFIELD_NAME_phone()方法而不是用"phone",避免了harcode。后面如果修改phone属性名为mobile属性名,那么只需要修改getFIELD_NAME_phone()方法就可以了。项目里可能还有很多地方使用到phone属性名,如果一开始使用"phone",维护起来就麻烦多了。
在前端的name标签也可以使用到获取属性名的方法。
后端传ModelField对象给前端:
@RequestMapping(method = RequestMethod.GET)
public ModelAndView index(@ModelAttribute("SpringWeb") Staff staff, ModelMap mm, HttpSession session) {
if (!canCallCurrentAction(session, BaseAction.EnumUserScope.STAFF.getIndex())) {
logger.debug("无权访问本Action");
return null;
}
logger.info("进入staff的页面时加载相关数据,staff=" + staff);
Company company = getCompanyFromSession(session);
Map<String, Object> load = load(company.getDbName(), session);
mm.put("shopList", load.get("shopList"));
mm.put("departmentList", load.get("departmentList"));
mm.put("roleList", load.get("roleList"));
mm.put("StaffField", new StaffField());
mm.put(BaseAction.JSON_ERROR_KEY, load.get(BaseAction.JSON_ERROR_KEY));
return new ModelAndView(STAFF_Index, "", new Staff());
}
前端使用${StaffField.FIELD_NAME_phone}获取属性名称phone:
<div class="layui-inline">
<label class="layui-form-label"><strong class="requiredField">*</strong>手机号</label>
<div class="layui-input-inline">
<input type="text" class="layui-input staffPhone" name="${StaffField.FIELD_NAME_phone}" oninput="checkUniqueField(this)" onchange="check_ifDataChange(this,this.value);" autocomplete="off" lay-verify="required|phone" maxlength="11"/>
</div>
</div>