spring结合velocity的应用实例

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; // 用来保存velocity中的参数值

    
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;
        
// 将参数值注入到模块后的返回值
        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());
        
         
        
//根据apache common IO 组件直接将内容写到一个文件中去. 
         File dest = new File( "test.html" );          
          
try ...{
            FileUtils.writeStringToFile( dest, msgBean.getMsg(), 
"GBK" );
        }
 catch (IOException e) ...{
            
// TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值