aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes c:if doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "c:if doubt" Watch "c:if doubt" New topic
Author

c:if doubt

Srividhya Kiran
Ranch Hand

Joined: Feb 17, 2008
Posts: 166
Hello floks

In the below code the string "sri" in bold should be given in single quote or double quote. HFSJ book says it can be either single or double quotes. But I get an error when I use double quotes.

<c:if test="${person.name eq "sri"}" var="cond" scope="request">
Welcome!! ${cond }
</c:if>

Thanks
Srividhya
Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
Could you let us know what error it shows?


SCJP 1.4, SCWCD 5, SCBCD 5, OCPJWSD 5,SCEA-1, Started Assignment Part 2
My blog- http://rkydesigns.blogspot.com
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

EL does allow either single or double quotes... but there's a catch!

If you put EL in an evaluated taglib's attribute, you can't use the same kind of quotes that the tag's attribute uses.

Either of the below would work:
<c:if test='${person.name eq "sri"}' var="cond" scope="request">
Welcome!! ${cond }
</c:if>

<c:if test="${person.name eq 'sri'}" var="cond" scope="request">
Welcome!! ${cond }
</c:if>

Can you figure out why?


A good workman is known by his tools.
sudhakar karnati
Ranch Hand

Joined: May 03, 2007
Posts: 93
Can you figure out why?


I think in the above codes you used either the doubles quotes or single quotes only one but not both..

But i don't understand why it would work like this?

Can any body tell me on which page in HFSJ 1.4 it is explained..

Thanks & Regards,
Sudhakar Karnati
Srividhya Kiran
Ranch Hand

Joined: Feb 17, 2008
Posts: 166
Its given in HFSJ 1st edition pg no 441.

And I am getting this error org.apache.jasper.JasperException: /result.jsp(18,33) equal symbol expected

and when i place my cursor on the string "sri" it says undefined attribute name sri.

I cant figure out why it didnt work

Srividhya
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

Originally posted by Srividhya Kiran:
and when i place my cursor on the string "sri" it says undefined attribute name sri.

That's perfect! It explains exactly what is going on.

Let's examine what's happening...
The compiler tries gathering up all the attributes of the rendered tag...
Oh looky! A test attribute!
<c:if test="${person.name eq "sri"}" var="cond" scope="request">

Let's look at what the value for test is. Hmm... I see it starts with a double quote.
<c:if test="${person.name eq "sri"}" var="cond" scope="request">

So now I can assume that the end of the attribute will also be a double quote!
<c:if test="${person.name eq "sri"}" var="cond" scope="request">

Mmmmm.... yummy test attribute!

What attribute do we have next? sri? c:if isn't supposed to have a sri attribute! And the developer didn't even give it an equals sign! What kind of cruel joke is this!
<c:if test="${person.name eq "sri"}" var="cond" scope="request">

*Puke!*
[ July 25, 2008: Message edited by: Marc Peabody ]
Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
its like this

<c:if test="${person.name eq "sri"}" var="cond" scope="request">

either use like this

<c:if test='${person.name eq 'sri'}' var="cond" scope="request">

or
<c:if test="${person.name eq "sri"}" var="cond" scope="request">

not like this

<c:if test="${person.name eq 'sri'}" var="cond" scope="request">

Please note the changes that i have made to the above inverted commas.
Srividhya Kiran
Ranch Hand

Joined: Feb 17, 2008
Posts: 166
Thanks marc for your detailed explanation.
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

Originally posted by Amandeep Singh:
either use like this...

Umm... your examples have it backwards. See my explanations above.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Thanks Marc Peabody
Renato Bobbio Calogero
Greenhorn

Joined: Apr 20, 2011
Posts: 18
I searched on the forum before posting a new topic, and I found this . Better to continue this zombie post than posting new stuff right ? Hope that's correct... however...

how should I test two String variables are the same , just like the Java "equals()" method ? Is this correct ?



thanks in advance for any reply!
Ashok Kurakula
Greenhorn

Joined: May 16, 2011
Posts: 15

Renato Bobbio Calogero wrote:
how should I test two String variables are the same , just like the Java "equals()" method ? Is this correct ?



Yes, you can test using either "eq" or "==" operators. However, this comparison is case-sensitive as it works just like "equlas()" method in Java. If you want to test the equality by ignoring the case, there isn't any operator for this. First you will have to convert them either to uppercase or lowercase (using JSTL) and then compare. More on string manipulation functions



OCPJP 6, OCE Java EE 6 JSP and Servlets Developer, OCE Java EE 6 EJB Developer...
Renato Bobbio Calogero
Greenhorn

Joined: Apr 20, 2011
Posts: 18
Thank you very much for your quick and clarifying answer!

I also had doubt on the toUpperCase() matter, which you solved
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: c:if doubt