SMARTY4J is an open-source template engine. Yes, it is the Java porting version of the famous PHP template engine.
It is characterized by the compilation of template files or strings into a Java class directly executed, so efficiency than the general template interpretation of the way to handle faster. It developed late, so no velocity, Freemarker famous, popular also than the PHP version of the template engine much worse.
But it's fast!
In a skeptical manner, I measured it myself. When rendering 1000 times a simple page (introducing only a few simple data types), the smarty4j takes 16 milliseconds, the velocity is 63 milliseconds, and the Freemarker uses 109 milliseconds. When rendered 5,000 times, the smarty4j takes 172 milliseconds, velocity is 328 milliseconds, and Freemarker uses 390 milliseconds. (The above tests are averaged over multiple tests)
Complex pages take more time-consuming differences. So replacing the velocity or freemarker in the project with SMARTY4J can speed up your application.
Already has the Netizen to provide the smarty4j the plug-in integration into the struts2. So can you integrate it into a homegrown compact Nutz MVC framework?
After looking through the Nutz user manual, I found this to be a very easy thing to do.
Let's start here:
1, first to implement the view adapter
Very simple:
Java code
- /**
- * SMARTY4J View Adapter
- * @author Qinerg ([email protected])
- */
- Public class Smartyviewmaker implements Viewmaker {
- Public View make (IOC, string type, String value) {
- if ("St". Equalsignorecase (Type)) {
- return new Smartyview (value);
- }
- return null;
- }
- }
2. Then implement the specific view parser
Java code
- /**
- * Generate pages using smarty4j templates
- * @author Qinerg ([email protected])
- */
- Public class Smartyview extends Abstractpathview implements View {
- Private Final String ext = ". html";
- private static engine engine = new engine (); Loading the template engine
- Public Smartyview (String dest) {
- super (dest);
- Engine.settemplatepath ("");
- Engine.setdebug (true);
- }
- / * Render page
- * @see Org.nutz.mvc.view#render (javax.servlet.http.HttpServletRequest, Javax.servlet.http.HttpServletResponse, Java.lang.Object)
- */
- public void Render (HttpServletRequest req, HttpServletResponse resp,
- Object obj) throws Throwable {
- if ("". Equals (Engine.gettemplatepath ())) {
- String Realpath = Req.getsession (). Getservletcontext (). Getrealpath ("/");
- Engine.settemplatepath (Realpath);
- }
- String Path = Evalpath (req, obj);
- //empty path, with default rules
- if (Strings.isblank (path)) {
- Path = Mvcs.getrequestpath (req);
- Path = "Web-inf"
- + (Path.startswith ("/")? "" : "/")
- + files.renamesuffix (path, ext);
- }
- //absolute path: The path beginning with '/' does not increase '/web-inf '
- Else if (Path.charat (0) = = '/') {
- if (!path.tolowercase (). EndsWith (EXT))
- Path + = ext;
- }
- //path in the form of package name
- else {
- Path = "web-inf/" + path.replace ('. ', '/') + ext;
- }
- Template template = engine.gettemplate (path);
- Context CTX = new Context (); //Generate Data container object
- Ctx.set ("obj", obj);
- Ctx.set ("Request", req);
- Ctx.set ("response", resp);
- Ctx.set ("Session", Req.getsession ());
- Template.merge (CTX, Resp.getwriter ());
- }
- }
All right, finish it!
So how do you use it specifically?
First declare the use of the SMARTY4J adapter on the main module
Java code
- @Views ({smartyviewmaker. Class})
- Public class Mainmodule {}
You can then declare the template path on the action, such as:
Java code
- @At ("/index")
- @Ok ("St:st.index")
- public Void Index () {
The template for this action will correspond to the web-inf/st/index.html file.
Of course the template path can also be placed outside the web-inf, such as:
@Ok ("ST:ABC.BBC") or @Ok ("ST:/ABC/BBC")
The corresponding template path is:
Abc/bbc.html
Let Nutz support the fastest template engine smarty4j