最近做项目需要将首页静态化,于是着手了下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定时器
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)
- 大小: 46.5 KB
- 大小: 49.5 KB
- 大小: 52.5 KB
- 大小: 70.3 KB
- 大小: 72.1 KB
- 大小: 70.6 KB
分享到:
相关推荐
Velocity+Spring 2[1].0+Hibernate的Web应用开发框架,讲解如何使用velocity生成spring+hibernate的框架代码。
struts + spring + hibernate + velocity + ajax + jotm + acegi
Spring+SpringMVC+Mybatis+Velocity+Maven整合
maven+springmvc+spring+ibatis+velocity+mysql
maven+springMVC+mybatis+velocity+mysql+junit项目框架搭建
Velocity+Spring+Ibatis框架搭建说明文档1
struts2.2+velocity+tiles+spring3+mybatis3.05集成框架实例 ,最新的框架组合.。
velocity+springmvc+spring+mybaitsg整合案例 用户管理 带sql ,亲自整合调试,没有问题! /* SQLyog Ultimate v8.32 MySQL - 5.6.19 : Database - test ******************************************************...
这是刚入velocity+ibatis+wework+spring门的一个学习与工作的demo,有不错的学习性.
采用struts+spring,以及手写封装类组装数据到vo模型,采用velocity模板渲染数据,采用spring的注解对action,server,dao层,
这是我写的一个Spring + Spring MVC + MyBatis + Velocity + MySQL 框架搭建完整项目,里面包含了所需的全部JAR包已经相关源码,感兴趣的同学可以下载喽。。。 配合我的博客更好哦!
maven+springMVC+mybatis+velocity+mysql+junit 代码生成器
Maven + Spring mvc + Mybatis + Velocity +国际化配置 整合实例,之前上传的整合 Demo,修改了一些问题,所以重新上传了一个
maven+spring mvc+hibernate+dubbo+velocity+mysql的demo源码,运行前请修改resources/config.properties下的数据配置
spring+velocity+ibatis
Velocity 是一个基于Java的模版引擎。它允许web 页面设计者引用JAVA代码预定义的方法。Web 设计者可以根据MVC模式和JAVA程序员并行工作...Velocity+Turbine 方案提供的模板服务将允许web 应用按真正的mvc模式进行开发。
velocity生成静态页面实例,自己写的,运行就可以看到效果
本篇文章将通过一个简单显示用户信息的实例整合Spring mvc+mybatis+Maven+velocity+mysql. 一、.Maven工程目录 二、Spring mvc + mybatis +maven实现 1.Mysql数据库表数据