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.