You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Shuai Zheng <zh...@gmail.com> on 2008/01/07 12:44:01 UTC

How to compare String in OGNL?

Dear All,

I want to know how to compare String using OGNL in jsp page for struts 2:

I have following codes:

<s:property value="%{(invProducts.type == 'T')?'Type A':' Type B '}"/>,

But always nothing return, and I try many different ways but the operator ==
looks like can't support String type.

I believe there should have a simple way to do it or I make some stupid
mistakes but I can't find it out by myself

Thanks very much,

Regards,

Zheng Shuai

Re: How to compare String in OGNL?

Posted by Laurie Harper <la...@holoweb.net>.
No, not a bug. It's as intended. A single character in single quotes is 
evaluated as a character constant, not a string. Try this instead:

   <s:if test='invProducts.type==("T")'>

"T" is a string literal whereas 'T' is a character litteral. Note that 
all quotes are switched.

L.

Shuai Zheng wrote:
> Just find out the problem, the code does not work because when we use one
> character like 'T',, the string compare will always fail, I guess it is
> because the OGNL treat it as a char type.
> 
> The following code works fine:
> <s:if test="invProducts.type==('T'+'')">
>  <s:property value="Type A"/>
> </s:if>
> <s:else>
>  <s:property value="Type B"/>
> </s:else>
> 
> It is not a good solution, but anyway it works.
> 
> Is it a bug?
> 
> Regards,
> 
> Zheng Shuai
> 
> On Jan 7, 2008 10:09 PM, lei.java@gmail.com <le...@gmail.com> wrote:
> 
>> I don't think you can do it in one line but you can do it this way:
>>
>> <s:if test="invProducts.type=='T'">
>>  <s:property value="Type A"/>
>> </s:if>
>> <s:else>
>>  <s:property value="Type B"/>
>> </s:else>
>> Hoping it helps.
>>
>> Thanks.
>>
>> On Jan 7, 2008 6:44 AM, Shuai Zheng <zh...@gmail.com> wrote:
>>
>>> Dear All,
>>>
>>> I want to know how to compare String using OGNL in jsp page for struts
>> 2:
>>> I have following codes:
>>>
>>> <s:property value="%{(invProducts.type == 'T')?'Type A':' Type B '}"/>,
>>>
>>> But always nothing return, and I try many different ways but the
>> operator
>>> ==
>>> looks like can't support String type.
>>>
>>> I believe there should have a simple way to do it or I make some stupid
>>> mistakes but I can't find it out by myself
>>>
>>> Thanks very much,
>>>
>>> Regards,
>>>
>>> Zheng Shuai
>>>
> 


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


Re: How to compare String in OGNL?

Posted by Shuai Zheng <zh...@gmail.com>.
Just find out the problem, the code does not work because when we use one
character like 'T',, the string compare will always fail, I guess it is
because the OGNL treat it as a char type.

The following code works fine:
<s:if test="invProducts.type==('T'+'')">
 <s:property value="Type A"/>
</s:if>
<s:else>
 <s:property value="Type B"/>
</s:else>

It is not a good solution, but anyway it works.

Is it a bug?

Regards,

Zheng Shuai

On Jan 7, 2008 10:09 PM, lei.java@gmail.com <le...@gmail.com> wrote:

> I don't think you can do it in one line but you can do it this way:
>
> <s:if test="invProducts.type=='T'">
>  <s:property value="Type A"/>
> </s:if>
> <s:else>
>  <s:property value="Type B"/>
> </s:else>
> Hoping it helps.
>
> Thanks.
>
> On Jan 7, 2008 6:44 AM, Shuai Zheng <zh...@gmail.com> wrote:
>
> > Dear All,
> >
> > I want to know how to compare String using OGNL in jsp page for struts
> 2:
> >
> > I have following codes:
> >
> > <s:property value="%{(invProducts.type == 'T')?'Type A':' Type B '}"/>,
> >
> > But always nothing return, and I try many different ways but the
> operator
> > ==
> > looks like can't support String type.
> >
> > I believe there should have a simple way to do it or I make some stupid
> > mistakes but I can't find it out by myself
> >
> > Thanks very much,
> >
> > Regards,
> >
> > Zheng Shuai
> >
>

Re: How to compare String in OGNL?

Posted by Shuai Zheng <zh...@gmail.com>.
Dear Lei,

Thanks a lot, it works. Actually I tried this before, but it does not work,
I don't what kind of stupid mistake I made previously:)

Regards,

Zheng Shuai

On Jan 7, 2008 10:09 PM, lei.java@gmail.com <le...@gmail.com> wrote:

> I don't think you can do it in one line but you can do it this way:
>
> <s:if test="invProducts.type=='T'">
>  <s:property value="Type A"/>
> </s:if>
> <s:else>
>  <s:property value="Type B"/>
> </s:else>
> Hoping it helps.
>
> Thanks.
>
> On Jan 7, 2008 6:44 AM, Shuai Zheng <zh...@gmail.com> wrote:
>
> > Dear All,
> >
> > I want to know how to compare String using OGNL in jsp page for struts
> 2:
> >
> > I have following codes:
> >
> > <s:property value="%{(invProducts.type == 'T')?'Type A':' Type B '}"/>,
> >
> > But always nothing return, and I try many different ways but the
> operator
> > ==
> > looks like can't support String type.
> >
> > I believe there should have a simple way to do it or I make some stupid
> > mistakes but I can't find it out by myself
> >
> > Thanks very much,
> >
> > Regards,
> >
> > Zheng Shuai
> >
>

Re: How to compare String in OGNL?

Posted by "lei.java@gmail.com" <le...@gmail.com>.
I don't think you can do it in one line but you can do it this way:

<s:if test="invProducts.type=='T'">
  <s:property value="Type A"/>
</s:if>
<s:else>
  <s:property value="Type B"/>
</s:else>
Hoping it helps.

Thanks.

On Jan 7, 2008 6:44 AM, Shuai Zheng <zh...@gmail.com> wrote:

> Dear All,
>
> I want to know how to compare String using OGNL in jsp page for struts 2:
>
> I have following codes:
>
> <s:property value="%{(invProducts.type == 'T')?'Type A':' Type B '}"/>,
>
> But always nothing return, and I try many different ways but the operator
> ==
> looks like can't support String type.
>
> I believe there should have a simple way to do it or I make some stupid
> mistakes but I can't find it out by myself
>
> Thanks very much,
>
> Regards,
>
> Zheng Shuai
>