1

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).

Yagnesh Agola
  • 4,556
  • 6
  • 37
  • 50
Tiny
  • 27,221
  • 105
  • 339
  • 599
  • The contents in smaller case (before the edit), the last paragraph was just a side note. It had mostly nothing to do with the question itself. Therefore, it should have been there as it had :) – Tiny Jan 07 '14 at 11:59
  • What about [this](http://stackoverflow.com/a/13491559/573032)? – Roman C Jan 07 '14 at 12:01
  • Use paramsPrepareParamStack or (if you are using some special patternMatcher) a custom stack [like described here](http://stackoverflow.com/a/19495334/1654265). – Andrea Ligios Jan 07 '14 at 12:35
  • The `prepare()` method seems to be invoke twice with `paramsPrepareParamStack` which causes costly operations with the database to be performed. – Tiny Jan 07 '14 at 12:39
  • `paramsPrepareParamStack` works after I removed `@InterceptorRef(value="defaultStack", params={...})` from every method. Is `defaultStack` not needed while using `paramsPrepareParamStack`? – Tiny Jan 07 '14 at 12:49
  • 1
    They are two full stacks, one with an extra interceptor. If you use both of them, you are calling each interceptor twice, not only prepare interceptor. Use one OR the other – Andrea Ligios Jan 07 '14 at 16:36
  • Okay then if that the case .. Use constructor for your action class. It will be called before every action call of that particular action class. – Rookie007 Jan 08 '14 at 01:53

0 Answers0