Back Forum Reply New

New @Preprocess Annotation?

Hi,

first sorry for my bad english, i hope you understand me :-)In some of my Controllers all Request Handlers have an fromServletResponse  Argument to prevent the Browser of cacheing the Result. It is a littlebit frustrating to pass the fromServletResponse to all of this Handlers. To solve and generalize this here comes my idea: A new Annotation @Preprocess (which can be annotated to Functions). Such an Function should have the same Arguments like an @RequestMapping Handler.

Before every Handler invocation all @Preprocess Functionsignatures are checked against the current context (An @Preprocess Function with quot;@PathVariable int idquot; or quot;@ModelAttribute MyClass clazzquot; arguments are not called if there is no PathVariable int id or there is no MyClass in the Model). In my case the @Preprocess Function needs an fromServletResponse, so if this Argument can be passed to the Handler, it will also be passed to the @Preprocess Handler.

quot;Old Codequot;

Code:
@Controller
public class MyController {
@RequestMapping(value = quot;fooquot;, method=RequestMethod.POST)
public ModelAndView setFoo(@RequestParam(quot;fooquot;) String foo, fromServletResponse res) {
res.setHeader(quot;Cache-Controlquot;, quot;no-cachequot;);
res.setHeader(quot;Expiresquot;, quot;-1quot;);
//...
}
@RequestMapping(value = quot;barquot;, method=RequestMethod.POST)
public ModelAndView setBar(@RequestParam(quot;barquot;) String bar, fromServletResponse res) {
res.setHeader(quot;Cache-Controlquot;, quot;no-cachequot;);
res.setHeader(quot;Expiresquot;, quot;-1quot;);
//...
}
}
quot;New Codequot;

Code:
@Controller
public class MyController {
// use this preprocessor for all POST Handlers
@Preprocess(method=RequestMethod.POST)
public void responsePreprocessor(fromServletResponse res) {
res.setHeader(quot;Cache-Controlquot;, quot;no-cachequot;);
res.setHeader(quot;Expiresquot;, quot;-1quot;);
}

@RequestMapping(value = quot;fooquot;, method=RequestMethod.POST)
public ModelAndView setFoo(@RequestParam(quot;fooquot;) String foo) {
//...
}

@RequestMapping(value = quot;barquot;, method=RequestMethod.POST)
public ModelAndView setBar(@RequestParam(quot;barquot;) String bar) {
//...
}
}
Please tell me your opinion.

Can't this be done in an interceptor and configured for those controllers that require such a behavior

maybe, but i think it is easier to read, implement and understand (for controller developers)

you can also implement @ModelAttribute functions with interceptors, but it is easier to have such an function in the controller class, quot;at the same placequot;.

the @InitBinder can convert/validate parameters, but not all possible argument types. with @Preprocess you can modify/validate or what ever you want all arguments, without the need to pass this parameters to all functions and duplicate the code there (even it is only a call to an class level processing function, this would be duplicate code and unnecessary repeated parameter[s])

Well as mentioned before that is basically what interceptors are for (crosscutting and/or general logic). Next to that what you try to do is also easily configured/added to a view class (instead of your controller because it basically is view logic), simply implement the prepareResponse method and set your properties there.
¥
Back Forum Reply New