You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by lucas owen <sr...@gmail.com> on 2010/06/26 20:28:54 UTC

s:if test condition

Hi Struts 2 users:

I was wondering if you can use a s:if test based on a pojo's
attribute value:

POJO:

public class Warning{

String text;
String activated; // Y or N

}

JSP:

<s:if test=*"%{warning==null}"*>

--> *Warning* null!

</s:if>

<s:else>

--> *Warning* not null!

--> Activated: <s:property value=*"warning.activated"*/>

<s:if test=*"warning.activated == 'Y' "*>

--> *Warning* *activated*.

</s:if>

<s:else>

--> *Warning* *deactivated*.

</s:else>

</s:else>

The first test (check null value) works OK, but the second always goes
through the else part.

I have tried test=*"%{warning.activated == 'Y' }" and
*test=*"%{#warning.activated
== 'Y' }" *too but it doesnt work either.

I dont know what is wrong, <s:property value=*"warning.activated"*/> shows
the value correctly...



I know I can use an auxiliar boolean variable depending on the value Y/N but
I would like to know why it doesnt work this way.

THANKS IN ADVANCE

Re: s:if test condition

Posted by Bill Bohnenberger <bi...@gmail.com>.
Yes, that is why I said "Since 'activated' is a String,..."

On Sun, Jun 27, 2010 at 3:47 PM, Dale Newfield <da...@newfield.org> wrote:

> lucas owen wrote:
>
>> Bill Bohnenberger<bi...@gmail.com>
>>
>>> Since 'activated' is a String, try swapping your single&  double quotes,
>>> e.g. <s:if test='%{warning.activated=="Y"}'>
>>>
>> >
> > that was it!!!
>
> The key is understanding *why* it makes a difference.
>
> The reason is that a single character demarcated by single quotes is not a
> String, it is a character.  The character 'Y' is not equal to the String
> "Y".
>
>
> http://struts.apache.org/2.1.8.1/docs/why-wont-the-if-tag-evaluate-a-one-char-string.html
>
> -Dale
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

Re: s:if test condition

Posted by Dale Newfield <da...@newfield.org>.
lucas owen wrote:
> Bill Bohnenberger<bi...@gmail.com>
>> Since 'activated' is a String, try swapping your single&  double quotes,
>> e.g. <s:if test='%{warning.activated=="Y"}'>
 >
 > that was it!!!

The key is understanding *why* it makes a difference.

The reason is that a single character demarcated by single quotes is not 
a String, it is a character.  The character 'Y' is not equal to the 
String "Y".

http://struts.apache.org/2.1.8.1/docs/why-wont-the-if-tag-evaluate-a-one-char-string.html

-Dale

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: s:if test condition

Posted by lucas owen <sr...@gmail.com>.
OMG!

that was it!!!

I would never have figured out by myself,

THANKS BILL!!!

2010/6/26 Bill Bohnenberger <bi...@gmail.com>

> Since 'activated' is a String, try swapping your single & double quotes,
> e.g.
> <s:if test='%{warning.activated=="Y"}'>
>
> -Bill
>
> On Sat, Jun 26, 2010 at 11:28 AM, lucas owen <sr...@gmail.com> wrote:
>
> > Hi Struts 2 users:
> >
> > I was wondering if you can use a s:if test based on a pojo's
> > attribute value:
> >
> > POJO:
> >
> > public class Warning{
> >
> > String text;
> > String activated; // Y or N
> >
> > }
> >
> > JSP:
> >
> > <s:if test=*"%{warning==null}"*>
> >
> > --> *Warning* null!
> >
> > </s:if>
> >
> > <s:else>
> >
> > --> *Warning* not null!
> >
> > --> Activated: <s:property value=*"warning.activated"*/>
> >
> > <s:if test=*"warning.activated == 'Y' "*>
> >
> > --> *Warning* *activated*.
> >
> > </s:if>
> >
> > <s:else>
> >
> > --> *Warning* *deactivated*.
> >
> > </s:else>
> >
> > </s:else>
> >
> > The first test (check null value) works OK, but the second always goes
> > through the else part.
> >
> > I have tried test=*"%{warning.activated == 'Y' }" and
> > *test=*"%{#warning.activated
> > == 'Y' }" *too but it doesnt work either.
> >
> > I dont know what is wrong, <s:property value=*"warning.activated"*/>
> shows
> > the value correctly...
> >
> >
> >
> > I know I can use an auxiliar boolean variable depending on the value Y/N
> > but
> > I would like to know why it doesnt work this way.
> >
> > THANKS IN ADVANCE
> >
>

Re: s:if test condition

Posted by Bill Bohnenberger <bi...@gmail.com>.
Since 'activated' is a String, try swapping your single & double quotes,
e.g.
<s:if test='%{warning.activated=="Y"}'>

-Bill

On Sat, Jun 26, 2010 at 11:28 AM, lucas owen <sr...@gmail.com> wrote:

> Hi Struts 2 users:
>
> I was wondering if you can use a s:if test based on a pojo's
> attribute value:
>
> POJO:
>
> public class Warning{
>
> String text;
> String activated; // Y or N
>
> }
>
> JSP:
>
> <s:if test=*"%{warning==null}"*>
>
> --> *Warning* null!
>
> </s:if>
>
> <s:else>
>
> --> *Warning* not null!
>
> --> Activated: <s:property value=*"warning.activated"*/>
>
> <s:if test=*"warning.activated == 'Y' "*>
>
> --> *Warning* *activated*.
>
> </s:if>
>
> <s:else>
>
> --> *Warning* *deactivated*.
>
> </s:else>
>
> </s:else>
>
> The first test (check null value) works OK, but the second always goes
> through the else part.
>
> I have tried test=*"%{warning.activated == 'Y' }" and
> *test=*"%{#warning.activated
> == 'Y' }" *too but it doesnt work either.
>
> I dont know what is wrong, <s:property value=*"warning.activated"*/> shows
> the value correctly...
>
>
>
> I know I can use an auxiliar boolean variable depending on the value Y/N
> but
> I would like to know why it doesnt work this way.
>
> THANKS IN ADVANCE
>