Original article: http://www.java2000.net/p7845
Test the use of IF Statements
Program
- Package freemarker;
- Import java. Io. file;
- Import java. Io. fileoutputstream;
- Import java. Io. outputstreamwriter;
- Import java. Io. Writer;
- Import java. util. hashmap;
- Import java. util. Map;
- Import freemarker. template. configuration;
- Import freemarker. template. template;
- Public class testif {
- Private configuration CFG;
- Public configuration getcfg (){
- Return CFG;
- }
- Public void Init () throws exception {
- CFG = new configuration ();
- Cfg. setdirectoryfortemplateloading (new file ("bin/freemaker "));
- }
- Public static void main (string [] ARGs) throws exception {
- Testif OBJ = new testif ();
- OBJ. INIT ();
- Map root = new hashmap ();
- Root. Put ("user", "java2000.net ");
- Map latestproduct = new hashmap ();
- Latestproduct. Put ("url", "http://www.java2000.net ");
- Latestproduct. Put ("name", "Java century network ");
- Root. Put ("latestproduct", latestproduct );
- Template T = obj. getcfg (). gettemplate ("testif. FTL ");
- Writer out = new outputstreamwriter (New fileoutputstream ("testif.html"), "GBK ");
- T. Process (root, out );
- System. Out. println ("successfull ................");
- }
- }
Template
- <HTML>
- <Head>
- <Title> welcome! </Title>
- </Head>
- <Body>
- <H1>
- Welcome $ {user} <# If user = "java2000.net">, our beloved leader <# else> my friend </# If>!
- </H1>
- <P> our latest product:
- <A href = "$ {latestproduct. url}" >$ {latestproduct. name} </a>!
- </Body>
- </Html>
Output result
- <HTML>
- <Head>
- <Title> welcome! </Title>
- </Head>
- <Body>
- <H1>
- Welcome java2000.net, our beloved leader!
- </H1>
- <P> our latest product:
- <A href = "http://www.java2000.net"> JAVA century Network </a>!
- </Body>
- </Html>
Result Analysis:
<# If
The statement passes the following test conditions. If true
If not, the <# else> section is displayed,
End with </# If>.