velocity应用于cms中的改造
在原有的CMS系统中,曾一度将模板文件存在数据库中,不仅难以调试管理,也非常容易丢失版本。而且velocity作为view层来显示页面,渲染页面的效率很高,于是便有了改造velocity配置的念头!
原有配置(applicationContext.xml中)
<bean id="velocityConfig"name="code"><bean id="velocityForNewsBuild"name="code">@Servicepublic class GetPageFromTemplate {@Autowiredprivate VelocityConfig velocityConfig;public String getPageHTML(Map<String, Object> contextValue,String templatePath) {_contextAssemble(contextValue);try {Template t = velocityConfig.getVelocityEngine().getTemplate(templatePath);StringWriter s = new StringWriter();VelocityContext velocityContext = new VelocityContext(contextValue);t.merge(velocityContext, s);return s.toString();} catch (Exception e) {e.printStackTrace();}return "";}private void _contextAssemble(Map<String, Object> contextValue) {contextValue.put("dateTool", new DateTool());contextValue.put("numberTool", new NumberTool());}}