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

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

问题描述

我很新的使用速度。我想用它来生成一个HTML表单。我在Eclipse中工作。下面的罐子都在我的类路径:

 速度-DEP-1.5.jar
公地collections.jar
公地lang.jar
的log4j-1.2.8.jar
的ant.jar

我运行一个Ant构建文件来构建我的项目,但我没有看到正在生成的HTML。是否有什么我失踪,为了得到它实际生成HTML文件?我下面的教程只有我基于矿山关闭的两个文件。它的工作人称,但也许还有别的东西,我不知道是新的使用速度。我已经包括了我的code和构建脚本,使之更容易看到,如果我失去了一些东西。非常感谢你!

我有模板code我在这里的形式( form.vm

 < HTML和GT;
< HEAD>
    <标题>我的形式和LT; /标题>
< /头><身体GT;
#如果($ fieldErrors)
    #foreach($在$ fieldErrors误差)
        $错误< BR>
    #结束
#结束
#如果($的ActionErrors)
    #foreach($在$的ActionErrors误差)
        $错误< BR>
    #结束
#结束<表格名称=编辑行动=edit.action方法=后>
    <表>
        < TR>< TD>测试与LT; / TD>< TD> 123 LT; / TD>< / TR>
        #foreach($在$单选列表图)
            #formRowRadio(办法$法真$选择)< BR />
        #结束
    < /表>
    <表>
        #foreach($在$文本清单图)
            #formRowText($标签$标签$值)
        #结束
        < TR>< TD>&安培; NBSP;< / TD>< TD><输入类型=提交名称=提交值=提交>< / TD>< / TR&GT ;
    < /表>< /表及GT;< /身体GT;
< / HTML>

下面是java code。我在那一起去( formDemo.java

 进口java.io.StringWriter中;
进口的java.util.ArrayList;
进口的java.util.HashMap;
进口的java.util.Map;进口org.apache.velocity.VelocityContext;
进口org.apache.velocity.app.Velocity;
进口org.apache.velocity.Template;公共类formDemo {
    公共静态无效的主要(字串[] args)
        抛出异常{        Velocity.init();        ArrayList的单选列表=新的ArrayList();
        地图=新的HashMap();
        map.put(法,是);
        map.put(选中,FALSE);
        radioList.add(地图);        地图=新的HashMap();
        map.put(法,否);
        map.put(选中,FALSE);
        radioList.add(地图);        / *
         *列表添加到VelocityContext
         * /
        VelocityContext语境=新VelocityContext();
        context.put(收音机,单选列表);        ArrayList的文本清单=新的ArrayList();
        地图=新的HashMap();
        map.put(标签,名字);
        map.put(价值,);
        textList.add(地图);        地图=新的HashMap();
        map.put(标签,姓氏);
        map.put(价值,);
        textList.add(地图);        context.put(文本框,文本清单);
        模板模板= Velocity.getTemplate(form.vm);
        StringWriter的作家=新的StringWriter();
        template.merge(背景下,作家);
    }
 }

构建脚本(的build.xml

 < XML版本='1.0'编码='UTF-8'&GT?;
<,项目名称=velocityTemplate默认为罐子BASEDIR =><属性名='CLS'位置='$ {BASEDIR} /班'/>
<属性名='逸'位置='$ {BASEDIR} /数据/>
<属性名='根'的位置='$ {BASEDIR} /根/>
<属性名='库'的位置='$ {BASEDIR} / lib目录/>
<属性名='src'中的位置='$ {BASEDIR} / src目录/>
<属性名=TMP位置='$ {BASEDIR} /模板/><路径ID ='project.classpath'>
    < pathelement位置='$ {} CLS/>
    <文件集DIR ='$ {} lib目录包括='*罐子。/>
< /路径> <目标名称='干净'的描述='干净'>
    <删除DIR ='$ {} CLS/>
    <删除DIR ='$ {}根/>
< /目标与GT;<目标名称='补偿'描述='编译源。'>
    <的mkdir DIR ='$ {} CLS/>
    < javac的SRCDIR =$ {src}字符串'DESTDIR ='$ {} CLS'classpathref ='project.classpath'叉=真/>
< /目标与GT;<目标名称='罐子'取决于='补偿'描述='JAR应用程序。'>
    <坛子destfile =$ {} ant.project.name的.jar更新='假'FILESONLY ='真'指数='真'>
        <文件集DIR ='$ {} CLS/>
        <文件集DIR ='$ {src}字符串/>
    < /瓶>
< /目标与GT;<目标名称='运行'取决于='罐子'描述=运行应用程序。'>
    <路径ID ='velocityTemplate.classpath'>
        < pathelement位置='$ {} ant.project.name的.jar/>
        <文件集DIR ='$ {} lib目录包括='*罐子。/>
    < /路径>
    <的taskdef classpathref ='velocityTemplate.classpath/>
    <的mkdir DIR ='$ {}根/>
    <枚举outputPath ='$ {}根'inputPath ='$ {}逸'templateFile ='$ {} TMP /form.vm/>
< /目标与GT;<目标名称=形式描述='创建形式'>
    <路径ID ='velocityTemplate.classpath'>
        < pathelement位置='$ {BASEDIR} /velocityTemplate.jar/>
        < pathelement位置='$ {}的lib /velocity-dep-1.5.jar/>
    < /路径>    <的taskdef classpathref ='velocityTemplate.classpath/>
    < velocityTemplate outputPath ='$ {BASEDIR} / src目录templateFile ='$ {BASEDIR} /form.vm/>
< /目标与GT;< /项目>


解决方案

如果你只是想看到正在生成的HTML,你必须打印出的StringWriter的值或将其写入文件。

目前,除非有code丢失,你只是填补了缓冲区。

 模板模板= Velocity.getTemplate(form.vm);
    StringWriter的作家=新的StringWriter();
    template.merge(背景下,作家);

template.merge(背景下,作家)只是渲染模板到的StringWriter对象。

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>

解决方案

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.

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

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