I need a method which is executed before an action is triggered to do some initialization, to get a list of rows from database to display it in a tabular form.
I can think of using the prepare()
method but the list retrieved from the database is dependent upon the current page number supplied dynamically (especially while deleting and editing rows displayed in an HTML table. On page load, the current page would be 1 by default, of course).
Therefore, the current page supplied via either a hidden field or a query-string parameter would not be available in the prepare()
method, since it is executed early before request parameters are bound to the action.
Another way I can think of is by using a method annotated by @Before
but this method is not executed, if any conversion/validation error(s) occur(s).
Can we have a method which is guaranteed to be executed before (or after) an action is executed and it can also have access to request parameters which is not the case with the prepare()
method?
Currently, I'm using a method annotated by @Before
. This requires some ugly conditional checks before getting rows from the database like
if(hasErrors() && CollectionUtils.isEmpty(list))
in the getter method of the list so that in case conversion/validation error(s) occur(s), the list can be initialized in this getter method instead of being initialized in the method annotated by @Before
which is in fact not going to be executed because of conversion/validation error(s).