Get an array value with dynamic index in struts2 - 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}" />

Related

How to automatically populate value from action class to the JSP TextBox

I am able to print value from Java Action Class to JSP Page without textbox, But when I want to put that value in a TextBox in JSP, its not coming.
My Code is :
<S:form>
<s:textfield name="toDate" key="td" > <s:property value="fromDate"/> </s:textfield>
<s:textfield name="fromDate" key="fd"> <s:property value="toDate" /> </s:textfield>
<s:submit value="Confirm"></s:submit>
</s:form>
toDate and fromDate is variables in Java Class.
Just dive in the Struts2 UI Tag documentation:
key : Set the key (name, value, label) for this particular component
If you want them different (eg. toDate the value to set, td the value to read, and so on), specify all of them:
<s:form>
<s:textfield name="toDate" value="td" label="To date" />
<s:textfield name="fromDate" value="fd" label="From date" />
<s:submit value="Confirm" />
</s:form>
(Obviously according to your getters and setters).
You can set value in TextBox using value attribute of <s:textfield> tag to value come from action class.
For this you have your getter and setter method for that variable in action class and read that value by below code:
For Example :
<s:textfield name="some_name" value="%{variable}"/>
your code look like this:
<s:form>
<s:textfield name="toDate" value="%{fromDate}" />
<s:textfield name="fromDate" value="%{toDate}" />
<s:submit value="Confirm" />
</s:form>

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

Use Struts2 property tag in DisplayTags

I'm trying to render a table using DisplayTag. Everything works normally when I render a column in the usual manner:
<display:column property="id_material" title="ID" />
Now, i want one column to show 2 properties concatenated. So I tried using the < s:property > tag:
<display:column title="UNIDAD"><s:property value="property1"/> <s:property value="property2"/></display:column>
But no value is showed in the column. Is there an alternative way to do this?
You can use a decorator, get the row object properties and return the concatenation of both.
A quick example:
public class ExampleDecorator extends TableDecorator{
public String getConcatenatedProperties(){
Object object = getCurrentRowObject();
return object.getProperty1() + " " + getProperty2();
}}
Then you add a column in the display table:
<display:column property="concatenatedProperties" title="Properties" />
To use the decorator in the displaytag:
<display:table [..] decorator="org.example.ExampleDecorator" [...] >
Try to add uid attribute in table tag and then you can access current row in Struts2 via request attribute. See following code:
<display:table name="list" uid="row">
<display:column title="UNIDAD"><s:property value="#attr.row.property1"/> <s:property value="#attr.row.property2"/>
</display:column>
</display:table>

Problem with variable inside gsp

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

Resources