spring MVC下velocity的使用配置

1.首先需要导入velocity需要的jar包,这个可以在网上下载到

2.在application.xml中进行配置

<!-- 模板信息设置 -->  
   <bean id="velocityConfigurer"  
       class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">  
       <property name="resourceLoaderPath"  value="WEB-INF/templates" /><!-- 設置模板防止位置-->  
       <property name="velocityProperties">  
           <props>  
               <prop key="directive.foreach.counter.name">loopCounter</prop>  
               <prop key="directive.foreach.counter.initial.value">0</prop>  
               <prop key="input.encoding">UTF-8</prop><!-- 指定模板引擎进行模板处理的编码 -->  
               <prop key="output.encoding">UTF-8</prop><!-- 指定输出流的编码 -->  
           </props>  
       </property>  
   </bean>

 3.写vm文件,这个文件一定要放到web-inf文件夹下,否者会报找不到文件错误

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
#set( $this = "Velocity")
$this is great!  <br/>
$name  <br/>
hi  , i am letian
<h1>你好</h1>
</body>
</html>

 如图放置
     
 

4.写Java方法

public void init(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		VelocityEngine velocityEngine = velocityConfigurer.getVelocityEngine();
		VelocityContext context = new VelocityContext();
		context.put("name", "test");
		StringWriter sw = new StringWriter();
		velocityEngine.mergeTemplate("../templates/test.vm", "utf-8", context, sw);
		System.out.println(sw.toString());
	}

 5.运行以后就可以在控制台输出

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
Velocity is great!  <br/>
test  <br/>
hi  , i am letian
<h1>你好</h1>
</body>
</html>

 好了,完成

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值