Results 1 to 3 of 3

Thread: Trying to set the cache expiry date in my controller

  1. #1

    Default 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. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,629

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3

    Default

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •