[Velocity三]基于Servlet+Velocity的web应用

什么是VelocityViewServlet

使用org.apache.velocity.tools.view.VelocityViewServlet可以将Velocity集成到基于Servlet的web应用中,以Servlet+Velocity的方式实现web应用

Servlet + Velocity的一般步骤

1.自定义Servlet,实现VelocityViewServlet的handleRequest方法,不要覆写VelocityViewServlet的doGet或者doPost方法,它的默认实现是将请求转发到handleRequest方法中,在实现handleRequest时,需要返回一个vm的模版对象Template

2.在web.xml中,配置自定义的Servet,同时需要指定,Velocity配置文件velocity.properties的位置

3.在指定的目录下,创建velocity.properties配置文件,用于存放Velocity的配置信息,比如vm文件的加载位置

4.定义vm文件,根据需要,在第一步Servlet的handleRequest方法实现中,将vm需要的参数由Context参数对象传入

定义Servlet

package com.tom.servlets;

import org.apache.velocity.Template;
import org.apache.velocity.context.Context;
import org.apache.velocity.tools.view.VelocityViewServlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestVelocityViewServlet extends VelocityViewServlet {

    @Override
    protected Template handleRequest(HttpServletRequest request, HttpServletResponse response, Context ctx) {
        ctx.put("key", "This is value for the key");
        ctx.put("favoriteFruit","All");
        ctx.put("elements", new String[]{"One", "Two", "Three"});
        return getTemplate("abc.vm");
    }
}
 

配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <servlet>
        <servlet-name>Velocity</servlet-name>
        <servlet-class>com.tom.servlets.TestVelocityViewServlet</servlet-class>
        <!--定义Velocity配置文件velocity.properties的位置,相对于web应用根的路径-->
        <init-param>
            <param-name>org.apache.velocity.properties</param-name>
            <param-value>/WEB-INF/velocity.properties</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>Velocity</servlet-name>
        <url-pattern>/servlets/velocity</url-pattern>
    </servlet-mapping>
</web-app>
 

Velocity配置文件velocity.properties的配置

resource.loader=webapp
webapp.resource.loader.class=org.apache.velocity.tools.view.WebappResourceLoader
#relative to the web context, has the same parent directory with WEB-INF
#that is, vm and WEB-INF are sibling folders
webapp.resource.loader.path=/vm
 

上面指定了vm文件的加载路径,因为是WebappResourceLoader,因此是相对于web应用根开始算起,也就是vm和WEB-INF同级目录

vm文件定义

<!--abc.vm-->
<html>
    <body>
        <p>$key</p>
        #foreach($elem in $elements)
            <li>$elem</li>
        #end
        <div>
            #include("def.vm")
        </div>
    </body>
</html>

<!--def.vm-->
What's your favorite fruit? $favoriteFruit

结果

访问velocity这个servlet,得到的结果是:

This is value for the key

What's your favorite fruit? $favoriteFruit

总结

基于Servlet+Velocity的web应用目录结构:

|-web

     |- WEB-INF

           |- web.xml 

           |- velocity.properties

     |- vm

           |- abc.vm

           |- def.vm

问题

在上面的显示结果中,需要关注的是favoriteFruit没有被替换,可见,Context定义的值没有传递!那么如何做变量传递呢?

   

猜你喜欢

转载自bit1129.iteye.com/blog/2106142
今日推荐