Announcement Announcement Module
Collapse
No announcement yet.
Trying to set the cache expiry date in my controller Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Trying to set the cache expiry date in my controller

    Hi,

    I'm using the following construct
    Code:
    @Controller
    public class DefaultValuesController{
    	
        @Autowired private Services services;
    
        @RequestMapping(value="/secure/{id}/metadataInfo.js", method = RequestMethod.GET, produces="text/javascript")
        public @ResponseBody String getMetadataInfo(@PathVariable String id, HttpServletResponse response)
        {
    		response.setHeader("Cache-Control", "max-age=31556926");
    		
    		return "result";
        }
    But I notice that the base class of all controller implements the setCache which controllers have access to.

    So I have two questions:

    1) What is the best way to set the cache attributes in controllers' response?
    2) How could I get access to the real controller behind my POJO? How can I access the instance of WebContentGenerator which actually took care of my request?

    Thanks for your help.

  • #2
    regarding 2 your controller IS the real controller.

    Regarding the first if it is for all controllers use a HandlerInterceptor else implement it in the controller.

    Comment


    • #3
      Thanks Marten,

      I realize that my question 2) was a really a newbies question. For some stupid reasons I was lead to believe that Spring was somehow wrapping my class into a controller class when I was using the @Controller annotation. I was searching a way to access the cacheForSeconds method which was defined in the WebContentGenerator.

      So now I figured out that I can do all this like this. Take the class which I defined before and add the extends WebContentGenerator, and there you go, you have access to the cacheForSeconds method.

      Code:
      @Controller
      public class DefaultValuesController extends WebContentGenerator{
      	
          @Autowired private Services services;
      
          @RequestMapping(value="/secure/{id}/metadataInfo.js", method = RequestMethod.GET, produces="text/javascript")
          public @ResponseBody String getMetadataInfo(@PathVariable String id, HttpServletResponse response)
          {
      		cacheForSeconds(response, 31556925, true);
      		
      		return "result";
          }
      Thanks.

      Comment

      Working...
      X