• Post Reply Bookmark Topic Watch Topic
  • New Topic

EL accessing a bean with non-existant member

 
Ronan Dowd
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Say for example I've a customer bean (Customer.java) which is stored in the request scope (or any scope in fact) under attribute name "customer". This bean
has a single member as below:
String age = "32";

So each of the below will return the value "32":

${customer.age}
${customer['age']}
${customer["age"]}

But what if I did the below:

${customer["name"]}

Would this throw a runtime exception as the bean property "name" (in Customer.java) doesn't exist .. or would it print nothing ?. I'm
thinking it would through a RuntimeException ?

Thanks, Ronan
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65664
129
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's preventing you from just trying it and finding out?
 
Marlo Magpantay
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will throw a runtime exception.
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Posts: 2782
174
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will throw an error according to the specs (jsp2.0)
JSP.2.3.4 Operators "[]" and "."
To evaluate expr-a[expr-b]:
...removed some part...

Otherwise (a JavaBeans object), coerce value-b to String

  • If value-b is a readable property of value-a, as per the JavaBeans specification:

  • - If getter throws an exception: error
    - Otherwise: return result of getter call
    
  • Otherwise: error.


  • Regards,
    Frits
     
    Nidhi Sar
    Ranch Hand
    Posts: 252
    Android Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I did try this out & it does throw an exception - javax.el.PropertyNotFoundException to be precise.

    But I'm confused about something. In HFSJ, 2nd ed - page 399, this is what the book says,

    EL handles null values gracefully
    ...Assume that ... there IS an attribute named "bar", but that"bar" does not have a property or key named "foo".

    EL_________________________ What Prints
    -----------------------------------------------------
    ${bar[foo]} _________________ Nothing prints out...

    EL is null friendly. It handles unknown or null values so that the page still displays, even if it can't find an attribute/property/key with the name in the expression.


    Is this all incorrect then?


    EDIT : Nevermind, I found the

    Javaranch thread

    where this isuue has been discussed thoroughly. Putting this here, incase someone else runs into this.

     
    What are you doing? You are supposed to be reading this tiny ad!
    the new thread boost feature brings a LOT of attention to your favorite threads
    https://coderanch.com/t/674455/Thread-Boost-feature
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!