Monday, March 2, 2009

Using The if, else and elseif tags in Struts 2

This post intends to answer the following questions :-
  1. How to use the if tag in Struts 2?
  2. What are the uses of the if else tag in Struts 2?
  3. How many flavors of the if else tag are there in Struts 2?
The all the flavors of the if else tag are control tags in the Struts 2 framework. As in every programming language, such control structures are used to evaluate a condition and then do something if the condition is true and do something else if the condition is false. But then you already knew that, didn't you? There are three basic flavours of this tag. I shall demonstrate each with an example.


Flavour1-Chocolate!

<s:if test="condition">

This will be printed if the condition is true.
</s:if>


Flavour2-Vanilla!
<s:if test="condition">
This will be printed if the condition is true.
</s:if>
<s:else>
This will be printed if the condition is false.
</s:else>

Flavour3-Strawberry!!
<s:if test="condition">
This will be printed if the condition is true.
</s:if>
<s:elseif test="second_condition">
This will be printed if the second_condition is true.
</s:elseif>
<s:else>
This will be printed if the second condition is false.
</s:else>

So, there you go, three delicious flavors to add to your ice cream bowl. you can eat them separately or you can mix them all up! Its all in your hands, or I would rather say, all over your hands! Feast on it!

Happy Programming ;)

Signing Off
Ryan

No comments: