In other words, the beetl template framework is faster than the freemarker template. As for how it is, we haven't felt it yet.
Download the beetl package: beetl template path.
Beetl provides the integration of the jfinal framework. Using the beetrenderfactory class, you can complete the integration by registering the following code:
@ Overridepublic void configconstant (constants me) {loadpropertyfile ("JDBC. properties "); // configure the template me. setmainrenderfactory (New mybeetlrenderfactory (); // gets the grouptemplate template. You can set the grouptemplate = mybeetlrenderfactory for the shared variable operation. grouptemplate; me. setdevmode (getpropertytoboolean ("config. devmodel ", false); me. setviewtype (viewtype. JSP); me. setencoding ("UTF-8 ");}
This method serves as the constant configuration method in the jfinal framework. Here, my custom mybeetlrenderfactory class inherits from beetlrenderfactory, as follows:
package com.tenghu.core.beetl;import org.beetl.ext.jfinal.BeetlRender;import org.beetl.ext.jfinal.BeetlRenderFactory;import com.jfinal.render.Render;public class MyBeetlRenderFactory extends BeetlRenderFactory{@Overridepublic Render getRender(String view) {BeetlRender render=new BeetlRender(groupTemplate, view);return render;}@Overridepublic String getViewExtension() {return ".html";}}
Because beetrenderfactory uses fileresourceloader by default, its root directory is under webroot. To modify other directories, you need to create a beetl and properties attribute file under SRC, configure the following code in this file:
RESOURCE.root=/WEB-INF/view
Because the beetl default configuration file is in org/beetl/CORE/beetl-default.properties, beetl will be loaded through the configuration class at startup, and then load beetl in classpath. properties, If you create beetl. properties file, then the latter will overwrite the former
Here, even if jfinal integrates the beetl template, the following figure shows the implementation result:
public void index(){List<Users> testList=Users.dao.find("select * from users");setAttr("testList", testList);render("login.html");}
Here I output is the login.html page, the page in the WEB-INF/view, looked at is not very convenient, If you do not configure beetl. properties file, then render this place needs to write WEB-INF/View/login.html, a project certainly has a lot of pages, if not configured beetl. properties file, each page output to the interface must use a complete path, it will be a very troublesome thing to configure beetl. peoperties file reduces repetitive work
As you can see above, here I output a set, so how to traverse the set using the beetl template is very simple, as shown below:
<%for(test in testList){%><tr><td>${testLP.index}</td><td>${test.id}</td><td>${test.username!''}</td></tr><%}%>
Is there a sense of familiarity with this output? By the way, it is similar to the tag of the JSP script, but don't confuse it.
You may also see the testlp variable, which is a variable implicitly defined by beetl and can be used in a loop. Its naming convention is that the item and LP can provide information about the current loop, for example:
Testlp. Index; current index, starting from 1
Testlp. size; length of the Set
Testlp. First; whether it is the first
Testlp. Last; whether it is the last one
Testlp. Even; whether it is an even number
Testlp. Odd; whether it is an odd number
In the beetl template, there are more than one loop operation. The above for loop is in the form of for-in, and the following are:
For (exp; exp)
While
Elsefor
Use:
For (exp; exp)
<%for(var i=0;i<testList.~size;i++){%><tr><td>${testList.~size}</td><td>${testList[i].id}</td><td>${testList[i].username!''}</td></tr><%}%>
While
<%var i=0;while(i<testList.~size){%><tr><td>${testList.~size}</td><td>${testList[i].id}</td><td>${testList[i].username!''}</td></tr><%i++;}%>
For a for loop, if it is not in the loop body, it enters the elsefor statement block. Therefore, elsefor cannot be used independently by dogs.
<% For (VAR I = 0; I <testlist .~ Size; I ++) {%> <tr> <TD >$ {testlist .~ Size} </TD> <TD >$ {testlist [I]. ID} </TD> <TD >$ {testlist [I]. Username! ''} </TD> </tr> <%} elsefor {%> <tr> <TD colspan =" 3 "> no records </TD> </tr> <% }%>
Next, let's talk about the conditional statements in beetl:
If-Else
Switch-case
Select-case
The specific usage is as follows:
<% var a=true,b=1; if(a&&b==1){ }else if(a){ }else{ } %>
Switch-case:
<% var b=1; switch(b){ case 0: print('1'); break; case 1: print('2'); break; case 2: print('2'); break; default: print('error'); break; } %>Switch supports any type
Select-case is an enhanced version of switch case. He allows a logical expression in case, and does not need to break each case.
Exit after a qualified case is executed.
<% var b=1; select(b){ case 0,1: print("it's small int"); case 2,3: print("it's big int"); default: print("error"); } %>If an exception occurs during the use of the template, you can use try-catch to handle the exception, as shown below:
<% Try {var A = 10; var B = 0; print (A/B);} catch (error) {print ("exception:" + error. message) ;}%>Error indicates an exception. You can use error. Message to obtain possible error information.
You can also omit the catch part. In this case, an exception occurs and no operation is performed.
Beetl template determines whether a variable exists. You can use has and isempty
The simple beetl template operation is here. You need to know other usage instructions. The downloaded package contains the official website documentation.