Anuj Patel
Anuj Patel

Reputation: 17839

sharing data between two controllers in Spring MVC

In my application there are two controllers,

  1. DisplayPageController
  2. ExportPageDataController

DisplayPageController gets the required View and sends it to JSP as Json Object which I then use to render page.

ExportPageController generates a pdf of the same data in view (not all but selected data). Also ExportPageController can only be called after DisplayPageController as the view page contains link to it (taking that my clients are not geeks like us and won't type the link of export after finding it from FireBug)

Currently I do calculation in both the controllers and which is REDUNDANT as both the controllers call same Service for getting data.

What I want is, First the DisplayPageController should get the data and render it, then as soon as ExportPageController is invoked it should be given the data already generated by DisplayPageController so that it dont have to calculate data again.

BTW I am using Spring MVC, JSP, JasperReports, MyBatis.!

Thanks.

Upvotes: 1

Views: 717

Answers (1)

Niel de Wet
Niel de Wet

Reputation: 8398

If you can put some key on the data to identify it you could implement a simple cache in a bean that gets injected into both controllers. That way both can check the cache before recalculating. You should limit the size of the cache to something small, because the data will probably only be reused once or twice. I would have make use of a bounded FIFO queue that drops the oldest entry if it is full and a new item is being added.

Upvotes: 2

Related Questions