读书人

velocity应用于cms中的改建

发布时间: 2012-12-19 14:13:14 作者: rapoo

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());}}

使用WebappLoader有很大的好处,管理目录结构就可以很好的运用模板,给模板分类。同时在模板文件里可以直接使用#parse("path")来进行vm文件引入。方便进行文件切片以及复用。非常方便。



读书人网 >编程

热点推荐