GeeCON Prague 2014*
The moose likes Struts and the fly likes Equivalent of logic iterate in Struts 2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Equivalent of logic iterate in Struts 2" Watch "Equivalent of logic iterate in Struts 2" New topic
Author

Equivalent of logic iterate in Struts 2

Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Hi.
Im currently working on migrating an application from Struts 1.x to Struts 2 , and have been trying to figure out an equivalent to logic iterate , as im using html indexed fields using an iterator .
Currently ,
its in the form --


<logic:iterate id="listDetails" name="list" scope="session" type="com.enzen.domain.vo.DomainDetailsVO" >
<tr>
<td><bean:write name="listDetails" property="code"/></td>
<td><bean:write name="listDetails" property="description"/></td>
<td><bean:write name="listDetails" property="active"/></td>
<td><bean:write name="listDetails" property="edit"/></td>



</tr>
</logic:iterate>


It would be great if anyone could help in how to implement the same using struts 2 tags or a workaround..


Thanks in Advance .

Learning and Learning!-- Java all the way!
Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
you'll probaly want to use s:iterator to do the loop and s:property to display your data.

something like this:



s:iterator will look for "list" in your Ognl value statck, if this is a property of your action class tha you have exposed correctly with getters and setters it will be found on the value stack. As the iterator pulls each element from the list it will put the elment on top of the stack. So when the first s:property tag runs, it will look for "code" on the stack and it should find it in the element from your list that was pushed onto the stack (again, assuming that it is a property of the element with getters and setters). If you want to generate input fields in the loop its a little more complicated, you'll need to give the fully qualified path to the property in your action class and specify what index its at using listStatus.index.
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Thanks for the reply tom!

Ill try and implement the same .
I was also looking at whether there was an equivalent for the <logic:notEmpty> tag which checks whether a list or structure is empty before processing a code block.
Right now , im using an if else block of this nature,---



Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
If you don't want to use the s:else then you could do

<s:if test="list != null && list.size > 0">
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Thanks tom!
Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
No Problem, hope it helps.
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Hi Tom,
Ive been trying to implement your logic and currently dont have any input fields to be generated within the record , in that case how do I use the ---- status="listStatus" part in my code , is it needed or can i do away with it . Please explain its use and relevance .
Thanks.





Tom Rispoli wrote:you'll probaly want to use s:iterator to do the loop and s:property to display your data.

something like this:



s:iterator will look for "list" in your Ognl value statck, if this is a property of your action class tha you have exposed correctly with getters and setters it will be found on the value stack. As the iterator pulls each element from the list it will put the elment on top of the stack. So when the first s:property tag runs, it will look for "code" on the stack and it should find it in the element from your list that was pushed onto the stack (again, assuming that it is a property of the element with getters and setters). If you want to generate input fields in the loop its a little more complicated, you'll need to give the fully qualified path to the property in your action class and specify what index its at using listStatus.index.


Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
Hi Vic,

by setting the status attribute you get a reference to an object that has information about the status of the iterator. Amongst the information is the index that the iterator is currently processing. For your puropses (just using s:property in the iterator) you don't really need a reference to this object. However, if you were creating input fields in the iterator and you wanted the names of the input fields to allow struts2 to put them into the same list that they were read from correctly, you need to use the full name of property that you are putting in the input field (rather than just the name to access it from the object that the iterator puts on top of the stack).

So if you wanted to display "code" in an input field that would repopulate your form, the tag would look like this:

<s:textfield name="[list%{#listStatus.index}].code"/>
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Hi Tom,

Thanks for the information.
I'm currently not implementing any input fields and I think wouldn't be needing the status parameter.However, I'm currently faced with another difficulty, my screen involves loading a dropdown box whose values are populated from a database. Ive written a method for the same , that returns a List from the database , but when i write the code for the drop down in the jsp , do i call the name of the list OR the action associated with populating the list
ie. JSP

OR


Ive also attached snippets of my action class and the dao class to help get a complete idea.

Action


DAO- Class



Thanks in Advance.
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Bounce! Please Help!
Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
you want the list attribute to be the name of a property of your action class. So you need to have a variable for it and the getters and setters. In your action clase you'll need to populate the list. Looks like you may have this already, called "dtList". Then you'll want to specify properties of the objects of the list for the "listKey" and "listValue" attributes of the s:select. The property you specify for listValue will be what appears on the screen and what you specify for listKey will be what is returned in the input filed when the form is submitted. So I'd guess you want something more like this:



You'll probably also want to specify a name attribute to control what property of your action class the selected value is put into.
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Hi Tom
I tried your suggestion . But however it throws the following error.


SEVERE: Servlet.service() for servlet jsp threw exception
tag 'select', field 'list': The requested list key 'dtList' could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name} - [unknown location]
at org.apache.struts2.components.Component.fieldError(Component.java:231)
at org.apache.struts2.components.Component.findValue(Component.java:293)
at org.apache.struts2.components.ListUIBean.evaluateExtraParams(ListUIBean.java:79)
at org.apache.struts2.components.Select.evaluateExtraParams(Select.java:99)
Please help on how i can correct my code.
Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
Hi Vic, looks like your jsp can't find the property "dtList" in the value stack. I saw that variable in the part of your action class that you posted and assumed it was set up as a property with getters and setters. Was this a bad assumption? Is the loadDomainType method in your action class getting called before your JSP is run? Posting more of your action class might help, also are you accessing the JSP by calling a struts action, or are you calling the JSP directly?
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Hi Tom,
Im using getters and setters to obtain the "dtList" both in the bean and action class . Im placing the action,bean java classes and the jsp of my project below .Please do have a look.
Thanks for helping.
DomainAction1.java

DomainBean.java

P.S How do i attach files on here , i tried attaching my source code using .java, .txt, .doc and the system rejects it , what format should i use to attach my code, in the form of files?
Thanks.

Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
At first glance this looks right, how are you calling the loadDomainType method in your action class? Can you please show the code or config that is doing it? Have you verified that it is getting called?
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Im calling the loadDomainType function through the struts.xml which ive placed below .


And my jsp is as follows.
Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
Ok, at first glance the config looks right too. Can you put some logging into your action class after

dtList=loadDomainType;

to verify that dtList is actually being populated with some values?
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Hi Tom,
Thanks for replying .
could you please check your PM.

Thanks
 
GeeCON Prague 2014
 
subject: Equivalent of logic iterate in Struts 2