Generate code 2nd using groovy Template

Source: Internet
Author: User

Update at 2005.7.28 upgrade update Java direct call method and JSR-2

Previous: lightweight code generation solution Selection

0. Groovy template Overview

Groovy template is actually a simplified groovy version of JSP, and there is a built-in XML simplified syntax such as XPath, and does not rely on JSP containers, can be directly called by Java or groovy.

Templateengine has only 219 lines of implementation code, in src/groovy/test/simpletemplateengine. Java

Because the multi-line string of the JSR-2 is enclosed by "instead of", simpletemplateengine is not available for the moment and can be used by gstringtemplateengine for the moment.

1. Groovy Template
Link to the official groovy Website: groovy templates

Groovy template is similar to JSP, but it has all the advantages of Dynamic Language.
Practical GROOVY: Using groovy templates for MVC programming on the IBM Chinese site

2. Groovy built-in XML syntax
Link on the official groovy Website: gpath

Gpath is groovy's built-in XML access syntax, which is very simple. The two typical usage methods are as follows:

Characters = book. Character
For (C in characters)
{
Println C ['@ name']
}
And
Character = book. character. Find {It ['@ id'] = '2 '}

3. Call groovy template in ant
3.1 I used groovy to call template in IBM's article.

3.2 using Java to directly call template is as follows:

 SimpleTemplateEngine engine = new SimpleTemplateEngine();
 Map binding  = new HashMap();
 binding.put(....);
 String output = engine.createTemplate(templateFile).make(dataModel).toString();

4. Limitations:
According to colleagues
1. The template cannot use the import statement. You must write the full path of the class.
2. No convenient escape characters for <%>
3. In XML syntax, nodes can only get children (), but not parent ()

It is not difficult. I believe that the subsequent versions will improve. The worst thing is that there is no good editor. Think about the benefits of velocity and freemarker. It is not so easy for GSP to have a good editor soon.

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.