GeeCON Prague 2014*
The moose likes JSP and the fly likes [Solved] EL doesn't work as per expectation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSP
Bookmark "[Solved] EL doesn Watch "[Solved] EL doesn New topic
Author

[Solved] EL doesn't work as per expectation

Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Code excerpt of jsp page



'yeararray' is an array contains {2009,...2017}.Its value will be always currentyear(2009) plus next nine years(upto 2017). So it would vary from time to time and that's why I didn't hard code it in jsp page.
Lets say user has filled the form and hit the submit button.Now validation fails for some value so I have to show the same page and I don't want to loose the values entered by user previously. It sustains the values for rest of the controls but not for dropdown menu as shown above. I made the same control work in other forms but there the values in drop down are static so I have hard coded them but in this case it comes dynamically at runtime.

I am unable to understand what's wrong in this logic

${param.ccexpyear eq '${year}' ? 'selected' : ''

When I debug it,
${param.ccexpyear} and ${year} returns same value.

(PS: No errror, But by default the first value (2009) gets selected. )
So,
1) What's wrong with the above given code?
2) Is it ok to fill the drop down dynamically?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61308
    
  66

What is it doing as opposed to what you expected? You haven't really said.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Bear Bibeault wrote:What is it doing as opposed to what you expected? You haven't really said.

Not Really.
Vishal Pandya wrote:(PS: No errror, But by default the first value (2009) gets selected. )

When the page is loaded for the first time by default 2009 is selected. Let's say I change it to 2011 and submit the page. But due to some XYZ validation error on the page I need to show the same page persisting old values. It persist for all the fields other than this drop down. i.e. It is reset to 2009 but if it works correctly it should read 2011.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61308
    
  66

Have you inspected the generated HTML? What's different from what you expect?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61308
    
  66

And to save you a little time, also ponder the syntax of this EL expression closely. Do you think it's valid?


Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Bear Bibeault wrote:Have you inspected the generated HTML? What's different from what you expect?

Generated HTML



Oh! here none of the options has selected but as per HTML rule it picks the first value by default. So that means my EL has some problem as you have pointed.



I can't understand what's wrong with that. First the inner EL ${year} will be evaluated then param.ccexpyear will be evaluated, then it will check for equality and then ternary operator will be evaluated. This is my understanding.

The main goal behind all this is I want to sustain the value of drop down box when it changes to some other value.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61308
    
  66

Vishal Pandya wrote:First the inner EL ${year} will be evaluated ...

So sure of this are you?

Where did you pick up the notion that EL expression need to be nested?

Rethink that.
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
I want to sustain the value of drop down box when it changes to some other value. How can I do this?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61308
    
  66

You just need to fix your EL expression. EL expressions are never nested. Why do you feel the need to do so?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61308
    
  66

Another hint: you are treating param.ccexpyear differently from year. Why would they be treated differently?
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374


Actually I am using Spring binding along with EL and JSTL.
ccexpyear is a field in command class vendor.
Drop down is filled by yeararray that I filled in controller and set it into request.
When the value of dropdown is changed to some other value say 2011 it would be set to ccexpyear. And year is a loop variable. I compare year and ccexpyear if they match selected text will come for only that option and hence it would be selected in the dropdown.

I didn't find anything else other then nested EL.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61308
    
  66

Vishal Pandya wrote:I didn't find anything else other then nested EL.

What do you mean by "find"? I can assure that nowhere will you find valid examples of EL expressions that contain nested EL.

Reread my previous hint:
Another hint: you are treating param.ccexpyear differently from year. Why would they be treated differently?

and answer the question.

Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Ok EL can't be nested.
you are treating param.ccexpyear differently from year. Why would they be treated differently?

I don't follow you. How am I treating them differently. I can replace param.ccexpyear with status.value but that also doesn't work.
year is a loop variable.
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Ok Bear it works now.
Here is the working piece of code.



Thanks bear for your patience.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61308
    
  66

Within your EL expression:
you are referencing one value as param.ccexpyear and the other as ${year}.

Why the difference?
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
actually I thought only param.ccexpyear will evaluated and I have to use EL ${year} to evaluate year separately.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61308
    
  66

Again, why would they need to be different? So how would you re-write the expression to treat them similarly?
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61308
    
  66

Bingo. There is no need -- in fact, it's completely invalid -- to nest EL expressions. The ${} notation is not some sort of "fetch" operator to get a value; it's a delimiter that encompasses the entire EL expression.

So, is it working now? If not, at least it's not because of invalid EL syntax.
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Bear it works perfectly now as I have already mentioned above couple of posts with working code snippet. Thanks for your patience. It was a great help. To be frank I didn't learn EL completely yet.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61308
    
  66

Great! I know this took some time, but I felt you'd get more out of this if you ended up solving it yourself! Good show.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: [Solved] EL doesn't work as per expectation