Velocity Quick Start [4]-condition and loop

Source: Internet
Author: User
Source file:
package nc.jonathan.velocity;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;

/**
* <P>
* Fourth instance
* </P>
*
* <P> learn VTL in Velocity-template language: Condition and loop </p>
*
* Create on 2006-4-3 13:20:12
*
* @ Author Jonathan Q. Bo
* @ Version 1.0 valocity study
*/
public class FourExample {
 
 public FourExample(){
}
 
/**
* Test Method
*/
Public void test (){
Try {
/* Initialize the runtime engine and specify the template folder location for initialization */
Properties p = new Properties ();
P. setProperty ("file. resource. loader. path", "vm ");
Velocity. init (p );

/* Create the context and put the data */
Velocitycontext context = new velocitycontext ();
Context. Put ("list", this. getlist ());
Context. Put ("condition", new Boolean (this. getboolean ()));

/* Output target of parsed data, subclass of Java. Io. Writer */
Filewriter W = new filewriter ("GEN/fourexamplegen. Java ");
Bufferedwriter BW = new bufferedwriter (w );


/* Parse */
// Velocity. mergetemplate ("secondtemplate. VM", context, W );
Velocity. mergetemplate ("fourtemplate. VM", "gb2312", context, BW );

Bw. Flush ();
Bw. close ();
System. out. println ("#... successful! ");
} Catch (ResourceNotFoundException e1 ){
System. out. println ("# The source file does not exist! ");
E1.printStackTrace ();
} Catch (ParseErrorException e2 ){
System. out. println ("# file parsing error! ");
E2.printStackTrace ();
} Catch (MethodInvocationException e3 ){
System. out. println ("# method call exception! ");
E3.printStackTrace ();
} Catch (Exception e4 ){
System. out. println ("# other errors! ");
E4.printStackTrace ();
}
}
/**
* Generate a random array
* @ Return java. util. List
*/
Private List getList (){
ArrayList al = new ArrayList ();
For (int I = 0; I <10; I ++ ){
Al. add (String. valueOf (Math. random () * 100 ));
}
Return al;
}
 
/**
* Generate a random Boolean Value
* @ Return boolean
*/
Private boolean getBoolean (){
Int tmp1 = (int) (Math. random () * 100 );
Int tmp2 = (int) (Math. random () * 100 );
Return (tmp1-tmp2)> 0? True: false;
}
 
Public static void main (String [] args ){
FourExample example = new FourExample ();
Example. test ();
}
 
}
Template File
Test loop foreach... end
#foreach( $name in $list )
    Array[$velocityCount] : [$name]
#end
Test condition if... else... end
#if ($condition)
    The condition is true!
#else
    The condition is false!
#end
Test loop + condition
#foreach( $name2 in $list )
#if(($velocityCount+1)%2 == 1)
  + Array[$velocityCount] : [$name2]
#else
  - Array[$velocityCount] : [$name2]
#end
#end
Output result
Test loop foreach... end
    Array[1] : [94.19815973077552]
    Array[2] : [92.175355103828]
    Array[3] : [28.983192371817836]
    Array[4] : [17.067573246183066]
    Array[5] : [69.93726110689275]
    Array[6] : [18.66140016348623]
    Array[7] : [66.10854437485507]
    Array[8] : [47.47973968131647]
    Array[9] : [79.88433146393561]
    Array[10] : [0.7504344062761126]
Test condition if... else... end
    The condition is true!
Test loop + condition
  - Array[1] : [94.19815973077552]
  + Array[2] : [92.175355103828]
  - Array[3] : [28.983192371817836]
  + Array[4] : [17.067573246183066]
  - Array[5] : [69.93726110689275]
  + Array[6] : [18.66140016348623]
  - Array[7] : [66.10854437485507]
  + Array[8] : [47.47973968131647]
  - Array[9] : [79.88433146393561]
  + Array[10] : [0.7504344062761126]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.