4-8 Helper设计
2019-08-09 22:39:59 0 举报
AI智能生成
Spring团购项目总结
作者其他创作
大纲/内容
FreeMarker模板辅助操作,页面数据替换入口,统一模板变量替换规则,为差异化页面替换提供入口
Http Request -> Interceptor -> Get Helper Instance -> Set To Model -> Freemarker Engine -> Replace
index.ftl
<a href="${ctx}/item/${deal.skuId}"><img src="${helper.getDealImageUrlForIndexDeal1List(deal)}" alt=""></a>
public class HelperInterceptor implements HandlerInterceptor
spring-servlet.xml里配置了该mvc拦截器,在http请求被handler处理后,会搜索该handler所在的controller是否有对应名称的helper bean,若有,则在model里set一个名为helper,值为该helper bean的键值对,若无,则set一个默认的baseSiteHelper。这样Freemarker模板就可以通过${helper.xxx()}取值了。若handler处理的结果以redirect开头,说明返回重定向,则将model清空。
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object object, ModelAndView modelAndView) throws Exception
this.injectHelper(modelAndView, object);
private void injectHelper(ModelAndView modelAndView, Object object)
object来源于拦截器参数object,其为http请求的handler或action方法。
判断modelAndView非空且modelAndView.getModel()非空且object类型为HandlerMethod才继续进行
HandlerMethod handlerMethod = (HandlerMethod) object;
String simpleClassName = handlerMethod.getBeanType().getSimpleName();
String controllerName = StringUtil.convertFirstChar2LowerCase(simpleClassName.replace("Controller", ""));
String actionName = handlerMethod.getMethod().getName();
String helperBeanId = controllerName + "Helper";//indexHelper
modelAndView.getModel().put("controller", controllerName);
modelAndView.getModel().put("action", actionName);
String simpleClassName = handlerMethod.getBeanType().getSimpleName();
String controllerName = StringUtil.convertFirstChar2LowerCase(simpleClassName.replace("Controller", ""));
String actionName = handlerMethod.getMethod().getName();
String helperBeanId = controllerName + "Helper";//indexHelper
modelAndView.getModel().put("controller", controllerName);
modelAndView.getModel().put("action", actionName);
if (modelAndView.getViewName().startsWith("redirect:")) {
modelAndView.getModel().clear();
}
modelAndView.getModel().clear();
}
BaseHelper helper = null;
try {
helper = SpringApplicationContext.getBean(helperBeanId);
} catch (Exception e) {
//防御性容错
helper = SpringApplicationContext.getBean("baseSiteHelper");
}
if (null != helper) {
modelAndView.getModel().put("helper", helper);
}
try {
helper = SpringApplicationContext.getBean(helperBeanId);
} catch (Exception e) {
//防御性容错
helper = SpringApplicationContext.getBean("baseSiteHelper");
}
if (null != helper) {
modelAndView.getModel().put("helper", helper);
}
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {
return true;
}
return true;
}
index.ftl是由IndexController的index方法返回的
查看Project类层次发现helper包下只有DealHelper和BaseSiteHelper,由HelperInterceptor的逻辑,可知index方法返回的model里set的helper是BaseSiteHelper
@Component
public class BaseSiteHelper extends FrontendBaseHelper
public class BaseSiteHelper extends FrontendBaseHelper
public String getDealImageUrlForDealList(Deal deal) {
return getObjectImageUrl(DealConstant.PICTURE_MODULE_DEAL, deal.getImageId(),
DealConstant.PICTURE_SIZE_BY_TYPE_LIST);
}
return getObjectImageUrl(DealConstant.PICTURE_MODULE_DEAL, deal.getImageId(),
DealConstant.PICTURE_SIZE_BY_TYPE_LIST);
}
/*还可以用于替换模板中的css文件或js文件等*/public String getCSSFileUrl(String uri)
public String getJSFileUrl(String uri)
public class FrontendBaseHelper extends BaseHelper
protected String getObjectImageUrl(String objectClass, Long imageId, int imageIndex)
main.ftl
<link rel="stylesheet" type="text/css" href="${ctx}/css/main.css">,以这一处为例,若css或js与应用部署在同一服务器则这样用,ctx是线程设置的ThreadLocal,为应用的协议+ip+port,若部署在不同的服务器,则可改成${helper.getCSSFileUrl(url)
收藏
收藏
0 条评论
下一页