spring结合velocity的应用实例
来源:互联网 发布:java socket示例代码 编辑:程序博客网 时间:2024/05/22 02:19
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();
}
}
}
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();
}
}
}
- spring结合velocity的应用实例
- springMVC + velocity 的结合
- 关于Spring与Velocity结合的问题(获得request,session)
- Tapstry+Spring的结合实例
- Velocity与struts的结合
- Spring渲染Velocity模版实例
- xfire与spring结合的实例
- Spring与Jms结合的实例
- Spring + mybatis整合方案总结 结合实例应用
- Spring Aop重要概念介绍及应用实例结合分析
- Velocity 的应用示例
- Velocity 的应用示例
- Velocity 的应用示例
- Velocity 的应用示例
- Velocity 的应用示例
- Velocity 的应用示例
- velocity的应用
- Velocity的应用
- 实验3.10 D/A、A/D转换器
- 第四部分 电 子 电 路 仿 真 实 验
- EDA技术在数字电路设计中的应用
- .ASP.NET页面跳转提示保存(原创)
- 5.4.1 寄存器
- spring结合velocity的应用实例
- #1 数电篇:计数器.触发器......
- 4_2 内核钩子 - 《Rootkits——Windows内核的安全防护》第二部分 (转自CSDN读书频道)
- #2 触发器的电路结构与动作特点
- 《时代》关于李宇春的评论(转载)
- Mother''s Day 母亲节(转载)
- Jersey Restlet and Apache CXF
- AutoCAD2000软件学习心得
- 发动机的分类和基本