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

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?

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.

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();" />

Related

Adding a conditional in gsp file

I have a situation where I get a value as a string and want to do a <= check on it inside my Grails GSP file.
For example.
<g:set var="dueAmount" value="${bean.dueAmount}"/>
<span class="pay-onetime-btn-wrapper ${dueAmount <=0 ?'show':'hide'}" >bla bla </span>
I get the following error.
java.lang.Integer cannot be cast to java.lang.String
Which makes sense as the bean.dueAmount is a string. How can I format it as a number or be able to a <= value comparison on it?
Thanks
Looks like your value is a double or floating value. So you can use toDouble() instead. Also, call toString() for the safe side before it.
<g:set var="dueAmount" value="${bean.dueAmount.toString().toDouble()}" />
<span class="pay-onetime-btn-wrapper ${dueAmount <=0 ? 'show' : 'hide'}">bla bla</span>
Grails has a toInteger() function
<g:set var="dueAmount" value="${bean.dueAmount.toInteger()}"/>
<span class="pay-onetime-btn-wrapper ${dueAmount <=0 ?'show':'hide'}" >bla bla </span>

Grails error when attempting to divide variables in GSP

I have a page where I'm trying to divide two variables and set the result to a new variable. I can add, subtract, and multiply them, but if I try and divide them I get an error saying "Division undefined."
I'm taking existing variables (totalProgressGoal and totalPrincGoal), fetching values from the db, and then adding them to the variables.
<g:set var="gain01" value="${pdd?.dataValue}" />
<g:set var="goal01" value="${pdd?.groupGoal.goalValue}" />
<g:set var="totalProgressGoal" value="${totalProgressGoal + gain01}" />
<g:set var="totalPrincGoal" value="${totalPrincGoal + goal01}" />
If I try and take the updated variables and divide them, then I get the error.
<g:set var="goalSuccessRatio" value="${totalProgressGoal / totalPrincGoal}"/>
Replacing the "/" with any other operator works, and if I set totalProgressGoal and totalPrincGoal with numeric values it works. What do I need to do to divide these values?
Clarification: The variables are set above as integers:
<g:set var="totalProgressGoal" value="${0}" />
<g:set var="totalPrincGoal" value="${0}" />
I suspect these variables are not actually of type Integer as you claim. Does casting them to an Integer work, e.g.
<g:set var="goalSuccessRatio"
value="${(Integer) totalProgressGoal / (Integer) totalPrincGoal}"/>

JSTL c:set and Struts s:set are undesirably formatting numbers

<c:set var="xmlDocumentId" value="${id}" scope="request" />
<s:set var="xmlDocumentId" value="%{id}" scope="request" />
are formatting id based on locale, setting xmlDocumentId to "12,345" while:
<c:out value="${id}" />
<s:property value="%{id}" />
are outputting "12345".
Any ideas how to break this behavior?
Because you are getting value with getText or <s:text> tag your long value gets formatted according to locale. To prevent this from happening convert your long to string.
With <s:set> tag you can call toString() method directly in value attribute.
<s:set var="xmlDocumentId" value="id.toString()" scope="request" />
For the concrete formatting algorithm take a look at java.text.MessageFormat class and its subformat method.
Once you know how to format numbers in Java, in Struts2 <s:property/> tag you can use getText() to format your number in the desired way, for example :
<s:property value="getText('{0,number,#,##0}',{id})"/>
Try this
<s:text name="id" > <s:param name="value" value="id"/> </s:text>

struts 2: equal symbol expected error

I am trying to set the text in a button to either 'Enable' or 'Submit' depending on the value of a action class member(mode). But it is reporting an error which is saying 'equal symbol expected' on the first line. I searched and found that there are questions regarding 'equal symbol expected' error but nothing specific to Struts 2 tags. Neither I could spot any obvious error as missing closing quotes.
It would be nice if anyone can help.
<s:set name="submitButtonLabel" value="<s:if test="mode.equals('enable')">Enable</s:if> <s:else>Submit</s:else>" />
<s:submit value = "%{#submitButtonLabel}" cssClass="btn btn-gray" />
Try this:
<s:submit value="%{mode.equals('enable') ? 'Enable' : 'Submit'}" />
You cannot nest tags like that. Write your <s:if> inside <s:set> tag instead.
<s:set name="submitButtonLabel">
<s:if test="mode.equals('enable')">Enable</s:if>
<s:else>Submit</s:else>
</s:set>

Grails <g:if> in <g:select>

I have this <g:select> in a .gsp file. But unlike any ordinary <g:select>'s this one would have the attribute disabled="" if a certain condition is met.
Following the code:
<g:select name="test"
from="${["foo1","foo2"]}"
<g:if test="${true}">disabled=""</g:if> />
It returned an error: Grails tag [g:select] was not closed
But when I change it into this:
<g:select name="test"
from="${["mu1","mu2","mu3"]}"
${ if(true) { println "disabled=\"\"" } }/>
It returned this error: Attribute value must be quoted.
Both of the error message are under the exception, org.codehaus.groovy.grails.web.taglib.exceptions.GrailsTagException
The question is how could we make this work? Is there a possible answer without using a custom TagLib?
The GSP form field tags treat disabled as a boolean property, so you can say
<g:select .... disabled="${true}" />
Generally you should be able to use any expression under the usual Groovy-truth rules but I believe it makes a special case for the strings "true" and "false" (the latter would normally be considered true under Groovy-truth rules as a non-empty string). If in doubt you can always say
disabled="${(someExpression) as boolean}"
No need to use the println, try this
<g:select .... ${(conditional)?"disabled":""} ... />
<g:select disabled="${true}"...
is fine but when you submit and it is a required field the value will not be submitted so use this jQuery code to enable the field when pressing the submit button
$(function() {
$('form').on('submit', function() {
$(this).find(':disabled').removeAttr('disabled');
});
});

Resources