构建和运行 Velocity 生成 HTML [英] Building and Running Velocity Generating HTML

查看:35
本文介绍了构建和运行 Velocity 生成 HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对使用 Velocity 很陌生.我正在尝试使用它来生成 HTML 表单.我在 Eclipse 工作.以下 jars 在我的类路径中:

I'm very new to using Velocity. I am trying to use it to generate an HTML form. I am working in Eclipse. The following jars are in my classpath:

velocity-dep-1.5.jar
commons-collections.jar
commons-lang.jar
log4j-1.2.8.jar
ant.jar

我正在运行一个 ant 构建文件来构建我的项目,但我没有看到正在生成的 HTML.为了让它实际生成HTML文件,我缺少什么吗?我遵循的教程只有两个文件,我基于我的.它对作者有用,但也许还有一些我没有意识到使用速度的新手.我已经包含了我的代码和构建脚本,以便更容易地查看我是否遗漏了什么.非常感谢!

I am running an ant build file to build my project, but I don't see the HTML being generated. Is there something I'm missing in order to get it to actually generate the HTML file? The tutorial I was following only has two files that I based mine off of. It worked for the author, but perhaps there is something else that I don't realize being new to using velocity. I have included my code and build script to make it easier to see if I'm missing something. Thank you very much!

我的表单模板代码在这里 (form.vm):

I have the template code for my form here (form.vm):

<html>
<head>
    <title> My Form </title>
</head>

<body>
#if ($fieldErrors)
    #foreach ($error in $fieldErrors)
        $error<br>
    #end
#end
#if ($actionErrors)
    #foreach ($error in $actionErrors)
        $error<br>
    #end
#end

<form name="edit" action="edit.action" method="post">
    <table>
        <tr><td>Testing</td><td>123</td></tr>
        #foreach($map in $radioList)
            #formRowRadio("method" $method "true" $selected)<br/>
        #end
    </table>
    <table>
        #foreach($map in $textList)
            #formRowText($label $label $value)
        #end
        <tr><td>&nbsp;</td><td><input type="submit" name="submit" value="submit"></td></tr>
    </table>

</form>

</body>
</html>

这是我必须使用的 Java 代码 (formDemo.java)

Here is the java code I have to go along with that (formDemo.java)

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.Template;

public class formDemo {
    public static void main ( String[] args )
        throws Exception {

        Velocity.init();

        ArrayList radioList = new ArrayList();
        Map map = new HashMap();
        map.put("method", "Yes");
        map.put("selected", false);
        radioList.add(map);

        map = new HashMap();
        map.put("method", "No");
        map.put("selected", false);
        radioList.add(map);

        /* 
         * add the list to a VelocityContext
         */
        VelocityContext context = new VelocityContext();
        context.put("radios", radioList);

        ArrayList textList = new ArrayList();
        map = new HashMap();
        map.put("label", "FirstName");
        map.put("value", "");
        textList.add(map);

        map = new HashMap();
        map.put("label", "LastName");
        map.put("value", "");
        textList.add(map);

        context.put("textfields", textList);
        Template template = Velocity.getTemplate("form.vm");
        StringWriter writer = new StringWriter();
        template.merge(context, writer);
    }
 }

构建脚本(build.xml)

<?xml version='1.0' encoding='UTF-8'?>
<project name="velocityTemplate" default="jar" basedir=".">

<property name='cls' location='${basedir}/classes'/>
<property name='dat' location='${basedir}/data'/>
<property name='gen' location='${basedir}/gen'/>
<property name='lib' location='${basedir}/lib'/>
<property name='src' location='${basedir}/src'/>
<property name='tmp' location='${basedir}/templates'/>

<path id='project.classpath'>
    <pathelement location='${cls}'/>
    <fileset dir='${lib}' includes='*.jar'/>
</path>    <target name='clean' description='Clean.'>
    <delete dir='${cls}'/>
    <delete dir='${gen}'/>
</target>

<target name='comp' description='Compile the source.'>
    <mkdir dir='${cls}'/>
    <javac srcdir='${src}' destdir='${cls}' classpathref='project.classpath' fork='true'/>
</target>

<target name='jar' depends='comp' description='JAR the application.'>
    <jar destfile='${ant.project.name}.jar' update='false' filesonly='true' index='true'>
        <fileset dir='${cls}'/>
        <fileset dir='${src}'/>
    </jar>
</target>

<target name='run' depends='jar' description='Run the application.'>
    <path id='velocityTemplate.classpath'>
        <pathelement location='${ant.project.name}.jar'/>
        <fileset dir='${lib}' includes='*.jar'/>
    </path>
    <taskdef classpathref='velocityTemplate.classpath'/>
    <mkdir dir='${gen}'/>
    <enumerator outputPath='${gen}' inputPath='${dat}' templateFile='${tmp}/form.vm'/>
</target>

<target name='form' description='Creates form'>
    <path id='velocityTemplate.classpath'>
        <pathelement location='${basedir}/velocityTemplate.jar'/>
        <pathelement location='${lib}/velocity-dep-1.5.jar'/>
    </path>

    <taskdef classpathref='velocityTemplate.classpath'/>
    <velocityTemplate outputPath='${basedir}/src' templateFile='${basedir}/form.vm'/>
</target>

</project>

推荐答案

如果您只想查看生成的 html,则必须打印出 StringWriter 的值或将其写入文件.

If you just want to see the html being generated, you'll have to print out the value of the StringWriter or write it to a file.

目前,除非缺少代码,否则您只是在填充缓冲区.

Currently, unless there's code missing, you're just filling up the buffer.

    Template template = Velocity.getTemplate("form.vm");
    StringWriter writer = new StringWriter();
    template.merge(context, writer);

template.merge(context, writer) 只是将模板呈现给 StringWriter 对象.

template.merge(context, writer) just renders the template to the StringWriter object.

这篇关于构建和运行 Velocity 生成 HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆