Adding a conditional in gsp file - grails

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>

Related

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>

Counters in Loops in Thymeleaf

Is there a way to do a loop in Thymeleaf without a list?
I'd like to essentially convert this snippet to Thymeleaf:
<jsp:useBean id="now" class="java.util.Date" />
<fmt:formatDate var="year" value="${now}" pattern="yyyy" />
<c:forEach var="i" begin="0" end="99">
<form:option value="${year-i}" />
</c:forEach>
</form:select>
-- Update --
I've decided this is along the lines of how I want to do it, but I'm not sure about the springEL syntax:
<option th:each="i : ${#numbers.sequence( 1, 100)}" th:value="#{ T(java.util.Date).getYear() - $i }">1</option>
In case you are still looking for the correct SpEL syntax,
here's what worked for me:
<option th:each="i : ${#numbers.sequence( 1, 100)}"
th:value="${ (new org.joda.time.DateTime()).getYear() - i }"
th:text="${ (new org.joda.time.DateTime()).getYear() - i }">1</option>
Notice:
added th:text to set the option text.
used Joda-Time instead as java.util.Date wouldn't give me the desired outcome
Read this discussion on java.util.Date and getYear()
You can use the special thymleaf iteration variable inside the each block.
This special variable name is the name of your element variable concatenate with the keyword 'Stat' (ex: elt -> eltStat)
This variable gives you many information related to the iteration.
You can also specify this variable name after your element variable. For example:
<tr th:each="prod,iterStat : ${prods}" th:class="${iterStat.odd}? 'odd'">
<td th:text="${prod.name}">Onions</td>
<td th:text="${prod.price}">2.41</td>
<td th:text="${prod.inStock}? #{true} : #{false}">yes</td>
</tr>
More information in the official documentation below:
http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#keeping-iteration-status

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

Struts 2 ApplicationResources.properties error

I am using struts2 for my application.
<s:submit cssClass="button" key="btn.search" tabindex="12" />
in ApplicationResources.properties file i have
btn.search = Go
and i am getting error while submitting the page,
OgnlValueStac W com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
Error setting expression 'btn.search' with value '[Ljava.lang.String;#14f414f4'
ognl.OgnlException: target is null for setProperty(null, "search", [Ljava.lang.String;#14f414f4)
What's the problem?
The key attribute is a shorthand for both the name and value attributes.
Using key means you're assuming a property named btn.search.
While you may set a value to the results of a text property lookup, you can also use it directly:
<s:submit value="%{getText('btn.search')}" />
The key is being submitted as a parameter and OGNL is trying to get an object named btn from the ValueStack to set the parameter by calling getBtn().setSearch("");, but, since you have no btn object in the stack, the null target exception is occurring.
This should work:
<s:set name="buttonText"><s:text name="btn.search"/></s:set>
<s:submit cssClass="button" value="%{#buttonText}" tabindex="12"/>
Or, as Dave has pointed out in his answer, this should work as well if your action class extends ActionSupport:
<s:submit cssClass="button" value="%{getText('btn.search')}" tabindex="12"/>

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