A simple example of the velocity template engine in complex mail services

Source: Internet
Author: User

Velocity is a Java-based template engine in Apache. the simple use of this Apache velocity template engine can better separate style design from Java development.


For example, when sending emails to some customers, the mail body usually contains some standardized forms or links. When sending emails, of course, we can add HTML Tag modifier when writing the body in Java code to achieve the following goals:


However, if the business is more complex and the project is relatively large, the division of work of the project team should be clear. At this time, the mail developers (Java developers) are not good at form design, in other words, even if Java developers spend a long time developing the mail sending function to use HTML tags to modify the mail body content, it is difficult to reach the level designed by professional web designers. So what is the best way to meet user requirements? Is to let everyone do what they are best at: Java developers only use the mail function, and web designer only uses the body of a web form similar to the mail.


The table in the preceding email and Its Style and connection (<A> </a>) effect is a template implemented by web designer using HTML, java developers provide the content required for table cell filling. The template engine uses the template language to perfectly combine the two to form a beautiful mail content. Velocity provides implementation in this way, as shown in:

The implementation of this method provided by velocity comes from MVC. Web designer designs a template that uses HTML tags and template language (velocity template language), while the velocity template engine plays a controlling role, java developers only need to provide the relevant data as a map or other variables to this template to complete the relevant work. The Code is as follows:

Template: templatedemo. VM:

</Head> <center> <Table width = 850 border = 1 bordercolor = "#000000" id = "Tabid"> <tbody align = "Left"> <tr rowspan =" 2 "> <TD colspan = 4> <B> <font color = blue> $ map. get ("applyformid ") </font> </B> </TD> </tr> <TD align = middle colspan = 4> <font size = 5> <B> templatedemo </ b> </font> </TD> </tr> <TD nowrap width = "140"> <B> title: </B> </TD> <TD> $ map. get ("name") </TD> <TD nowrap> <B> keywords </B> </TD> <TD> $ map. get ("key") </TD> </tr> </tbody> </table> </center> 

 

Use of velocityengine

Velocityengine VE = new velocityengine (); ve. setproperty (velocity. resource_loader, "class"); ve. setproperty ("class. resource. loader. class "," org. apache. velocity. runtime. resource. loader. classpathresourceloader "); // sets the class path to load the template ve. setproperty (velocity. input_encoding, "UTF-8"); // sets the input Character Set ve. setproperty (velocity. output_encoding, "UTF-8"); // sets the output character set ve. init (); // initialize template engine template T = ve. gettemplate ("/templatedemo. VM "); // load the template, relative to the classpath path velocitycontext context = new velocitycontext (); hashmap <string, Object> result = new hashmap <string, Object> (); result. put ("name", "template"); result. put ("key", "language"); context. put ("map", result); stringwriter writer = new stringwriter (); T. merge (context, writer); // converts writer. tostring (); // form the final result

 

Although the template engine is not only used in the business of complex mail content, this simple example shows the implementation of interface and business data separation, which is also the main role of the template engine, that is, it can separate the interface from the data, greatly improving the development efficiency and making it easier for the project to reuse excellent interface design.

 

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.