A clean approach to Wicket models

One of the core concepts of Apache Wicket web framework is the model and IModel as its programming interface. Wicket components rely heavily on models, which makes them important pieces of the architecture. Apache Wicket is stateful framework that stores the pages and their components to a page store that usually resides in the HTTP session. Components create the public facing view from the content of the models. Misusing models can cause awkward side effects, like pages not updating their content or huge memory consumption in your application. And the most issues with new Wicket users, from what I’ve seen, are with the correct use of the models. In this blog post I’ll try to explain how and what kind of models should be used in different use cases.

The most basic of the Wicket IModel implementations is the Model class. It is basically a simple placeholder for the model content. This suits fine for situations where the model content does not consume much memory and is more like a constant. A simple String could be a good candidate for Model’s content.

 IModel<String> name = new Model<String>("John");

You have to understand, that when you create a Model object its content stays the same even if the page containing the model is refreshed. This behavior is caused by the use of a static model. Dynamic models can change the actual content every time the value is asked from the model. Using a static model instead of a dynamic one is quite common mistake by new Wicket users. If the concept of static and dynamic models is not clear to you, you should check chapter 4 Understanding models from the book 


Source : http://www.javacodegeeks.com/2013/09/a-clean-approach-to-wicket-models.html

Back to Top