Chapter 9: Behavior Secrets - Power without inheritance
What is Behavior?
A Behavior is a reusable capability attached to a component. It lets you extend rendering and event handling without subclassing every UI element.
Lifecycle
Behaviors participate in configuration, rendering, and detachment phases. This makes them suitable for cross-cutting UI concerns.
Header Contributions
Use behaviors to register CSS and JavaScript resources near the components that require them.
Custom Behavior
Example
component.add(new AttributeAppender("class", " highlighted"));Design Rules
- Use behaviors for reusable cross-component concerns.
- Avoid storing heavy mutable state in behaviors.
- Prefer explicit naming and focused scope.
Practical Pitfalls
-
ProblemAJAX behavior never fires.CauseEvent names differ by version and old naming stayed in the code.SolutionVerify event names against your Wicket version and standardize them.
-
ProblemEvent handling is unstable across repeated interactions.CauseDOM timing and JavaScript issues conflict with behavior binding.SolutionInspect event propagation and JS errors alongside Wicket behavior code.
-
ProblemVisibility logic becomes slow and unpredictable.CauseHeavy checks are placed in high-frequency visibility evaluation paths.SolutionCentralize expensive checks in configure phase and keep visibility checks lightweight.