• Post Reply Bookmark Topic Watch Topic
  • New Topic

JSF Primefaces: getting the value of a Calendar field on the page into a Bean property  RSS feed

 
Wim Van Geyt
Ranch Hand
Posts: 32
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

i wonder how i can get the value from a component, here a Calendar pickfield, to a JavaBean property?

I tought it would be with something like FacesContext. ...?


Thanks
 
Tim Holloway
Bartender
Posts: 18777
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rule #1 in JSF is that any time you start referencing something in the java.faces package subtree, you are probably doing it wrong (with the exception of the JSF model classes).

JSF works via Inversion of Control. That means that you don't "go out and get things", things are delivered to you automatically. So, for example, the "value=" attribute of a Calendar View Template control should reference a backing bean property of type java.util.Date. Which means that there needs to be public "set" and "get" methods for that property in the backing bean. If you provide those methods, then JSF will automatically populate the view display with the current value of that property and if the user changes the date control, then a form submit will automatically push the new date value into the backing bean property - provided that all controls on the submitted form have valid values, since JSF is "all or nothing" when it comes to updating a backing bean.
 
Wim Van Geyt
Ranch Hand
Posts: 32
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Holloway wrote:Rule #1 in JSF is that any time you start referencing something in the java.faces package subtree, you are probably doing it wrong (with the exception of the JSF model classes).

JSF works via Inversion of Control. That means that you don't "go out and get things", things are delivered to you automatically. So, for example, the "value=" attribute of a Calendar View Template control should reference a backing bean property of type java.util.Date. Which means that there needs to be public "set" and "get" methods for that property in the backing bean. If you provide those methods, then JSF will automatically populate the view display with the current value of that property and if the user changes the date control, then a form submit will automatically push the new date value into the backing bean property - provided that all controls on the submitted form have valid values, since JSF is "all or nothing" when it comes to updating a backing bean.


Hi, thank you for the information.

I think i haven't bean clear in my question.

The reason why i want to get that information from the component, wich I understand now is a bad way to go, is because i want to compare the old value in the db with the new value wich was set in the component before submitting the form.

I tried to do it with this code:



currentDossier is at origin an Dossier Object wich goes through a DTO object.
The goal was that  currentDossier.getDateDecision() came from the value that has been entered in the forms date component.
And the dateDecision property is filled with the date value of the db.

But currentDossier is the same object for the two values.

The value parameter in the datepicker goes and goes also to and comes from ... .currentDossier.dateDecision

 
Tim Holloway
Bartender
Posts: 18777
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, if you are using your persistence model object as part of the GUI model object, there's a much neater way.

Just add a valueChangeListener to the control(s) that you want to monitor for changes. The valueChangeListener only gets called if the value is changed by the user - which is exactly what you want to detect, so the logic required becomes trivial. Something like this:


Every control you want to watch just add "valueChangeListener="#{myBean.markDirty}". Then in the action method you can simply say:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!