问题描述
Hello,各路大俠,小弟最近做一個JavaWeb應用項目。请教如下一个Action代码中哪些是HardCode,作为程序员有什么比较好的方法避免这些HardCode?例如具体需求如下:用户将查询到的有效记录,选中其中部分或者全部记录,将之变成无效;查询无效记录,选择其中部分或者全部,将之变成有效。备注:activeFlag=1表示Active,activeFlag=0表示Inactive。publicvoidexecStatusUpdate(ModelAndViewmav,FormBeanform,HttpServletRequestrequest,HttpServletResponseresponse)throwsActionExecutionException{ApplyVisitFormBeanactionForm=(ApplyVisitFormBean)form;DynamicModelmodel=actionForm.buildVisitSearchModel(request);//這個是從頁面得到Form表單信息Stringchangeflag="1".equals(model.getParam("activeFlag"))?"0":"1";//activeFlag是Form表單的一個欄位booleanupdateflag=false;String[]checkappno=request.getParameterValues("checkappno");//得到页面上用户选择的要变更状态的记录的IDModelListappNoList=newModelArrayList();if(checkappno!=null){for(inti=0;i<checkappno.length;i++){DynamicModelbecomeModel=newDynamicModel();Stringappno=checkappno[i];String[]ids=appno.split(",");if(ids.length==2){becomeModel.addParam("id",ids[0]);becomeModel.addParam("personId",ids[1]);becomeModel.addParam("changeflag",changeflag);becomeModel.addParam("userId",userId);appNoList.addModel(becomeModel);}}}if(appNoList.isEmpty()){StringapplyId=request.getParameter("id");StringpersonId=request.getParameter("personId");model.addParam("applyId",applyId);model.addParam("personId",personId);updateflag=facade.updateInternalSupplierStatus(model);//进行写数据库,更新指定的状态}else{updateflag=facade.updateInternalSupplierStatus(appNoList);//进行写数据库,更新指定的状态}if(updateflag){mav.setMessageCode(appNoList.size()+"InternalSuppliersyouchoicedhavebeenbecome"+("1".equals(changeflag)?"Active":"Inactive"));}else{mav.setMessageCode("Sorry,systemfoundsomeunknowexceptionwhentriedtoupdatethestatus");}flowName=FLOW_NAME_DETAIL;}有的人说将"personId","applyId"等这些Key定义为常量,如StaticfinalStringPERSON_ID="personId",然后model.addParam(PERSON_ID,personId);这样算是将HardCode变成软编码吗?
解决方案
本帖最后由 fhlu18 于 2014-11-15 15:07:04 编辑