Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why can't I access the value of this Map in JSF?

 
Dave Alvarado
Ranch Hand
Posts: 436
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm using MyFaces 1.1.6. On a certain JSF page, I have



Then, on the same page, I try to access the value I put in the Map, like so



but nothing prints out when my page renders. I'm sure this is simple -- what am I doing wrong? - Dave
 
Christian Nicoll
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Dave,

I don't use MyFaces, but tried the same with RI. I don't receive any output, because the map can't be found by the EL. I think that the EL-statement is evaluted and Faces tries to look for an attribute myMap in the scopes request -> session -> application, but no attribut is found.

To solve this problem, you can save your map in the request-scope and everything should work as you had expected it:
 
Dave Alvarado
Ranch Hand
Posts: 436
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for this creative work-around, but is the lesson here, that in JSF, it is impossible to directly access values of instance variables defined on the JSF page?

- Dave
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18277
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Alvarado wrote:Thanks for this creative work-around, but is the lesson here, that in JSF, it is impossible to directly access values of instance variables defined on the JSF page?

- Dave


Instance variables of what?. You listed the property named "mymap", but you didn't say which bean it was contained in.

JSF isn't intended to be composed of JSPs with scriptlets in them. That's a violation of the MVC architectural standard, putting a Model object directly inside the View.
 
Christian Nicoll
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it is possible to directly access values of instance variables in a JSF/JSP page, but for that use the scriptlet <%= expression %>. This code will be inserted directly in the service()-method of your servlet and so is able to access the instance variable. I don't say that this look pretty...

With an faces-tag like <h: outputText> I don't see a possbility to access the instance variable, because the "page-scope" misses. I think in Faces 2.0 exists a page scope, and so perhaps it is then also possible with tags like <h: outputText>.


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic