???? 最近看《Professional Java Development with the Spring Framework》時(shí)看到了spring對(duì)底層資源的抽象,才找到了完美解決方案。
???? 原來(lái)的代碼:
????private?String?templatePath;
????public?void?setTemplatePath(String?templatePath)?{
????????this.templatePath?=?templatePath;
????}
????public?void?initListener()?{
????????TemplateEventListener?templateListener?=?new?TemplateEventListener(){
????????????public?void?handleTemplateEvent(TemplateEventSupport?evt)?{
????????????????//?添加事件到隊(duì)列中
????????????????queue.offer(evt);
????????????????if(log.isDebugEnabled()){
????????????????????log.debug("Add?Template?about:"?+?evt.getTemplateName());
????????????????}
????????????}
????????????
????????};
????????
????????//注冊(cè)模版監(jiān)聽事件
????????templateManager.addEventListener(Constants.TEMPLATE_SAVE_EVENT,?templateListener,false);
????????
????????
????????//設(shè)置freemarker的參數(shù)
????????freemarkerCfg?=?new?Configuration();
????????try?{
????????????freemarkerCfg.setDirectoryForTemplateLoading(new?File(templatePath));
????????????freemarkerCfg.setObjectWrapper(new?DefaultObjectWrapper());
????????????freemarkerCfg.setDefaultEncoding("UTF-8");
????????}?catch?(IOException?ex)?{
????????????throw?new?SystemException("No?Directory?found,please?check?you?config.");
????????}
????}
配置文件????public?void?setTemplatePath(String?templatePath)?{
????????this.templatePath?=?templatePath;
????}
????public?void?initListener()?{
????????TemplateEventListener?templateListener?=?new?TemplateEventListener(){
????????????public?void?handleTemplateEvent(TemplateEventSupport?evt)?{
????????????????//?添加事件到隊(duì)列中
????????????????queue.offer(evt);
????????????????if(log.isDebugEnabled()){
????????????????????log.debug("Add?Template?about:"?+?evt.getTemplateName());
????????????????}
????????????}
????????????
????????};
????????
????????//注冊(cè)模版監(jiān)聽事件
????????templateManager.addEventListener(Constants.TEMPLATE_SAVE_EVENT,?templateListener,false);
????????
????????
????????//設(shè)置freemarker的參數(shù)
????????freemarkerCfg?=?new?Configuration();
????????try?{
????????????freemarkerCfg.setDirectoryForTemplateLoading(new?File(templatePath));
????????????freemarkerCfg.setObjectWrapper(new?DefaultObjectWrapper());
????????????freemarkerCfg.setDefaultEncoding("UTF-8");
????????}?catch?(IOException?ex)?{
????????????throw?new?SystemException("No?Directory?found,please?check?you?config.");
????????}
????}




templatePath.path=D:/template
使用spring對(duì)底層資源的抽象只要把templatePath改成Resource就可以了????private?Resource?templatePath;
????public?void?setTemplatePath(Resource?templatePath)?{
????????this.templatePath?=?templatePath;
????}
????public?void?initListener()?{
????????????TemplateEventListener?templateListener?=?new?TemplateEventListener(){
????????????public?void?handleTemplateEvent(TemplateEventSupport?evt)?{
????????????????//?添加事件到隊(duì)列中
????????????????queue.offer(evt);
????????????????if(log.isDebugEnabled()){
????????????????????log.debug("Add?Template?about:"?+?evt.getTemplateName());
????????????????}
????????????}
????????????
????????};????
????????//注冊(cè)模版監(jiān)聽事件
????????templateManager.addEventListener(Constants.TEMPLATE_SAVE_EVENT,?templateListener,false);
????????
????????
????????//設(shè)置freemarker的參數(shù)
????????freemarkerCfg?=?new?Configuration();
????????try?{
????????????freemarkerCfg.setDirectoryForTemplateLoading(templatePath.getFile());
????????????freemarkerCfg.setObjectWrapper(new?DefaultObjectWrapper());
????????????freemarkerCfg.setDefaultEncoding("UTF-8");
????????}?catch?(IOException?ex)?{
????????????throw?new?SystemException("No?Directory?found,please?check?you?config.");
????????}
????}
bean的配置不變,只要修改properties文件就可以了。????public?void?setTemplatePath(Resource?templatePath)?{
????????this.templatePath?=?templatePath;
????}
????public?void?initListener()?{
????????????TemplateEventListener?templateListener?=?new?TemplateEventListener(){
????????????public?void?handleTemplateEvent(TemplateEventSupport?evt)?{
????????????????//?添加事件到隊(duì)列中
????????????????queue.offer(evt);
????????????????if(log.isDebugEnabled()){
????????????????????log.debug("Add?Template?about:"?+?evt.getTemplateName());
????????????????}
????????????}
????????????
????????};????
????????//注冊(cè)模版監(jiān)聽事件
????????templateManager.addEventListener(Constants.TEMPLATE_SAVE_EVENT,?templateListener,false);
????????
????????
????????//設(shè)置freemarker的參數(shù)
????????freemarkerCfg?=?new?Configuration();
????????try?{
????????????freemarkerCfg.setDirectoryForTemplateLoading(templatePath.getFile());
????????????freemarkerCfg.setObjectWrapper(new?DefaultObjectWrapper());
????????????freemarkerCfg.setDefaultEncoding("UTF-8");
????????}?catch?(IOException?ex)?{
????????????throw?new?SystemException("No?Directory?found,please?check?you?config.");
????????}
????}
????<bean?id="buildHtmlService"?class="cn.jdk.leaf.service.impl.BuildHtmlServiceImpl"?init-method="initListener">
????????<property?name="templatePath"><value>${templatePath}</value></property>
????</bean>
把properties文件修改成????????<property?name="templatePath"><value>${templatePath}</value></property>
????</bean>
templatePath.path=template
在webcontext目錄下面建立一個(gè)template目錄就可以了。在部署到服務(wù)器的時(shí)候需要部署到一個(gè)特定的目錄只要修改這個(gè)配置文件為templatePath.path=file:/D:/template
這樣就可以了。![]() | 除經(jīng)特別注明外,本文章版權(quán)歸莫多泡泡所有. 署名,非商業(yè)用途,保持一致.???somebody(莫多)??? |