Grails Development platform In view technology, my favorite technology, in addition to the introduction of flash variables, is template technology.
The most convenient reuse technology for the Java platform in view is include. Although tiles and so on are very powerful, but because of the cumbersome configuration, so that they as the entire project layout outside, almost no application. Include is convenient to use, but it has a natural weakness in the value-passing aspect.
The template technology of the Grails platform includes the convenience of include, the problem without configuration, and the strong value of the lack of include.
Let's give a few examples to illustrate the problem.
I have a bunch of code below the test page:
<input type= "button" value= "Report3" onclick= "window.location.href= ' <g:createlink controller=" The "action=" Bar3 "/>" ></INPUT>
<input type= "button" value= "Report4" onclick= " window.location.href= ' <g:createlink controller= ' "action=" Bar4 "/>" ></INPUT>
<input Type= "button" value= "Pie1" onclick= "window.location.href= ' <g:createlink controller=" "" action= "Pie1"/> " "></input>
<input type=" button "value=" Pie2 "onclick=" window.location.href= "<g:createlink Controller= "action=" pie2 "/> '" ></INPUT>
<input type= button "value=" Pie3 "onclick=" window.location.href= ' <g:createlink controller= ' "action=" Pie3 "/>" ></INPUT>
<input Type= "button" value= "XY" onclick= "window.location.href= ' <g:createlink controller=" "" action= "xy"/> " ></input>
<input type= "button" value= "Scatter" onclick= "window.location.href=" <g:createlink ControlLer= "action=" scatter "/> '" ></INPUT>
<input type= button "value=" Water "onclick=" window.location.href= ' <g:createlink controller= ', "action=" Water "/>" ></INPUT>
There are more than 10 lines of the same button statement, although you can use the "copy" and "Paste" method to quickly finish the code, but the line of code length is too long to exceed the length of the display, making maintenance quite difficult.
So I want to use template to reduce the length of lines of code, so that the code is in line of sight, so easy to maintain. The following is the process of making the template:
We often say that the project of Grails platform uses the contract type programming, really is a well-deserved reputation. The contract is also used on the template, in general, the GSP file for the filename beginning with the underscore is template. So I named my first template as _TEST.GSP:
<input type="button" value="${value}" onclick="window.location.href='<g:createLink controller="${control}" action="${action}" />'"></input>
You can see whether the content of the template is simple and contains three variables: value, control, and action.
How are we going to use this template?
In fact, it's simple:
<g:render template="test" model="[value:'nulti',control:'report',action:'multi']"/>
The first is the use of <g:render> tags, template to remove the underlined template file name, that is, test, the final pass in model template required three parameters.
It's really simple. Let me give you an example.
We often need to catch some error messages at the beginning of each face, as follows:
<g:if test="${flash.error}">
<div class="errors">${flash.error}</div>
</g:if>