未初始化及其影响
如下代码中及其不合理的地方
public void execute(Environment env, Map params, TemplateModel[] loopVars,
TemplateDirectiveBody body) throws TemplateException, IOException {
Pagination page=new Pagination();
CmsSite site = FrontUtils.getSite(env);
String orgName=DirectiveUtils.getString(PARAM_ORG_NAME, params);
int areaId=site.getAreaId().getId();
List list=reportMng.getList(areaId);
if(null!=page.getList()||null!=list) //从这开始未对page 或list 初始化 导致编译为通过后条件大括号一直控制到结尾影从而影响代码的维护性,也导致后面无需用page list对象的条件也放进来
{
page = reportMng.queryOrg_InfoCount(site.getId(),site.getAreaId().getId(),orgName,1, 10);
Map<String, TemplateModel> paramWrap = new HashMap<String, TemplateModel>(
params);
paramWrap.put(OUT_BEAN, DEFAULT_WRAPPER.wrap(site));
paramWrap.put(OUT_PAGINATION, DEFAULT_WRAPPER.wrap(page));
paramWrap.put(OUT_LIST,DEFAULT_WRAPPER.wrap(list));
Map<String, TemplateModel> origMap = DirectiveUtils
.addParamsToVariable(env, paramWrap);
InvokeType type = DirectiveUtils.getInvokeType(params);
String listStyle = DirectiveUtils.getString(PARAM_STYLE_LIST, params);
if (InvokeType.sysDefined == type) {
if (StringUtils.isBlank(listStyle)) {
throw new ParamsRequiredException(PARAM_STYLE_LIST);
}
env.include(TPL_STYLE_LIST + listStyle + TPL_SUFFIX, UTF8, true);
} else if (InvokeType.userDefined == type) {
if (StringUtils.isBlank(listStyle)) {
throw new ParamsRequiredException(PARAM_STYLE_LIST);
}
FrontUtils.includeTpl(TPL_STYLE_LIST, site, env);
} else if (InvokeType.custom == type) {
FrontUtils.includeTpl(TPL_NAME, site, params, env);
} else if (InvokeType.body == type) {
body.render(env.getOut());
} else {
throw new RuntimeException("invoke type not handled: " + type);
}
DirectiveUtils.removeParamsFromVariable(env, paramWrap, origMap);
}
}
现在贴上修改后代码:
public void execute(Environment env, Map params, TemplateModel[] loopVars,
TemplateDirectiveBody body) throws TemplateException, IOException {
//对page, list对象先初始化 使其对象有个地址指向
List<Org_Info> list = new ArrayList();
Pagination page=new Pagination();
CmsSite site = FrontUtils.getSite(env);
String orgName=DirectiveUtils.getString(PARAM_ORG_NAME, params);
int areaId=site.getAreaId().getId();
list=reportMng.getList(areaId);
//初始化后虽然没值 但有地址指向 至使 对象不为null 能够编译通过
page = reportMng.queryOrg_InfoCount(site.getId(),site.getAreaId().getId(),orgName,1, 10);
// if(null!=page.getList()||null!=list)
// {
//
// }
Map<String, TemplateModel> paramWrap = new HashMap<String, TemplateModel>(
params);
paramWrap.put(OUT_BEAN, DEFAULT_WRAPPER.wrap(site));
paramWrap.put(OUT_PAGINATION, DEFAULT_WRAPPER.wrap(page));
paramWrap.put(OUT_LIST,DEFAULT_WRAPPER.wrap(list));
Map<String, TemplateModel> origMap = DirectiveUtils
.addParamsToVariable(env, paramWrap);
InvokeType type = DirectiveUtils.getInvokeType(params);
String listStyle = DirectiveUtils.getString(PARAM_STYLE_LIST, params);
if (InvokeType.sysDefined == type) {
if (StringUtils.isBlank(listStyle)) {
throw new ParamsRequiredException(PARAM_STYLE_LIST);
}
env.include(TPL_STYLE_LIST + listStyle + TPL_SUFFIX, UTF8, true);
} else if (InvokeType.userDefined == type) {
if (StringUtils.isBlank(listStyle)) {
throw new ParamsRequiredException(PARAM_STYLE_LIST);
}
FrontUtils.includeTpl(TPL_STYLE_LIST, site, env);
} else if (InvokeType.custom == type) {
FrontUtils.includeTpl(TPL_NAME, site, params, env);
} else if (InvokeType.body == type) {
body.render(env.getOut());
} else {
throw new RuntimeException("invoke type not handled: " + type);
}
DirectiveUtils.removeParamsFromVariable(env, paramWrap, origMap);
}
可见对象未初始化对其后面的代码修改及逻辑影响有着决定性的影响,当涉及到代码整体修改时,先自问对其初始化否,进而再下结论。