How can I create toggle buttons in JSF?
Basically, I need two buttons “in” and “out”. They essentially call the same managed bean, but as a result of every click the one should be disabled and the other should be enabled and vice versa. How can this be done? Should I use ajax functionality?
Just have a
booleanproperty which you inverse in action method and use exactly that property in thedisabledattribute of the both buttons, inversed.Kickoff example:
With
Ajax is technically not necessary. Feel free to add
<f:ajax>to both buttons to improve the user experience though.A
@ViewScopedbean is in turn very necessary. A@RequestScopedone would be trashed on end of request and recreated in next request, hereby causing thebooleanto be reinitialized to default and thus seemingly fail to work after second click, because JSF will as part of safeguard against tampered/hacked requests also check thedisabled(andrendered) attribute before actually invoking the action.See also: