пятница, 1 марта 2013 г.

Аннотация которая позволяет создавать PropertyEditor на ходу

@Controller
public class MyController {
....
  @InitBinder
  protected void initBinder(WebDataBinder binder) {
    binder.registerCustomEditor(int.class, "fieldNameOfSomeModelAttribute", new PropertyEditorSupport() {
      @Override
      public void setAsText(String text) throws IllegalArgumentException {
        if (StringUtils.isEmpty(text)) {
          super.setValue(0);
        } else {
          setValue(Integer.valueOf(text.trim()));
        }
      }
    });
  }

  @ActionMapping(params = "action=headerMsgsConfigSave")
  public void save(ActionRequest request, ActionResponse response, @ModelAttribute("someModelClass") SomeModelClass model) {
  .....
  }


Можно привязать не к конкретному полю модели, а вообще на ходу сделать пропертиедитор:
...
@InitBinder
  protected void initBinder(WebDataBinder binder) {
    binder.registerCustomEditor(HeaderRegViewModel.LinkAction.class, new PropertyEditorSupport() {
      @Override
      public void setAsText(String text) throws IllegalArgumentException {
          setValue(HeaderRegViewModel.LinkAction.getFromString(text));
      }
    });
  }

Комментариев нет:

Отправить комментарий