how to use <c:forEach > for loop String - foreach

if array data is
String sampdata[]= new String [22];
sampdata[0] ="a";,sampdata[1] ="b";
sampdata[2] ="c";,sampdata[3] ="d";
sampdata[4] ="e";,sampdata[5] ="f";
sampdata[6] ="g";,sampdata[7] ="h";
sampdata[8] ="i";,sampdata[9] ="j";
sampdata[10] ="k";,sampdata[11] ="l";
sampdata[12] ="m";,sampdata[13] ="n";sampdata[14] ="o";
sampdata[15] ="m";,sampdata[16] ="n";sampdata[174] ="o";
sampdata[18] ="m";,sampdata[19] ="n";sampdata[20] ="o";
sampdata[21] ="m";
and jsp use jstl foreach
<c:forEach var="list" items="${sampdata" begin="0" end="9" step="2" varStatus="status">
<tr>
<td>${list}</td> //a
<td>${list}</td> //td : b
<td></td>
</tr>
</c:forEach>
i mean
check this java array
for (int i=0 ; i<9;i +=2){
System.out.println(i)
System.out.println(i+1) <- i want like this.
}
i want "td:b" view b.
how to fix that?
"foreach" can use like that?

Check documentation on loop scoped variable with nested visibility that
lets authors use the status object to obtain information about the iteration range, step, and current object.
<c:forEach items="${sampdata}" step="2" varStatus="loop">
<c:out value="${sampdata[loop.index]}"/>
<c:out value="${sampdata[loop.index+1]}"/>
</c:forEach>

Related

How does a filtered table work in PrimeFaces?

I am trying to implement my own filtered table and I am struggling with understanding how it exactly works.
Redueced example from the PrimeFaces showcase:
<h:form>
<p:dataTable var="car" value="#{dtFilterView.cars}" widgetVar="carsTable"
emptyMessage="No cars found with given criteria" filteredValue="#{dtFilterView.filteredCars}">
<p:column filterBy="#{car.price}" headerText="Price" footerText="custom (min)" filterFunction="#{dtFilterView.filterByPrice}">
<h:outputText value="#{car.price}">
<f:convertNumber currencySymbol="$" type="currency"/>
</h:outputText>
</p:column>
</p:dataTable>
My main problems with understanding how it works are:
The filter-function is called via EL without parameters, whereas the filter-function in the managed-bean needs Object value, Object filter, Locale locale
The data-table points to two collections. First the actual values without filtering and second an own collection for filtered values.
Code of the filter-function form the showcase:
public boolean filterByPrice(Object value, Object filter, Locale locale) {
String filterText = (filter == null) ? null : filter.toString().trim();
if(filterText == null||filterText.equals("")) {
return true;
}
if(value == null) {
return false;
}
return ((Comparable) value).compareTo(Integer.valueOf(filterText)) > 0;
}
QUESTION 1: How does the filter-function from the managed-bean gets the parameters it needs ?
QUESTION 2: Why does it need Locale locale ?
QUESTION 3: When and where does it write to the collection which contains the filtered values ?

Split string in GSP grails

How to split a string and get first words?
studentController.groovy
def student = {
def ex = new ArrayList()
ex[0]= "Steven | ABCDEF0123456"
ex[1]= "Steven | ABCDEF0123456"
//student's value
[studentlist:ex] //send to gsp
}
student.gsp
<g:each in="${studentlist}" status="i" var="stdnt">
<tr class="${(i % 2) == 0 ? 'odd' : 'even'}">
<td>${stdnt}</td> //i want this show "Steven"
<td>${stdnt}</td> // and this show ABCDEF0123456
</tr>
</g:each>
How can I get the first word and second word?
You want Java's String.split() method:
List ex = "Steven | ABCDEF0123456".split('\\|')*.trim()
[studentlist:ex] //send to gsp
If you know there are only two items in the list, then you can reference them via studentlist[0] and studentlist[1]. It's a little unclear what you expect from the controller currently (a list of lists?):
<td>${studentlist[0]}</td>
<td>${studentlist[1]}</td>

JSTL Conditional Operator with variable

While working on jstl tags I came across foreach loop:
<c:forEach items="#{data.steps}" var="item" varStatus="status">
<li>#{item}</li>
</c:forEach>
How can I replace the static '1' (status.index == '1') with an variable? Should look like: status.index == '1' == #{data.step}?
Let say you want to replace constant '1' with a variable name 'step' existing in page/request/session/application scope. Here is the code:
<a href="" class='${status.index == step ? "current" : ""}'>
If step is a property of a bean 'beanA' existing page/request/session/application scope scope. You can code like this
<a href="" class='${status.index == beanA.step ? "current" : ""}'>
// beanA has a method getStep().

iterate over two arrays in jsp using struts 2 tags

Can Anyone help me please.
My intention is to iterate over the two 2Dimentional arrays
Org_Positions_IdTitle which contains a)the ids of raised positions and b)their Titles.
Org_Apps which contains a)student Ids and b)ids of raised positions.
So I want whenever the position Ids in the two arrays are checked equal to display the Title of the position. Thanks a lot for your help guys.
ResultSet ResSet_Org_Positions_IdTitle = OrgInfo.Get_Organisation_Positions_IdTitle(username,password);
ResultSet ResSet_Org_Apps = OrgInfo.Get_Organisation_Apps(username,password);
int i=0;
while(ResSet_Org_Positions_IdTitle.next()){
Org_Positions_IdTitle[i][0] = new String(String.valueOf(ResSet_Org_Positions_IdTitle.getInt("P_Ps_Id")));
Org_Positions_IdTitle[i][1]= new String(ResSet_Org_Positions_IdTitle.getString("Title")); i++;
Org_Positions_IdTitle[i][0]= new String("Last");
//System.out.println(i+":"+Org_Positions_IdTitle[i-1][j-1]+Org_Positions_IdTitle[i-1][j]);
}
int m=0; //Org_Apps A_St_Id A_Ps_Id
while(ResSet_Org_Apps.next()){
Org_Apps[m][0] = new String(String.valueOf(ResSet_Org_Apps.getInt("A_St_Id")));
Org_Apps[m][1]= new String(ResSet_Org_Apps.getString("A_Ps_Id")); m++;
Org_Apps[m][0]= new String("Last");
//System.out.println(i+":"+Org_Positions_IdTitle[i-1][j-1]+Org_Positions_IdTitle[i-1][j]);
}
session.put("Org_Positions_IdTitle", getOrg_Positions_IdTitle());
session.put("Org_Apps", getOrg_Apps());
<s:iterator value="Org_Positions_IdTitle" id="P_Ps_Id" >
<s:iterator value="Org_Apps" id="A_Ps_Id" >
<s:if test="%{#P_Ps_Id==#A_Ps_Id} ">
<s:property value="Title" />
</s:if>
</s:iterator>
</s:iterator>
So it should look something like that:
<s:iterator value="firstArray" var="arr1">
<s:iterator value="secondArray" var="arr2">
<s:if test="#arr1[0] == #arr2[1]">
<s:property value="#arr1[1]" />
</s:if>
</s:iterator>
</s:iterator>
Don't forget that you need setters/getters for your arrays in action class.
BTW id attribute in Struts2 tags was deprecated long time ago, use var instead.
Also consider changing your code to use maps instead of arrays. Then there is no need to iterate it on JSP.

Map problem when passing it as model to view in grails

In a controller, I have populated a map that has a string as key and a list as value; in the gsp, I try to show them like this:
<g:each in="${sector}" var="entry" >
<br/>${entry.key}<br/>
<g:each in="${entry.value}" var="item" >
${item.name}<br/>
</g:each>
</g:each>
The problem is that item is considered as string, so I get the exception
Error 500: Error evaluating expression [item.name] on line [11]:
groovy.lang.MissingPropertyException: No such property: name for class:
java.lang.String
Any hints on how to fix it other than doing the find for the item explicitly in the gsp ?
I did the poc which works fine for me
class Item{
String name
}
List items = [new Item(name:"laptop" ,
new Item(name:"mouse")]
Map sector =[:]
sector.manager = items
sector.manager.each{item->
println item.name
}
Even If it doesn't work, Try declaring Map sector as
Map < String, List < Item > > sector =[:]

Resources