aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes EL expressions involvin . and [] operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "EL expressions involvin . and [] operator" Watch "EL expressions involvin . and [] operator" New topic
Author

EL expressions involvin . and [] operator

Kamal Tripathi
Ranch Hand

Joined: Oct 02, 2008
Posts: 86
Source: HFSJ 2nd Edition, Question 1 Page 421 Chapter 8: Scriptless JSPs

Given an HTML form that uses checkboxes to allow a user to select multiple values for a parameter called hobbies.

Which EL expression evaluate to the first value of the hobbies parameter? (Choose all that apply.)

A. ${param.hobbies}
B. ${paramValue.hobbies}
C. ${paramValues.hobbies[0]}
D. ${paramValues.hobbies[1]}
E. ${paramValues[hobbies][0]}
F. ${paramValues[hobbies][1]}


Now the answer is AC and I am convinced by the answer.

However it says options E and F are incorrect syntax. How so ???. Yes it wouldn't return the desired value but why is it the incorrect syntax ???

If I try explaining option C. ${paramValues.hobbies[0]}

paramValues is a map of request parameters and it contains param name and array of possible values in the map. So container will start parsing the expression from left so it will first parse paramValues.hobbies that will return an array (say referenced by arr) of values for parameter hobbies next this expr will be reduced to ${arr[0]} that will give the first values in the array

On same lines if I try explaining option E. ${paramValues[hobbies][0]}

here first paramValues[hobbies] will be evaluated and it will look for hobbies as attribute first and since it won't find it, null will be returned so the expression evaluates to null and ${null[0]} will evaluate to null so it will evaluate to null or empty space will be printed if we use this expression.

1) Please let me know if I am interpreting the explanation wrongly.

2) Also I am a bit confused wherever . and [] get mixed in the expression. So logic is to evaluate it from left hand side taking one operator at a time.

3) Also if this were to be answered using only [] operator we will use it as ${paramValues["hobbies"][0]}, is this correct ??

I feel shy asking these type of questions as my exam date is nearing and still I am asking some very basic doubts


Kamal Tripathi
SCJP 1.4 90%, SCWCD5 94%, Next SCDJWS--> In Naescent stage. Researching abt exam and material itself.
nisha chidella
Ranch Hand

Joined: Jul 24, 2008
Posts: 57
E. ${paramValues[hobbies][0]}
F. ${paramValues[hobbies][1]}

E,F are incorrect syntax because hobbies is not enclosed in double quotes. Its should be ${paramValues["hobbies"][0]}.




3) Also if this were to be answered using only [] operator we will use it as ${paramValues["hobbies"][0]}, is this correct ??


you had the answer for your question


SCJP 5.0(100%)
Preparing for SCWCD
Kamal Tripathi
Ranch Hand

Joined: Oct 02, 2008
Posts: 86
nisha chidella wrote:E,F are incorrect syntax because hobbies is not enclosed in double quotes. Its should be ${paramValues["hobbies"][0]}.



I was assuming by incorrect syntax they mean that it would throw error. But I think I need to watch the language closely. Thnx anyways.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: EL expressions involvin . and [] operator