• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Expression Language question

 
Suhas Wadadekar
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is from HFSJ Chapter 8, Page 429, Q19

Question:
An online jewelry retailer wished to customize their online catalog for users who are logged in. They want to show specials for the user's birthstone month. The company's special offers are stored as a Map<String, Special[]> identified as specials in application scope and updated daily.
There is a bean stored as a session-scoped attribute named userInfo. Calling getBirthdate().getMonth() on this bean will return the user's birthstone month.
Which of the following cope snippets could correctly retrieve the appropriate special offerings?
  • A. ${applicationScope[userInfo.birthdate.month.specials]}
  • B. ${applicationScope.specials[userInfo.birthdate.month]}
  • C. ${applicationScope["specials"].userInfo.birthdate.month}
  • D. ${applicationScope["userInfo.birthdate.month"].specials]}



  • I think both B and C are correct answers. What do you think?

    Note: Any typos please ignore, I have tried to keep this as type-free as possible and this question does not test on typos.

    Suhas.
     
    Chinmaya Chowdary
    Ranch Hand
    Posts: 434
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi, Suhas.

    Here option C says, get the 'specials' attribute that is present in application scope, which is a map object. Then container checks is there 'userInfo' key present in map object. Here it is not available. It seems, Option C is wrong.
     
    Patil Niteen
    Ranch Hand
    Posts: 48
    Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    In option B. ${applicationScope.specials[userInfo.birthdate.month]}, userInfo.birthdate.month results into the birthstone month of the user which is nothing but the key of a Map specials. So this expression returns the special offerings for the user.
    Consider option C. ${applicationScope["specials"].userInfo.birthdate.month}. Here the container looks for a key by name userInfo.But since the key to the Map is the user's birthstone month ,it will not find the specified key and hence doesnot return the special offerings.
    Thanks.
    Nitin.
     
    Suhas Wadadekar
    Ranch Hand
    Posts: 97
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks Chinmaya and Niteen. I got it. Thanks for the explanation.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic