File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes if else in iterator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "if else in iterator" Watch "if else in iterator" New topic
Author

if else in iterator

rahulJ james
Ranch Hand

Joined: Oct 03, 2008
Posts: 123
I have a list from action through which I am able to iterate the values and I am able to display..

Based on a particular value from the list I have to make a particular field a textbox or just the value displayed..

I tried to use if loop under the iterator.. But I dont have a clear idea to use the test attribute for <s:if tag..

Advise or give some suggestions
Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
You'll need to write an OGNL expression that evaluates to true or false. Struts2 has a page about it but if you want to get very indepth on it you'll have to use the link to the actual OGNL site on the page. Here's the link to the Struts2 page.

Struts2 OGNL intro

If you have more specific questions I might be able to give more specific answers.
Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 689

Hi...

You can use like this may be this will help you.

<s:if test="%{#id == categoryId}">
<font class="verdana11greynormal"><s roperty value="categoryName"/</font>
</s:if>
<s:else>

</s:else>

Thanks,

Nishan Patel


Thanks, Nishan Patel
SCJP 1.5, SCWCD 1.5, OCPJWSD Java Developer,My Blog
rahulJ james
Ranch Hand

Joined: Oct 03, 2008
Posts: 123
What do that #id represents in this context..

I am going to use only in this place.. So Is there anyway I can get a brief thing to implement this OGNL
Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 689

Hi..

<s:if test="%{#id == categoryId}">

in my Ans #id represent your list with field id.Its just an example.

when you select result from database and set list this list contain your result #id is just an example you can use it your own as per your required condition.

The reason why we use # because in Struts2 when your Action with extends ActionSupport every contains all push to OGNL so if you want to retrieve you have to use # for Action variable.

Thanks,
Nishan Patel.
rahulJ james
Ranch Hand

Joined: Oct 03, 2008
Posts: 123
Is this correct

<html:if test="%{#strToken == 'A'}" >
<html ropertyvalue="%{strToken }" />
</html:if>

where strToken is the value in my list which is passed from the action.. I wanted to know whether the representation is correct. if not give me your suggestions
And also I wanted to compare with a char where I have given as 'A' . Is that correct
Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 689

hi...

First of all this functionality is Struts 2. if you use struts 1 then I dont know condition in iterator.

Make sure you use Struts 2.0.

Thanks,
Nishan Patel
Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
If strToken is a property of your action class then the code should look like this:



If strToken is just an OGNL variable they you'll need to use the #. I'm also assuming that you are still set up to use "html" instead of "s" for your struts tags. Also, if strToken is a string I'm not sure how it will compare against the character 'A', I'm not sure what OGNL will do there.
rahulJ james
Ranch Hand

Joined: Oct 03, 2008
Posts: 123
In my side I have this strToken in my VO class and I have this class's objects added to a list in the action and this action is being used in the iterator..

When I tried to use something like this
<html:if test="strToken== 'A'" >
<html ropertyvalue="strToken" >

I am not able to get the desired result.. Is ther anything I am overlooking at..
Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
To tell you the correct syntax I'd need to see what properties you have in your action class, as well as the code for any bean objects you are using as properties and I'd need to see how you have your iterator set up in your jsp.
rahulJ james
Ranch Hand

Joined: Oct 03, 2008
Posts: 123
Ok here is the code..

I have defined the variable "strToken" in my VO class.. In a DAO class, I am getting the values from database and dumping these list of "strToken" value in an ArrayList(which holds list of my VO objects) which is in the action class.. I have a getter and setter for this ArrayList in the actin class. I pass this ArrayList in my iterator tag and try to iterate the values, where I need to check for whether this value is equals to 'A' and make this field enabled if not disabled..
Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
the way you have it set up there will only work if strToken is a character type. If its a string you'll want to change the syntax to:

<html:if test='strToken== "A"'>
rahulJ james
Ranch Hand

Joined: Oct 03, 2008
Posts: 123
Thanks a lot... I got the solution...

Thanks again
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: if else in iterator