Shahid Ghafoor
Shahid Ghafoor

Reputation: 3103

certain parameter should be attached on every return by controller using model in spring MVC

I am using spring MVC with Hibernate

My Query is that when a Controller return some data to a view using Model, as defined below

@RequestMapping(value = "/finGeneralJournalAdd", method = RequestMethod.POST)
public String finGeneralJournalAdd(Model model) {

    model.addAttribute("srcDocumentList", pt.getAll(FinSourceDocumentModel.class));
    model.addAttribute("currencyList", pt.getAll(GenCurrencyModel.class));
    model.addAttribute("batchList", pt.getAll(FinBatchModel.class));

    return "fin/finGeneralJournalAdd";
}

Certain parameters should be attached to Model by default on every return.i.e CurrencyId

Upvotes: 1

Views: 58

Answers (2)

a better oliver
a better oliver

Reputation: 26828

If I understand your question, that's exactly what @ModelAttribute is for (docs). You add a method to the controller that is called everytime before a handler method is called. The return value is added to the model.

@ModelAttribute("currencyId")
public Integer currencyId(...) {
    ...
    return currencyId;
}

If that should happen for every controller you can define it in an @ControllerAdvice-annotated class.

Upvotes: 1

Ralph
Ralph

Reputation: 120771

You can use the postHandle method of a Spring HandlerInterceptor.

See this blog http://www.mkyong.com/spring-mvc/spring-mvc-handler-interceptors-example/ for an example of how to implement a HandlerInterceptor

Upvotes: 0

Related Questions