Grails GSP <g:set> tag set as integer?

23,469

Solution 1

Use the ${} syntax when defining the value. For example:

<g:set var="x" value="${100}"/>

You can see the tag doc for g:set for more info.

Solution 2

Just as an additional comment for someone who comes across this since it is the only useful result on the Internet for and casting/Int/Sring/etc. This example works in the case of variables:

<g:set var="printLeft" value="${offer?.metaInfo?.redeemPrintY as Integer}"/>
<g:set var="printTop" value="${offer?.metaInfo?.redeemPrintX as Integer}"/>
<g:set var="printWidth" value="${offer?.metaInfo?.redeemPrintW as Integer}"/>
<g:set var="printHeight" value="${offer?.metaInfo?.redeemPrintH as Integer}"/>

...

<area shape="rect" coords="${printLeft},${printTop},${printLeft+printWidth},${printTop+printHeight}" onClick="printOffer();" />
Share:
23,469
Steve Kuo
Author by

Steve Kuo

Software, pilot, travel, life http://www.linkedin.com/in/stevekuo1

Updated on July 09, 2022

Comments

  • Steve Kuo
    Steve Kuo almost 2 years

    Using Grails' GSP <g:set> tag, is it possible to specify the type of the variable? I want to declare an integer variable, but <g:set> always declares a sting. For example:

    <g:set var="x" value="100"/>
    ${x.getClass()}
    ${x+23}
    

    results in

    class java.lang.String
    10023
    

    I'd like to declare x as an integer. I noticed that using the JSP tag <% int x=100; %> results in:

    class java.lang.Integer
    123
    

    Is there a way to do this the Grails/GSP way?

  • Alexander Suraphel
    Alexander Suraphel over 10 years
    Is is the preferred way to set variables than doing <% x = 100 %>?