1

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?

Shibina EC
  • 135
  • 2
  • 11

1 Answers1

1

@InitBinder works with the name of command/form attributes. It does not work with the method name.In your case you should give @ModelAttribute("saveCustomerProfile") which will bind it with the specific command.

public ModelAndView saveCustomerProfile( @ModelAttribute("saveCustomerProfile") @Valid CustomerProfile customerProfile, BindingResult result, HttpServletRequest request)throws Exception{

  • The thing is I need to allow the same field in some other methods. if I apply initbinder globally ,it will block the field in other controllers too right??How can I apply to a specific method only? if you go through the link which I provided they have did the same but why it is not working for this code. – Shibina EC Jun 25 '18 at 10:32
  • If you apply @InitBinder in a controller without arguments,It will be applied for every method of that controller,not all the controllers. If you want to apply it on specific methods you can do that by defining multiple InitBinder with different values which is shown in the link.The only thing is remaining in the link is that you have to mention value i.e. @ModelAttribute("intbinderValue") in your method.You can use the same field and apply multiple initBinder on the different methods.Just give the diff InitBinder value to ModelAttribute. Let me know if this helps you to solve the problem. – Shailesh Modi Jun 25 '18 at 12:06
  • By giving value to @InitBinder, It is restricted to the method only. – Shailesh Modi Jun 25 '18 at 12:08
  • Sorry I mean in all other handler methods ,not all other controllers.Anyway I got your point, you are telling to initialize initbinder with value command name. And the command name should be different for other Handler methods. then it will individually work for methods. The link says it to give initbinder value as method name..so it wasn't working.So I got confused with that. unfortunately My issue is that, we are using same command name(one value added to constants and using repeatedly) to map different handler methods. I may need to replace those all to fix this I think. – Shibina EC Jun 25 '18 at 13:43