mapping arraylist to iterator - struts2

How to map an String arraylist to iterator tag in struts 2
for er: public ArrayList list1 = new ArrayList;
this list1 have to be displayed in jsp using s:iterator give me the syntax

Have you looked at the tag reference?
http://struts.apache.org/2.1.6/docs/iterator.html

do something like this:
<s:iterator value="list1">
<s:property/>
</s:iterator>
before posting any question like this do your hand-full exercise first

Related

nesting varstatus into var as index for method in jstl foreach

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.

Struts 2 List of List access

I wonder if someone can help.
Background Architecture:
I have an application which contians many areas which have many specific questions, where these questions contian progress
So at a high-level view there can be many applications. A user is associated to maybe many applications.
So I have a list of applications and I can quite easliy iterate my way through and get its relevant details. But when it comes to accessing each specific area to get question progress for some reason it does not work out form me.
So here is logic I want:
for the applications
display its details
for each area(applicaitonId)
display each its completion progress
so to translate to struts I have this
<s:iterator id="app" value="appList" status="row">
<s:iterator id="qsp" value="questionProgessList[%{#row.index}]" status="status">
so applist is the list of all applications with its own getter/setter && questionSetProgress is defined by:
List<ApplicationQuestionSetProgress> getQuestionProgressList(final int index)
or
List> getQuestionProgressList()
So trying in many ways to access the list has proved futile I have tried:
<s:iterator id="qsp" value="questionProgessList">
<s:property />
</s:iterator>
with this:
List<List<ApplicationQuestionSetProgress>> getQuestionProgressList()
but no joy
I've also tried using the iterator index to call the method but it just does not work out.
List<ApplicationQuestionSetProgress> getQuestionProgressList(final int index)
i.e.
<s:iterator id="qsp" value="questionProgessList[%{#row.index}]" status="status"> or <s:iterator id="qsp" value="questionProgessList(%{#row.index})" status="status">
any hints or tips??
TIA
Considering you have something like
List<ApplicationQuestionSetProgress> getQuestionProgressList(final int index)
Use this
<s:iterator var="qsp" value="questionProgessList[#row.index]" status="status">
You mentioned the following method:
List<List<ApplicationQuestionSetProgress>> getQuestionProgressList()
But you mentioned that the action needed to get a list of "applications" where each contained among its properties a list of "areas".
As such I would name that method:
List Applications getApplications(){
return this.applications;
}
The form of a JSP to extract all properties would be:
<s:iterator value="applications">
<s:property value=applicationsProperty1"/>
<s:property value="applicationsProperty2"/>
...
<s:iterator value="areas">
<s:property value="areasProperty1"/>
<s:property value="areasProperty2"/>
...
<s:iterator value="progress">
<s:property value="progressProperty1"/>
<s:property value="progressProperty2"/>
...
</s:iterator>
</s:iterator>
</s:iterator>

Struts2 checkbox with iterator

I'm Using Struts2 in jsp page i have iterator values for checkbox. how to get the values in action class? Am using javascript to validate the checkboxes. So please help me to solve this problem.
i believe you have checkboxex something like
<s:form action="myaction">
<s:checkbox name="a" fieldValue="ORIGINATOR" value="%{value1}" label="A"/>
<s:checkbox name="a" fieldValue="EVALUATOR" value="%{value2}" label="B"/>
<s:checkbox name="a" fieldValue="EXECUTOR" value="%{value3}" label="C"/>
</s:form>
here is how you will get values in your action class
public class Handler extends ActionSupport{
private String[] a;
public void setA(String[] a){
this.a= a;
}
#Override
public String execute() throws Exception {
// use the checkbox values here
return Action.SUCCESS;
}
}
hope this will help you.
There are two struts tags which can generate checkbox items on the page.
checkboxlist
checkbox
The tag checkboxlist is used to populate checkboxes from a map, list or array. This will generate checkboxes to the number of items in the structure you provide. The list attribute, which is mandatory is used to specify the list or map or array. When the form gets submitted, to get the selected items in your action object, map it using attribute name. i.e. to get the selected items in action, specify the name attribute, define an array or list with the same name in action class, provide the getter and setter for the array or list. You can refer the discussion below to know more:
http://www.mkyong.com/struts2/struts-2-scheckboxlist-multiple-check-boxes-example/
The checkbox tag will generate a single checkbox item on your page. It is comparatively simple and easy to implement. Please refer the link below to understand how it works:
http://www.mkyong.com/struts2/struts-2-scheckbox-checkbox-example/
And, to validate the checkbox items, you can implement the logic as you handle the basic html + javascript. You may use cssClass attribute to specify the html class while using checkboxlist and then use jQuery select by class to get the items and validate.

Struts 2: s:if inside s:iterator not working

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

how to set a value to <s:hidden > from the action class without using list and iterator in struts 2?

In action class i am reading value from the database.
Let's say "abc".Now the "abc" value should be populated to jsp page.i.e "abc" value should set to s:hidden field in the jsp page.
Since it is single value , i don't want to use List in the action class.
Is there any other way to do that ?
Why would you want to use a list? Just provide the appropriate getter like:
public Object getAbc(){
return abc;
}
and in your page access it with simple OGNL expression like:
<s:hidden name="filedYouWantToSetThisValueTo" value="%{abc}"/>
Hope I got it right.

Resources