Problem with variable inside gsp - grails

I have a loop inside a gsp page, and I want to do a calculation on the fly, so I set a variable:
<g:set var="total" value="0" />
And in the loop:
<g:each in="${mob}" var="m">
...
<g:set var="total" value="${total+(m.q*m.sts.uf)}"/>
...
</g:each>
The "total" value does not correspond to the expected calculation. m.q is an integer, and m.sts.uf is a float.
Any hints ?
Thanks.

What does total end up being?
It could be because total is being initialised as a String.
Try
<g:set var="total" value="${0l}" />

Related

Passing values to input through g:each iteration

Say if i have this data from my controller that i want to pass it to my GSP
def test = [value: ['TEST1', 'TEST2', 'TEST3']]
And in my GSP, i have the following
<g:set var="counter" value="${0}"/>
<g:set var="count" value="${count}"/>
<g:while test="${counter < count}">
<g:set var="counter" value="${counter+1}"/>
<g:each var='obj' in='${test}' status='i'>
<g:textField id='justATest1' name='justATest' value='obj.value'>
<g:textField id='justATest2' name='justATest' value='obj.value'>
<g:textField id='justATest3' name='justATest' value='obj.value'>
</g:each>
</g:while>
And i want it so that justATest1 will get TEST1, justATest2 will get TEST2 and justATest3 will get TEST3.
How can i achieve that?
Assuming your model contains the following
def myAction() {
[value: ['TEST1', 'TEST2', 'TEST3']]
}
This should do it
<g:each status="i" in="${value}" var="item">
<g:textField id="justATest${i}" name='justATest' value="${item}">
</g:each>
Ok seems i managed to iterate object in GSP
<g:each var='obj' in='${test}'>
<g:each var='subObj' in='${obj.value}'>
<p>${subObj}</p>
</g:each>
</g:each>
But kind of not elegant enough.
I can do it too with this, as what Donal put in his answer
<g:each var='obj' in='${test.value}'>
<p>${obj}</p>
</g:each>
But it would be hard i think if i want to iterate each item inside test object separately and uniquely.
Anyone please feel free to fix my approach.

setting the value of datePicker to a variable

in Grails 2.3.4 i need to set the value that comes from the datePicker
<g:datePicker name="toDate" precision="day" relativeYears="[0..10]" default="${new Date()}" />
to a variable for instance :
<g:set var="fromDate" />
how can i do that ?
Thanks
You can use the <g:set /> tag to set the variable value, then assign the variable to the datePicker using the value attribute.
<g:set var="fromDate" value="${new Date()}" />
<g:datePicker name="toDate" precision="day" value="${fromDate}" />
Here is the documentation.

Null param value on form submit grails

I have one checkbox(id, name:checkedEx) in grails gsp list and have sortableColumn where I pass checkbox value in params map. Now when I click any of the sortable column, the checkbox gets checked, as the query string gets the value localhost:8080/api-name/list.gsp?checkedEx=null (this null value sets the checkbox)
<g:checkBox name="checkedEx" id="checkedEx" value="${checkedEx}" onclick="this.form.submit();" />
<g:sortableColumn property="date" title="date" params='["checkedEx":"${checkedEx}"]' />
How to check if the value of checkedEx is null , then in sortableColumn send param checkedEx as empty.
Thanks!
You can check it on the same line like this:
<g:sortableColumn ... params='["checkedEx":"${checkedEx?checkedEx:''}"]' />
or use a
<g:if> tag and <g:set> if you want to do more than use the operator

struts2 TreeMap: keys with spaces not appearing in action

JSP Code:
<s:iterator value="#currentRequisitionGroup.plFldWrap.allPFields" var="pMap" status="hStatus">
<s:iterator value="#pMap.value.paramMdlList" var="paramModel" status="fStat">
<li>
<label>
<s:property value="#paramModel.parameterName" />
</label>
<s:set var="cEdit" value="%{#paramModel.isEditable}"> </s:set>
<s:if test="%{#cEdit == true}">
<s:textfield id="paramId_%{#paramModel.parameterId}" name="rqPGPrmMdl.rqGrp[% {#cGStat.index}].plFldWrap.allPFields['%{(#pMap.key)}'].paramMdlList[%{#fStat.index}].prmValue"/>
</s:if>
<s:else>
<s:textfield id="paramId_%{#paramModel.parameterId}" name="rqPGPrmMdl.rqGrp[% {#cGStat.index}].plFldWrap.allPFields['%{(#pMap.key)}'].paramMdlList[%{#fStat.index}].prmValue" readonly="true"/>
</s:else>
</li>
<s:hidden id="prmId_%{#paramModel.parameterId}" name="rqPGPrmMdl.rqGrp[%{#cGStat.index}].plFldWrap.allPFields['%{(#pMap.key)}'].paramMdlList[%{#fStat.index}].parameterId"></s:hidden>
<s:hidden id="paramName_%{#paramModel.parameterId}" name="rqPGPrmMdl.rqGrp[%{#cGStat.index}].plFldWrap.allPFields['%{(#pMap.key)}'].paramMdlList[%{#fStat.index}].parameterName"></s:hidden>
<s:hidden id="pGId_%{#paramModel.parameterId}" name="rqPGPrmMdl.rqGrp[%{#cGStat.index}].plFldWrap.allPFields['%{(#pMap.key)}'].paramMdlList[%{#fStat.index}].parameterGroupId"></s:hidden>
<s:hidden id="seqNo_%{#paramModel.parameterId}" name="rqPGPrmMdl.rqGrp[%{#cGStat.index}].plFldWrap.allPFields['%{(#pMap.key)}'].paramMdlList[%{#fStat.index}].sequenceNumber"></s:hidden>
</s:iterator>
</s:iterator>
The problem is that some values do not appear in action.
Investigation till now indicates that, if the corresponding html input has space in the key of allPFields, that value doesn't appear in action.
<input id="prmId_30" type="hidden" value="30"
name="rqPGPrmMdl.rqGrp[1].plFldWrap.allPFields['Emp System'].paramMdlList[0].parameterId">
However, if the corresponding html input has no space in the key of allPFields, that value appears in action.
<input id="prmId_46" type="hidden" value="30" name="rqPGPrmMdl.rqGrp[1].plFldWrap.allPFields['Emp'].paramMdlList[0].parameterId">
Here is what the logs say:
xwork2.interceptor.ParametersInterceptor - Parameter [rqPGPrmMdl.rqGrp[1].plFldWrap.allPFields['Emp System'].paramMdlList[0].parameterId] **didn't match acceptedPattern pattern!**
This is happening because white spaces are not accepted in parameter names. You can change acceptParamNames parameter of the ParametersInterceptor, BUT as the documentation states
acceptParamNames - a comma delimited list of regular expressions to describe a whitelist of accepted parameter names. Don't change the default unless you know what you are doing in terms of security implications
So I suggest you to get rid of white spaces in parameters names.

Get an array value with dynamic index in struts2

I'm trying to get a value from an array into a s:textbox element:
it works if I take a hard coded index like:
<s:textarea value="%{languageHelper.myHauptuebbeschr[0]}" />
but when I try to use a dynamic index it doesn'T work:
<s:textarea value="%{languageHelper.myHauptuebbeschr[attr.number]}" />
Number is not empty, I can get the value via
<s:property value="%{#attr.number}" />
So how can I use the number variable as index for my array?
shouldn't it be:
<s:textarea value="%{languageHelper.myHauptuebbeschr[#attr.number]}" />
I found a solution:
I first save the value in an addtional variable:
<c:set var="myHauptuebbeschr" value="${languageHelper.myHauptuebbeschr[number]}" ></c:set>
and then get it via:
<s:textarea value="%{#attr.myHauptuebbeschr}" />

Resources