• Post Reply Bookmark Topic Watch Topic
  • New Topic

Accessing JSF stuff from another servlet

 
Dave Wood
bronco
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Short version: is there a way to get access to the component tree from a non-JSF servlet (e.g. a servlet processing an AJAX-style request)?

Long version:

I have a dropdown menu (let's call it Status) that, when selected, can impact several other fields. The one that matters for this question is a field that can be enabled/disabled based on the selected status. When the status is changed, I have a little Javascript function that makes an AJAX call to a servlet to get various information used to update the state of the UI. One of the things that comes back is whether or not field X should be disabled. Everything works fine, except in this scenario...

The page is initially loaded with field X DISABLED.
Status is chanaged such that field X gets ENABLED.
Form is submitted. The setX() method on my bean is not called.

This makes some sense. Presumably, there's some logic out there that says "that field is diabaled, so don't call the set method for it." The JSF engine doesn't KNOW that I enabled the field behind its back.

What I'd like to do is have the servlet that was telling the UI (the Javascript function) to enable field X *also* update the component tree to tell it that the field is no longer disabled.

I'm thinking that I could probably stuff the FacesContext into the HttpSession and do it from there (I'm going to try this)...but that seems really ugly.

Any other suggestions? I'm hoping I'm just not thinking of something that's actually fairly obvious...

Thanks!
 
Dave Wood
bronco
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dave Wood:
I'm thinking that I could probably stuff the FacesContext into the HttpSession and do it from there

Silly me. The FacesContext is only "live" for the duration of a request...so this doesn't work. Hmm...
 
Dave Wood
bronco
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll continue this little conversation with myself...

I got what I wanted by adding:

- a hidden field on the form that tracks whether field X has been enabled/disabled by javascript (i.e. the javascript updates this hidden field whenever it updates the disabled-ness of the field)
- a PhaseListener (on PhaseId.APPLY_REQUEST_VALUES) that checks the value of this hidden field and updates the component state accordingly (i.e. calls setDisabled() on an HtmlSelectOneMenu object).

FWIW, this solution was suggested by the guy in charge of the JSF JSR (see this post), so I figure it's not going to get much cleaner.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!