Use freemarker (3) if statement from scratch

Source: Internet
Author: User
Original article: http://www.java2000.net/p7845
Test the use of IF Statements

Program

  1. Package freemarker;
  2. Import java. Io. file;
  3. Import java. Io. fileoutputstream;
  4. Import java. Io. outputstreamwriter;
  5. Import java. Io. Writer;
  6. Import java. util. hashmap;
  7. Import java. util. Map;
  8. Import freemarker. template. configuration;
  9. Import freemarker. template. template;
  10. Public class testif {
  11. Private configuration CFG;
  12. Public configuration getcfg (){
  13. Return CFG;
  14. }
  15. Public void Init () throws exception {
  16. CFG = new configuration ();
  17. Cfg. setdirectoryfortemplateloading (new file ("bin/freemaker "));
  18. }
  19. Public static void main (string [] ARGs) throws exception {
  20. Testif OBJ = new testif ();
  21. OBJ. INIT ();
  22. Map root = new hashmap ();
  23. Root. Put ("user", "java2000.net ");
  24. Map latestproduct = new hashmap ();
  25. Latestproduct. Put ("url", "http://www.java2000.net ");
  26. Latestproduct. Put ("name", "Java century network ");
  27. Root. Put ("latestproduct", latestproduct );
  28. Template T = obj. getcfg (). gettemplate ("testif. FTL ");
  29. Writer out = new outputstreamwriter (New fileoutputstream ("testif.html"), "GBK ");
  30. T. Process (root, out );
  31. System. Out. println ("successfull ................");
  32. }
  33. }

Template

  1. <HTML>
  2. <Head>
  3. <Title> welcome! </Title>
  4. </Head>
  5. <Body>
  6. <H1>
  7. Welcome $ {user} <# If user = "java2000.net">, our beloved leader <# else> my friend </# If>!
  8. </H1>
  9. <P> our latest product:
  10. <A href = "$ {latestproduct. url}" >$ {latestproduct. name} </a>!
  11. </Body>
  12. </Html>

Output result

  1. <HTML>
  2. <Head>
  3. <Title> welcome! </Title>
  4. </Head>
  5. <Body>
  6. <H1>
  7. Welcome java2000.net, our beloved leader!
  8. </H1>
  9. <P> our latest product:
  10. <A href = "http://www.java2000.net"> JAVA century Network </a>!
  11. </Body>
  12. </Html>

Result Analysis:

<# If

The statement passes the following test conditions. If true

If not, the <# else> section is displayed,

End with </# If>.

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.