0

私はVelocityの使用に非常に慣れていません。それを使用して HTML フォームを生成しようとしています。私はEclipseで働いています。次のjarは私のクラスパスにあります:

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

プロジェクトをビルドするために ant ビルド ファイルを実行していますが、HTML が生成されていません。HTMLファイルを実際に生成するために欠けているものはありますか? 私が従ったチュートリアルには、私が基にした2つのファイルしかありません。著者にとってはうまくいきましたが、速度を使用することに慣れていないことに気付いていないことが他にもあるかもしれません。何か不足しているかどうかを簡単に確認できるように、コードとビルド スクリプトを含めました。どうもありがとうございました!

ここにフォームのテンプレート コードがあります ( 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)

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>
4

4 に答える 4

0

テンプレートは次のようにする必要があると思います。

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

また、StringWriter を File.html に効果的に出力する必要もあります。

于 2013-09-27T15:43:03.140 に答える
0

html が生成されていることだけを確認したい場合は、StringWriter の値を出力するか、ファイルに書き込む必要があります。

現在、コードが欠落していない限り、バッファをいっぱいにするだけです。

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

template.merge(context, writer) は、テンプレートを StringWriter オブジェクトにレンダリングするだけです。

于 2013-09-27T15:41:17.657 に答える
0

あなたが投稿したコードでは:速度を実行するには2つの方法があります:

  • メインの実行 (ただし、これはプロジェクトをビルドするときに行うことではありません)
  • 実行ant form中ですが、速度コンテキストを入力していないため、速度はテンプレートの変数を置き換えることができません (つまり、生成された html には動的データが含まれません)。

何が必要かわからない:

ビルド時に速度を使用していますか? この場合、実行してから実行する必要がありますant form(ant runまたは、comp ターゲットに depends="form" を追加します)。ただし、build.xml で速度に速度コンテキストを提供する必要もあります。

実行時に速度を実行する必要がある場合: ファイル ライターを使用して速度マージを実行します。

于 2013-09-27T16:32:29.117 に答える