• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

if else in iterator

 
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 689
Scala Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
rahulJ james
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 689
Scala Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 689
Scala Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 349
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 349
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 349
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot... I got the solution...

Thanks again
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic