jimphei學(xué)習(xí)工作室

          jimphei學(xué)習(xí)工作室

            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            23 隨筆 :: 0 文章 :: 1 評(píng)論 :: 0 Trackbacks

          2009年11月26日 #

          import java.util.Map;

          import org.apache.velocity.app.VelocityEngine;
          import org.springframework.ui.velocity.VelocityEngineUtils;

          public class MsgBean ...{
              private VelocityEngine velocityEngine;

              private String msg;

              private Map model; // 用來(lái)保存velocity中的參數(shù)值

              private String encoding; // 編碼

              private String templateLocation; // 注入的velocity模塊

              public String getEncoding() ...{
                  return encoding;
              }

              public void setEncoding(String encoding) ...{
                  this.encoding = encoding;
              }

              public String getTemplateLocation() ...{
                  return templateLocation;
              }

              public void setTemplateLocation(String templateLocation) ...{
                  this.templateLocation = templateLocation;
              }

              public Map getModel() ...{
                  return model;
              }

              public void setModel(Map model) ...{
                  this.model = model;
              }

              public String getMsg() ...{
                  // return title;
                  // 將參數(shù)值注入到模塊后的返回值
                  return VelocityEngineUtils.mergeTemplateIntoString(velocityEngine,
                          templateLocation, encoding, model);

              }

              public void setMsg(String msg) ...{
                  this.msg = msg;
              }

              public VelocityEngine getVelocityEngine() ...{
                  return velocityEngine;
              }

              public void setVelocityEngine(VelocityEngine velocityEngine) ...{
                  this.velocityEngine = velocityEngine;
              }

          }

          <?xml version="1.0" encoding="UTF-8"?>
          <beans xmlns="http://www.springframework.org/schema/beans"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

           

             
           <bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean"> 
             <property name="resourceLoaderPath">
                      <value>classpath:velocity</value>
               </property>
              <property name="velocityProperties">
                           <props>
                                 <prop key="resource.loader">class</prop>
                                 <prop key="class.resource.loader.class">
                                       org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
                                 </prop>
                                 <prop key="velocimacro.library"></prop>
                                 <prop key="input.encoding">GBK</prop>
                                 <prop key="output.encoding">GBK</prop>
                                 <prop key="default.contentType">text/html; charset=GBK</prop>
                           </props>
                     </property>
          </bean>

          <bean id="msgBean" class="MsgBean">
                  <property name="templateLocation" value="test.vm"></property>
                  <property name="encoding" value="GBK"></property>
                  <property name="velocityEngine" ref="velocityEngine"></property>
          </bean>


          </beans>

          import java.io.File;
          import java.io.IOException;
          import java.util.HashMap;
          import java.util.Map;

          import org.apache.commons.io.FileUtils;
          import org.springframework.context.ApplicationContext;
          import org.springframework.context.support.ClassPathXmlApplicationContext;


          public class TestVeloctiy ...{
              public static void main(String[] args) ...{
                  // TODO Auto-generated method stub
                  ApplicationContext ctx=new ClassPathXmlApplicationContext("test3.xml");
                  MsgBean    msgBean=((MsgBean)ctx.getBean("msgBean"));
                  Map<String, String> data = new HashMap<String, String>();
                  data.put("me","yourname");
                  msgBean.setModel(data);
                  System.out.println(msgBean.getMsg());
                 
                  
                  //根據(jù)apache common IO 組件直接將內(nèi)容寫到一個(gè)文件中去.
                   File dest = new File( "test.html" );         
                    try ...{
                      FileUtils.writeStringToFile( dest, msgBean.getMsg(), "GBK" );
                  } catch (IOException e) ...{
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  }

              }
          }

          本文來(lái)自CSDN博客,轉(zhuǎn)載請(qǐng)標(biāo)明出處:http://blog.csdn.net/pengchua/archive/2008/01/17/2049490.aspx

          posted @ 2009-11-26 11:36 jimphei 閱讀(1153) | 評(píng)論 (0)編輯 收藏

          主站蜘蛛池模板: 陇南市| 甘孜县| 东丽区| 渑池县| 肇庆市| 交口县| 镇坪县| 微博| 闻喜县| 定南县| 南漳县| 海口市| 营山县| 景洪市| 元阳县| 广宁县| 平和县| 丰都县| 六枝特区| 丘北县| 遂溪县| 高唐县| 望都县| 浪卡子县| 祁阳县| 家居| 普格县| 海林市| 乐亭县| 芦溪县| 台湾省| 来宾市| 航空| 高邮市| 高唐县| 贵阳市| 福贡县| 白沙| 久治县| 吉木乃县| 通州市|