I have a question regarding struts 2 and s:if tags. As per described on the documentation of s:if the following example was supposed to work:
...
<s:iterator value="questao.alternativas" status="alternativa">
<tr>
<td>
<s:if test="#alternativa.correta == true">Correta!</s:if>
</td>
</tr>
</s:iterator>
...
But this is not working on my case, could you please help? More details:
questao.alternativas is found on the action and it works fine, all "alternativas" are "iterated"!
the getCorreta() method is never call on the example described above.
when using the value true is printed.
Any ideas??
Thanks!
Specifying status in iterator tag will push an instance of IteratorStatus on value stack. You need to consult the documentation of IteratorStatus for valid attributes to use and correta is not a valid attribute. If you want to access the current iteration's object specify a var attribute instead.
Edit for comment:
That's right as Quaternion said you don't need to specify var for accessing current iteration's object. It's already on top of value stack.
Quickly looking at your code I see that you are referencing the status in your conditional(if) statement. Your conditional should directly reference the accessor method of the object in the list your are iterating through, for example:
<s:iterator value="questao.alternativas" status="stat">
<tr>
<td>
// If correta is a boolean value, there is not need to use an
// == operator at all
<s:if test="%{correta}">
Correta!
</s:if>
</td>
</tr>
</s:iterator>
When an iterator traverses a supplied list, assuming questao.alternativas is a list of objects with accessor methods, you directly reference the methods inside your iterator.
The status in an iterator gives you access to the iterator's position. For example, if you want to determine if your loop is at the last element in the list to perform something special you would do something like:
<s:if test="%{#stat.last}">--- END OF LIST ---</s:if>
Or to get the indexed position of the current element in your array or list:
<s:property value=%{#stat.index} />
Related
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})}" />
Is there a way to use varstatus as index (integer param) for var's method in jstl foreach loop in jsp pages?
I want something like this:
<c:forEach items="${pizza1.getFeltetlist()}" var="aktpizza" varStatus="index" >
<tr>
<td>${index.index+1}</td>
<td>${aktpizza.nev}</td>
<td>${aktpizza.ar}</td>
<td>Add</td>
</tr>
</c:forEach>
So getaFeltet(int i) method requires an int parameter and I want to pass the actual index of varstatus. All other fields above are populated correctly. What should be the correct syntax to achieve this?
You can't and don't need to nest ${...} in each other. Just have one.
Add
This may not help your exact case but I've found that if you are trying to get a value in the jsp, this will not work:
${List.get(varStatus.index)}
but this will
${List[varStatus.index]}
In other words, you can't pass the status index into a function call, but you can use it to get an item at that index in a list.
If you can pass the results of aktpizza.getaFeltet() as a list, you should be able to use this technique.
In Grails I'm trying to delete a record from a (HTML) table, I want to achieve this by passing the ID of the object to the controller. So there is a table in HTML and the user clicks "delete" and the item is deleted from the database. (This logic works according to the unit tests, the problem is passing the ID from view -> controller)
I have tried several solutions found on here such as
<g:each in="${recipe}" var="item">
<tr>
<td>
${item.recipeName}
</td>
<td>
${item.people}
</td>
<td>
<g:link controller="UserRecipe" action="delete" params="[id: '${item.id}']" class="class">Info to Display</g:link></tr>
</g:each>
Which doesn't crash but calls the controller correctly, though the value of the id = null. When I manually edit the url and add /4 for example, the ID will be four. Several solutions on here showed how to pass parameters, but the error arrises when I try to pass a parameter which I access using the ${} notation with these solutions, e.g. using createLink and a params list, it will crash the application because it's a nested ${${}} it seems.
You need to change your params to:
params="[id: item.id]"
I need to iterate through List<String>, each element has s:checkbox.
I have defined one list in Action layer to keep selected elements.
Using my code I can submit my form and capture selected values in action layer. But, some of the checkboxes must be pre-checked. I cannot display pre- checked status when loading the page.
Value1 contains list of String objects.
functionCheckBoxList - The list I have defined to keep checked element
While loading, I added some element to functionCheckBoxList that belongs to Value1.
But still does not show pre-checked status in the page.
<s:iterator value="value1" var ="functionName">
<s:checkbox fieldValue="%{#functionName}" name="functionCheckBoxList"
value="%{#functionName}" theme="simple" >
</s:checkbox>
</s:iterator>
Note: I know how to do it using s:checkboxlist but can't here as I need special format in the iteration.
i could find a solution for my problem, i would like to share it with everyone.
<s:iterator value="value1" var ="functionName">
<s:checkbox fieldValue="%{#functionName}" name="functionCheckBoxList"
value="%{#functionName in functionCheckBoxList}" theme="simple" >
</s:checkbox>
</s:iterator>
I am new to struts 2 and am losing my mind over iterating a collection of collections in my JSP using the iterator tag.
My action class exposes a List (parent) which in turn contains 4 more lists (children). Each child list contains 5 domain objects (e.g. User).
In my JSP, i need to display the User.Name after iterating over the collections. I am able to iterate over the parent collection but cannot get to the child lists. They are anonymous lists and are not exposed by a specific name (i.e. a getter is not available).
<s:iterator value="usrList" var="refParent">
<ul>
<s:iterator value="#refParent.columns" var="usr">
<li>
<s:property value="#usr.Name"/>
</li>
</s:iterator>
</ul>
</s:iterator>
The outer iterator results in 4 <ul> tags but each of those tags are empty i.e. none of the <li> tags are displayed.
All the examples that i see are accessing specific named Collections (e.g. User.PhoneList) but none of them seem to demonstrate this particular behavior.
Any help is greatly appreciated
I think i finally figured it out from one of the Iterator-tag examples (thanks to Umesh and Quaternion). I totally missed that one and did not read through the entire example using top.
<s:iterator value="usrList">
<ul>
<s:iterator value="top">
<li>
<s:property value="Name"/>
</li>
</s:iterator>
</ul>
</s:iterator>
Please try this example from this link
<table>
<s:iterator value="allCities" id="cities">
<s:iterator value="#city.phoneNumbers" id="number">
<tr>
<td>
<!-- Accessing the city name through the city variable -->
<s:property value="#city.name" />
</td>
<td>
<!-- Accessing the objects inside the phone number object through the number variable -->
<s:property value="#number.prefix" />
</td>
<td>
<s:property value="#number.rate" />
</td>
</tr>
</s:iterator>
</s:iterator>
</table>
Struts2 Iterator tag a simple enough and work in a very simple manner.So if you have Collection of Collection or what ever structure think how you will iterator them in a simple Java application , same will apply for the S2 iterator tag, but at same time it will give you more flexibility as well configurations.
In Short you need nested iterator tag.When S2 iterator tag iterate objects it will place current object on the top of value stack and you can easily access that tag in your JSP code.
Since in your code Top level object will be a Collection you need to iterate that again like
<s:iterator value="firstCollection">
<s:iterator value="firstCollection>
/ do what you want here
</iterator>
Since while iterating object will be at top of stack you can refer to that tag directly without any reference.
i Suggest you to have look at following official documents
iterator
iterator-tag-examples