How to concatenate 2 action class variables in Struts 2? - struts2

I have 2 variable in my action class, id1 and id2. Joined by a _, they're used as a map key.
I am not able to retrieve the map value using this code:
<s:property value="%{mymap[id1_id2]}" />
How should I retrieve the map value?

The expression id1_id2 in OGNL will assume the presence of a variable named id1_id2, since it's a perfectly legal identifier.
If you want to concatenate strings, you'd need:
<s:property value="%{mymap[id1 + '_' + id2]}" />
I'd likely create a separate variable to use as the key:
<s:set var="mapKey" value="%{id1 + '_' + id2}" />
<s:property value="%{mymap[#mapKey]}" />
Or more likely, I'd do it somewhere besides the view layer.

Related

Numbers not formatted properly in Struts 2

I am using following syntax to display a value in a proper number format, e.g. 1,250.00.
<s:property value="%{getText('{0,number,#,##0.00}',#plan.amount)}" />
However, it is not working. The plan is an object with a property amount.
First of all, if you want to display multiple values from a list, you need an iterator;
second, if plan is a list in the action with a getter method
public List<Something> getPlan() { return plan; }
then you don't have to put the # ahead of the variable.
The right code for your case would be:
<s:iterator value="plan">
<s:property value="getText('{0,number,#,##0.00}',{amount})" />
</s:iterator>
There's a related Q&A on the topic.
EDIT
Since you have
<s:iterator value="list" var="plan" status="status">
<div class="values">
$ <s:property value="%{getText('{0,number,#,##0.00}',#plan.amount)}"/>
</div>
</s:iterator>
Then it should be:
<s:iterator value="list">
<s:property value="getText('{0,number,#,##0.00}',{amount})" />
</s:iterator>
If the value is printed like 1250.00 then it's not formatted properly. The method getText() has many overloaded methods and which method is used is determined by the parameter's types and count.
To pass arguments to the getText() method you can use OGNL list construction {}. And the arguments should be listed as single values, not "list of object". Perfectly it should be list of Double with one element inside the list.
<s:set var="amount" value="%{1250.0}"/>
<s:property value="%{getText('{0,number,#,##0.00}',{#amount})}" />

Keep attribute class consistent in TagLibs

I am using a custom TagLib to format dates. From my view, I am passing a domain field into the TagLib like so:
<g:usDate value="${invoice.invoiceDate}" />
In this case the value goes into the TagLib with a type of Date. However, when I try to use the TagLib to format the contents of a field:
<g:textField name="invoiceDate" value="${g.usDate(value:"${invoice.invoiceDate}")}" />
the value goes into the TagLib as a type of GStringImpl. Is there a different syntax I can be using on the textField that passes the value as a Date so that I can use the same TagLib for both cases?
Remove the inner quotes and ${}:
<g:textField name="invoiceDate" value="${g.usDate(value:invoice.invoiceDate)}" />
This will pass the actual object invoice.invoiceDate to the taglib, rather than creating a GString containing the string representation of the object.
It's a little confusing but you need to remember that ${} means different things in different places. As a tag attribute it says "the content inside the braces is a Groovy expression, evaluate it and pass the resulting value directly to the tag". But once you're inside a Groovy expression it has the normal GString meaning of "evaluate this expression and insert its toString representation into the GString".

struts 2 assigning value of property tag to hidden field

I want to assign the value from field Description to a hidden field test.
But the problem is the "Description" contains sequence of words and the following code is assigning only first word to "test"
<s:hidden value=<s:property value="Description" /> name="test">
I am kind of new to struts. Can someone please help.
Also it would be nice if i get to know good tutorial links of struts2.
If this is a property in your action class you need not to use <s:property value="Description" /> as the Description will be available at the top of value stack and you can use OGNL to fetch the value from value-stack.This is what you need to do
<s:hidden value="%{description}" name="test" />
Please make sure the value in hidden filed should be similar to the name of property in your action class as it will be resolved to either the getter and setter in your action class or the public property defined in your action.
So this means value="%{description}" will be converted by OGNL like getDescription() and will try to find the getter in your action class to fetch the property value.
<s:hidden value="%{description}" name="test" />

GSP- Select tag. How to achive selected="selected"

I have a select tag in my GSP file as
<g:select name="clientId" id="clientId" size = "4" from="${com.springcommunity.fleet.partymodel.roles.ClientRole.list()}" class = "filter_combo" optionKey="id" />
i want client with id 2 is selected initially (in simple html it is achived by using selected="selected")
how can i do it?
You need to specify the value attribute in this tag. http://grails.org/doc/2.0.x/ref/Tags/select.html
So in your example,
<g:select ... value="${com.springcommunity.fleet.partymodel.roles.ClientRole.get(2)}" />
One thing to be aware of here is that the value that you're selecting must be an object equal to the item in the list, and not just an id - this is where a lot of people get tripped up. So you can't just say value='2', you need to specify the object in the list that you have in your from attribute.
From the docs -
value (optional) - The current selected value that evaluates equals()
to true for one of the elements in the from list.

struts2 ognl retrieve data from session (nested property tags?)

I have a situation where I would like to retrieve data from the session on a jsp using OGNL. The data in my session is stored like this:
/data/abc/-Name (key) -> ABC Inc. (value)
I can easily retrieve this from the session by doing
<s:property value="#session['/data/abc/-Name']"/>
But unfortunately, the string '/data/abc/' is a dynamic one and is stored in my action under the variable companyFolder.
How do I use this variable to get the data from the session.. something like
<s:property value="#session['%{companyFolder}-Name']"/> // this didnt work
<s:property value="#session['<s:property value="%{companyFolder}"/>-Name']"/> // this didnt work
<s:property value="#session[companyFolder + '-Name']" />
I would likely do this in the action, though, using SessionAware. This makes things easier to test, and avoids executing the JSP to see if things work.

Resources