Velocity + Spring定时器 生成静态页面
2015-08-10 22:54
357 查看
最近做项目需要将首页静态化,于是着手了下velocity,成功将首页静态化。
首先需要下载 velocity-dep.jar 在附件中我上传了
velocity需要配置如下
velocity.property
日志生成路径
runtime.log = velocity.log
乱码解决
input.encoding=GBK
output.encoding=GBK
指定vm模板路径 绝对所在路径(如我的vm模板放在src下 编译后就在一下目录了)
file.resource.loader.path = X://workspace//test//web//WEB-INF//classes
spring定时器
Java代码
public class ToHTMLServiceImp implements ToHTMLService{
// 稿件点击排行
private List articlelist = new ArrayList();
public void toHtml() {
//执行查询的方法
articlelist = 查询方法list
//使用vm模板静态化
读取配置文件
VelocityEngine ve = new VelocityEngine();
ve.init("X:\\workspace\\test\\src\\velocity.properties");
VelocityContext context = new VelocityContext();
//路径跟前面设置的path相对应
Template template = ve.getTemplate("mytemplate.vm");
//将获得的对象放入Velocity的环境上下文中方便取用
context.put("articleListMap", articleListMap);
//将模板生成到指定位置 我生成的名字叫home.html
File savefile = new File("X:\\workspace\\test\\web\\site\\home\\home.html");
if(!savefile.getParentFile().exists()) savefile.getParentFile().mkdirs();
FileOutputStream outstream = new FileOutputStream(savefile);
OutputStreamWriter writer = new OutputStreamWriter(outstream,"GBK");
BufferedWriter bufferWriter = new BufferedWriter(writer);
template.merge(context, bufferWriter);
bufferWriter.flush();
outstream.close();
bufferWriter.close();
}catch( Exception e ){
e.printStackTrace();
}
}
jsp页面包含html
<body>
<%@include file="home_head.jsp" %>
<!--生成的静态页面 使用jsp动态标签 加入 如编码一致 基本不会出现乱码的情况 -->
<jsp:include page="home.html"></jsp:include>
<%@include file="home_foot.jsp" %>
</body>
spring定时器相关配置
<bean id="timer" class="org.springframework.scheduling.timer.TimerFactoryBean">
<property name="scheduledTimerTasks">
<list>
<ref local="timeTask"/>
</list>
</property>
</bean>
<bean id="home.ToHTML" class="zgzy.site.timer.Home2HtmlTimer" singleton="true">
</bean>
<bean id="methodInvokingTask" class="org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean">
<property name="targetObject"><ref bean="home.ToHTML"/></property>
<property name="targetMethod"><value>toHtml</value></property>
</bean>
<bean id="timeTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
<property name="timerTask"><ref bean="methodInvokingTask"/></property>
<property name="delay"><value>0</value></property>
<!-- 我设的是 十分钟一次 -->
<property name="period"><value>600000</value></property>
</bean>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/config/site/site-time-task.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
vm标签语法 我贴几个我为了方便贴在记事本里的草稿 应该很容易理解 所以不解释了:)
$element.sid
#set ($i=0)
#foreach($element in $pictureList)
#set($i=$i+1)
$element.getStringFormat($element.name,22)
$element.fileDate
#foreach($element in $articleListMap.get("test"))
#foreach($element in $zhuantiList)
#if($velocityCount < 4)
#end
$element.getStringDate($element.fileDate,5,10)
提一句:生成的静态文件,如果是linux服务器,通过rsync+inotify,同步到静态文件夹或者是静态文件服务器
首先需要下载 velocity-dep.jar 在附件中我上传了
velocity需要配置如下
velocity.property
日志生成路径
runtime.log = velocity.log
乱码解决
input.encoding=GBK
output.encoding=GBK
指定vm模板路径 绝对所在路径(如我的vm模板放在src下 编译后就在一下目录了)
file.resource.loader.path = X://workspace//test//web//WEB-INF//classes
spring定时器
Java代码
public class ToHTMLServiceImp implements ToHTMLService{
// 稿件点击排行
private List articlelist = new ArrayList();
public void toHtml() {
//执行查询的方法
articlelist = 查询方法list
//使用vm模板静态化
读取配置文件
VelocityEngine ve = new VelocityEngine();
ve.init("X:\\workspace\\test\\src\\velocity.properties");
VelocityContext context = new VelocityContext();
//路径跟前面设置的path相对应
Template template = ve.getTemplate("mytemplate.vm");
//将获得的对象放入Velocity的环境上下文中方便取用
context.put("articleListMap", articleListMap);
//将模板生成到指定位置 我生成的名字叫home.html
File savefile = new File("X:\\workspace\\test\\web\\site\\home\\home.html");
if(!savefile.getParentFile().exists()) savefile.getParentFile().mkdirs();
FileOutputStream outstream = new FileOutputStream(savefile);
OutputStreamWriter writer = new OutputStreamWriter(outstream,"GBK");
BufferedWriter bufferWriter = new BufferedWriter(writer);
template.merge(context, bufferWriter);
bufferWriter.flush();
outstream.close();
bufferWriter.close();
}catch( Exception e ){
e.printStackTrace();
}
}
jsp页面包含html
<body>
<%@include file="home_head.jsp" %>
<!--生成的静态页面 使用jsp动态标签 加入 如编码一致 基本不会出现乱码的情况 -->
<jsp:include page="home.html"></jsp:include>
<%@include file="home_foot.jsp" %>
</body>
spring定时器相关配置
<bean id="timer" class="org.springframework.scheduling.timer.TimerFactoryBean">
<property name="scheduledTimerTasks">
<list>
<ref local="timeTask"/>
</list>
</property>
</bean>
<bean id="home.ToHTML" class="zgzy.site.timer.Home2HtmlTimer" singleton="true">
</bean>
<bean id="methodInvokingTask" class="org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean">
<property name="targetObject"><ref bean="home.ToHTML"/></property>
<property name="targetMethod"><value>toHtml</value></property>
</bean>
<bean id="timeTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
<property name="timerTask"><ref bean="methodInvokingTask"/></property>
<property name="delay"><value>0</value></property>
<!-- 我设的是 十分钟一次 -->
<property name="period"><value>600000</value></property>
</bean>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/config/site/site-time-task.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
vm标签语法 我贴几个我为了方便贴在记事本里的草稿 应该很容易理解 所以不解释了:)
$element.sid
#set ($i=0)
#foreach($element in $pictureList)
#set($i=$i+1)
$element.getStringFormat($element.name,22)
$element.fileDate
#foreach($element in $articleListMap.get("test"))
#foreach($element in $zhuantiList)
#if($velocityCount < 4)
#end
$element.getStringDate($element.fileDate,5,10)
提一句:生成的静态文件,如果是linux服务器,通过rsync+inotify,同步到静态文件夹或者是静态文件服务器
相关文章推荐
- spring 注解注入方式
- Spring MVC 4值传递
- JAVA class文件中的符号引用
- Struts2文件的上传
- Struts2中通配符的使用
- java中的代码块理解
- 根据XML文件的id自动写java文件的声明方法和findViewById方法
- Java中多组输入的方式
- Java 连接MS Access数据库
- Java多线程:线程状态
- Java中接口和抽象类有哪些区别
- java工具类Date 日期类应用
- Java垃圾回收机制
- Eclipse 堆栈和内存大小设置(转载)
- 2012届华为校园招聘机试题(java实现)
- JDBC及eclipse连接数据库
- spring 事务的配置方式
- 浅析Java中的final关键字
- eclipse,github文件误删后找回
- java基础入门