How can I apply init binder to a method only?I have searched a lot to find an answer for this. According to the docs The value in @InitBinder
is the names of command/form attributes and/or request parameters that this init-binder method is supposed to apply to. I want to blacklist a model field to a particular handler method only.Following is my code.
@InitBinder("saveCustomerProfile")
public void initBinder(WebDataBinder binder)
binder.setDisallowedFields(new String[]{"cbr1"});
}
Following is my Handler method signature.
@RequestMapping(method= RequestMethod.POST, value="/saveCustomerInfo.htm")
public ModelAndView saveCustomerProfile( @ModelAttribute @Valid CustomerProfile customerProfile, BindingResult result, HttpServletRequest request)throws Exception{
String[] suppressedFields = result.getSuppressedFields();
if (suppressedFields.length > 0) { throw new Exception("You've attempted to bind fields that aren't allowed by @InitBinder: "+ StringUtils.arrayToCommaDelimitedString(suppressedFields));
}}
when I'm removing the @Initbinder
'value' the disallowed fields are setting and it throwing the exception.ie it is working globally for the entire controller.
How can I restrict it to be used for a particular method only? I want to disallow the fields 'cbr1' field in customerProfile
model when /saveCustomerInfo.htm handler method is invoking(only for this method).
I referred the link to implement this but not working.. can anyone please help?