Building and Running Velocity Generating HTML

2019-06-09 01:20发布

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

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!

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>

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 script (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>

4条回答
Ridiculous、
2楼-- · 2019-06-09 02:02

I think your template should be as the following:

<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 $radios)
            #formRowRadio("method" $map.method "true" $map.selected)<br/>
        #end
    </table>
    <table>
        #foreach($map in $textfields)
            #formRowText($map.label $map.label $map.value)
        #end
        <tr><td>&nbsp;</td><td><input type="submit" name="submit" value="submit"></td></tr>
    </table>

</form>

</body>
</html>

And you also need to effectively print the StringWriter to a File.html.

查看更多
仙女界的扛把子
3楼-- · 2019-06-09 02:03

It looks like your build script is not actually executing the Java code.

Try using the java ant task, see here:

Ant Java task

查看更多
一纸荒年 Trace。
4楼-- · 2019-06-09 02:08

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) just renders the template to the StringWriter object.

查看更多
时光不老,我们不散
5楼-- · 2019-06-09 02:15

In the code you posted: there 2 ways to run velocity:

  • running the main (but this is not what you do when you build your project)
  • running ant form but since you don't fill any velocity context: velocity won't be able to replace the variable in your template (i.e. the generated html won't contains dynamic data)

I'm not sure what you need:

Is it using velocity at build time ? In this case you have to run ant form and then ant run (or add a depends="form" in comp target). But you also have to provide a velocity context to velocity in the build.xml

If you need to run velocity at runtime: just use a file writer to perfom the velocity merge.

查看更多
登录 后发表回答