This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Struts and the fly likes logic:iterate display entries Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "logic:iterate display entries" Watch "logic:iterate display entries" New topic
Author

logic:iterate display entries

Hubert Pernegger
Ranch Hand

Joined: Apr 11, 2006
Posts: 49
hi,
at the moment I display my entries the following way:


Accession Code Name of Protein Peptide Sequence Length of Peptide Seq Start in Protein Length of Protein
gb|AAX33375.1| RH43429p [Drosophila melanogaster] WRWSRWRR 8 38 124
gb|AAX33375.1| RH43429p [Drosophila melanogaster] RWRRWRR 7 42 124
gb|AAX33375.1| RH43429p [Drosophila melanogaster] RWRRWSR 7 45 124
gb|AAX33375.1| RH43429p [Drosophila melanogaster] RWRRWWR 7 33 124



but I want that Accession Code and Name of Protein is only displayed once, is that possible with logic:iterate

thanks for help
Soumya Saha
Ranch Hand

Joined: Apr 14, 2006
Posts: 64
Hi Hubert,
it can be done in the Following way.
1.Have a field which will have the count of the no of times the data with the Key is repeated(Have a count of the Data corresponding to the protein id)
2.In the td Block of the Accession Code and Name of Protein specify the rowspan as the count.
Example: (Change as Applicably..)
<logic:iterate id="proteinBean" name="proteinForm" property="proteinList" type="com.protein.proteinApp.beans.ProteinViewBean">
<tr>
<td nowrap rowspan='<bean:write name="proteinBean" property="count"/>' align="center" valign="top">
<bean:write name="proteinBean" property="accessionCode" />
</td>
<td nowrap rowspan='<bean:write name="proteinBean" property="count"/>' align="center" valign="top">
<bean:write name="proteinBean" property="proteinName" />
</td>
<td>
//...Peptide
</td>
<td>
//...Sequence
</td>
<td>
//...Length of Peptide
</td>
<td>
//...Seq
</td>
<td>
//...Start in Protein
</td>
<td>
//...Length of Protein
</td>
</tr>

Thanks
Hubert Pernegger
Ranch Hand

Joined: Apr 11, 2006
Posts: 49
hi soumya,
i didn't get it...
should count contain the value how often name of protein and accession code appears in the set or how often I want it to displayed (because I only want that displayed one, so the value could be hardcoded)

thanks
Soumya Saha
Ranch Hand

Joined: Apr 14, 2006
Posts: 64
Hi Hubert,
You got it.Which ever works for you.
But there is one issue regarding the Accession Code & protein Row, it will be displayed once and rest will be repeated no of times you want.(Like a table with 2 Columns and One Row --- Left Column with one row and the corresponding Right Column having multiple sub-rows in the single row)Is that okay with u?
Anyways, did the code work?

Thanks
Hubert Pernegger
Ranch Hand

Joined: Apr 11, 2006
Posts: 49
hi,
joop, that exactly how I want it...
but I don't know whether it is working so far because i get always that strange messages:

javax.servlet.ServletException: No getter method for property: "count" of bean: "proPep"

although there is a getter and a setter....I don't know why

can I hardcod that value too, because it should be always 1
Hubert Pernegger
Ranch Hand

Joined: Apr 11, 2006
Posts: 49
hi soumya,



that is a part of my method...
what i don't understand where I shall put my setter....I have tried it like you can see above....

tuple[6] = 1;
pair.setCounter((Integer)tuple[6]);

but it doesn't work, because I get a arrayoutofbound exception....it is clear why, because there are exact 5 objects selected by the SQL Statement,
so how I should do that

Hubert Pernegger
Ranch Hand

Joined: Apr 11, 2006
Posts: 49
hi,
I got it to work, but it is displayed the same way like before...
Soumya Saha
Ranch Hand

Joined: Apr 14, 2006
Posts: 64
Hi Hubert,
u are almost there..I missed on onething, I thought u would have guessed it.
Anyways now one thing u have to do..
Scenario:
Say Accession Code 123 has 3 records.Now when u are setting the values in ur Collection u have to make sure that for the first Record the Count field has a value 3 and for all the rest 2 rows it has a value 0.

So the individual rows in the pair should have values in the Counter as Follows:
/-- Pair 1
pair.setName_of_protein(tuple[1].toString());
pair.setPeptide_seq(tuple[2].toString());
pair.setLength_of_peptideseq((Integer)tuple[3]); pair.setStartpos_in_proteinseq((Integer)tuple[4]);
pair.setLength_of_protein((Integer)tuple[5]);
pair.setCounter(3);

/-- Pair 2
pair.setName_of_protein(tuple[1].toString());
pair.setPeptide_seq(tuple[2].toString());
pair.setLength_of_peptideseq((Integer)tuple[3]); pair.setStartpos_in_proteinseq((Integer)tuple[4]);
pair.setLength_of_protein((Integer)tuple[5]);
pair.setCounter(0);

/-- Pair 3
pair.setName_of_protein(tuple[1].toString());
pair.setPeptide_seq(tuple[2].toString());
pair.setLength_of_peptideseq((Integer)tuple[3]); pair.setStartpos_in_proteinseq((Integer)tuple[4]);
pair.setLength_of_protein((Integer)tuple[5]);
pair.setCounter(0);

In JSP Only one CHange
<logic:iterate name="proteinPeptideForm" property="protPeptSet" id="proPep">
<tr><%-- peptide informations --%>
<logic:notEqual name="proPep" property="counter" value="0"><td nowrap rowspan= '<bean:write name="proPep" property="counter"/>' align="center" valign="top"><bean:write name="proPep" property="accession_code" />
</td>
<td nowrap rowspan= '<bean:write name="proPep" property="counter"/>' align="center" valign="top"><bean:write name="proPep" property="name_of_protein" />
</td>
</logic:notEqual>
<td><bean:write name="proPep" property="peptide_seq" /></td>
<td><bean:write name="proPep" property="length_of_peptideseq" /></td>
<td><bean:write name="proPep" property="startpos_in_proteinseq" /></td>
<td><bean:write name="proPep" property="length_of_protein" /></td>
</tr>
</logic:iterate>

U can automate the updation of the counter by holding the first element of the Iterator for Accession Code 123 and then updating its counter field for each iteration of Accession Code 123

Come on we can do it now..
If there is any issues pls do get back..

Thanks
Hubert Pernegger
Ranch Hand

Joined: Apr 11, 2006
Posts: 49
hi soumya,
thanks for your help, I really appreciate that...
but, I don't know how many entries I will have....ok, I can make a count() in my sql statement....that still clear.....

I'm also clear with pair one, there I set the counter....ok,

but what you are doing in pair 2 and 3......, because I cannot set the counter for the other pairs, I can set the counter only once....the rest I have to do in the jsp.....

sorry, that is beyond my horizon...

ok, I have tried that now......



I have done a count in the sql statement...but get the following error:
Soumya Saha
Ranch Hand

Joined: Apr 14, 2006
Posts: 64
Hi Hubert,

U can try this..

//... We will use HashSet to store and compare our first elemt.I assume that the accessionCode is the primaryKey..and we will store its position and update the first element of a set of row of AccesionCOde with Counter...

HashSet Accessset = null;
int count = 0;
int BeanPosition = 0;
int FirstBeanTag = 0;
for(){
//.... Your Logic
// Do not Add the pair Class into proPep Collection Now
if (Accessset.add(String.valueOf(pair.getAccession_code()))) {
count = 0;
count++;
pair.setCounter(count);
proPep.add(pair);
FirstBeanTag = BeanPosition;
} else {
count++;
proPep.add(pair);
pair = (Protein_Peptide)proPep.get(FirstBeanTag);
pair.setCounter(pair.getCounter() + 1);
}
BeanPosition++;

} // Complete Your For Loop

Hope this works out..

Thanks
Hubert Pernegger
Ranch Hand

Joined: Apr 11, 2006
Posts: 49
hi soumya,
ok, I have done that....



but I get a nullpointerexception at this line:



thank you very much
Soumya Saha
Ranch Hand

Joined: Apr 14, 2006
Posts: 64
Hi Hubert,
It seems that ur accessioncode is string...So don't use String.valueOf() it is only for non-string variables.. just use: pair.getAccessionCode

Let me know if there are any more errors

Thanks
Soumya Saha
Ranch Hand

Joined: Apr 14, 2006
Posts: 64
Hi Hubert,
One more thing..I forgot....

Initialize the HashSet:

Accesset = new HashSet();

Sorry ..huh

Thanks
Hubert Pernegger
Ranch Hand

Joined: Apr 11, 2006
Posts: 49
hi soumya,
not bad, respect....that works.....

I have another question, if you don't mind:

I have tried in my logic:iterate loop to count the whole entries and then display the number,
i have tried it with indexID and bean:size but i didn't got it to work as I wanted to....

could you help me, because my problem that I want first (on the top of the page) to display the amount of entry and then interate over the loop to display the entries but with indexID it doesn't recognize the variable, of course and I don;t know how

thanks and greetings
Soumya Saha
Ranch Hand

Joined: Apr 14, 2006
Posts: 64
Hi Hubert,
I will not mind at all...

By Whole Entries ..Did u mean like no of AccessionCodes?..say 123, 124, 125, 126.. So the No of Entries : - 4... Is it?

If So, then it can be done in this way:

when u are trying to add in the Hashset, use another variable to store the entry means :

int noEntry =0;

if(accessset.add(....)) {

noEntry++;

}

Since Hashset will only allow unique keys..so u will have the count...Now display it on JSP.

If u had anything else...in mind..let me know..

Thanks
Hubert Pernegger
Ranch Hand

Joined: Apr 11, 2006
Posts: 49
hi,
ok...
it is not the same function, little different, I only want to count the entries, that's it:



but I cannot do like proPep.add(noEntry); because that doesn't work, and I cannot do it with a setter and getter because the pair only exists in the for loop......
when I could do it with getter and setter then i just can get the noEntry with kind of <% proPep.getNoEntry()%>
or like that....or?
or shall I create the ProteinPeptide pair outside the for loop.....
Soumya Saha
Ranch Hand

Joined: Apr 14, 2006
Posts: 64
Hi Hubert,
Then Just count the size of the protein-peptide Collection and assign it to a variable and display it.

Thanks
Hubert Pernegger
Ranch Hand

Joined: Apr 11, 2006
Posts: 49
ok, hi,
I have done this, I count the entries in my form and have their a getter and setter....

but I don't know to invoke the getter in the jsp:
have tried like that: (I know that's a stupid question)
that's a part of my jsp page:
it is that line where I have tried:
</h3><%= proteinPeptideForm.getNoEntry(); %></td>

Hubert Pernegger
Ranch Hand

Joined: Apr 11, 2006
Posts: 49
hi,
sorry if I bother you once more,
but I get a nullpointerexception doing it in that way...(only need the amount of entries):

Soumya Saha
Ranch Hand

Joined: Apr 14, 2006
Posts: 64
Hi Hubert,
Don't use the variable in the pair.
Use a separate variable in the Action Form.

In the for loop just do :

noofEntry++;

In the End after for Loop set it in the FormBean
proteinBean.setProteinNoEntry(noofEntry);

JSP:
<bean:write name="proteinForm" property="proteinNoEntry"/>

Thanks
Hubert Pernegger
Ranch Hand

Joined: Apr 11, 2006
Posts: 49
hi soumya,
sorry for replying to you so late, but I can't get it to work, maybe (quite sure ) I'm too stupid,

but I have done as you said, that the entire method, where I want to count the entries:



but I have a problem I get a nullpointer execption at that line:



the problem is that this method is separated from the form class and the action class....that means in the action class I invoke that method....

thanks for your help
Soumya Saha
Ranch Hand

Joined: Apr 14, 2006
Posts: 64
Hi Hubert,
Is it possible to change the signature of the method?
If so then instead of returning the Array , u can return a HashMap.

HashMap hm = new HashMAp();
hm.put("Protein_Pep", ArrayObject);
hm.put("NoOfEntry", new Integer(noofentries));

ACTION CLASS:

// Cast the results accordingly...

proteinBean.setPair(hm.get("Protein_Pep"));
proteinBean.setNofoEntries(String.valueOf(hm.get("NoOfEntry"))); // if ur no of entry is String

Thanks
Hubert Pernegger
Ranch Hand

Joined: Apr 11, 2006
Posts: 49
hi soumya,
thanks for your amazing help....
I didn't get it to work with a HashMap, but by returning a list

thank you very much
Soumya Saha
Ranch Hand

Joined: Apr 14, 2006
Posts: 64
U are Welcome Hubert...

Thanks
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: logic:iterate display entries