Grails error when attempting to divide variables in GSP - grails

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}"/>

Related

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>

How to use fn:replace() to replace one doublequote by two doublequotes

In my JSF application I am trying to use fn:replace() to replace " by "". I tried the following:
<h:outputText value="#{fn:replace(str, '\"', '\"\"')}" />
However, it causes a XML parsing exception:
javax.faces.view.facelets.FaceletException: Error Parsing /test.xhtml: Error Traced[line: 20] Element type "h:outputText" must be followed by either attribute specifications, ">" or "/>".
The same code is working for other charecters like the below one:
<h:outputText value="#{fn:replace(str, 'a', 'b')}" />
How can I replace a doublequote by two doublequotes using fn:replace()?
Indeed, this is nasty. The " has special treatment in XML. It represents the start and end of an attribute value. Your best bet is to parameterize it into another variable with help of <c:set>.
<c:set var="doublequote" value='"' />
<c:set var="twodoublequotes" value='""' />
<h:outputText value="#{fn:replace(bean.string, doublequote, twodoublequotes)}" />

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');
});
});

Inner evaluation in EL 2.2

I have some code as follows:
<f:loadBundle basename="messages.application" var="prop" />
<ui:param name="currentUserAttr" value="#{prop['currentUser']}" />
// currentUserAttr=currentUserVal
<h1>Welcome #{sessionScope.currentUserAttr.name}</h1> // should be evaludated to sessionScope.currentUserVal.name
I want to get the value of currentUserAttr then when reading from session replacing the attr with its value then getting the value from session, any ideas how to do that in EL 2.2?
Use the brace notation as well if you want to use a dynamic key.
<h1>Welcome #{sessionScope[currentUserAttr].name}</h1>

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

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

Resources