The template of grails exploration

Source: Internet
Author: User
Tags grails

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>

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.