Example usage for org.springframework.web.bind WebDataBinder getTarget

List of usage examples for org.springframework.web.bind WebDataBinder getTarget

Introduction

In this page you can find the example usage for org.springframework.web.bind WebDataBinder getTarget.

Prototype

@Nullable
public Object getTarget() 

Source Link

Document

Return the wrapped target object.

Usage

From source file:cz.muni.fi.pa165.mvc.controllers.TeamController.java

@InitBinder
protected void initBinder(WebDataBinder binder) {
    if (binder.getTarget() instanceof TeamDTO) {
        binder.addValidators(new TeamValidator());
    }/* w ww .  j  av  a 2  s  .c o m*/
}

From source file:cz.muni.fi.pa165.mvc.controllers.GameController.java

@InitBinder
protected void initBinder(WebDataBinder binder) {
    if (binder.getTarget() instanceof GameCreateDTO) {
        binder.addValidators(new GameCreateDTOValidator());
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
        sdf.setLenient(true);// w  ww .java 2  s .  c  o m
        binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
    }
}

From source file:cz.muni.fi.dndtroopsweb.controllers.TroopController.java

/**
 * Binds validator for Troop creation//w ww .  j  av  a  2  s.co  m
 */
@InitBinder
protected void initBinder(WebDataBinder binder) {
    if (binder.getTarget() instanceof TroopCreateDTO) {
        binder.addValidators(new TroopCreateDTOValidator());
    }
}

From source file:cz.muni.fi.pa165.mvc.controllers.PlayerController.java

@InitBinder
protected void initBinder(WebDataBinder binder) {
    if (binder.getTarget() instanceof PlayerCreateDTO) {
        binder.addValidators(new PlayerCreateDTOValidator());
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
        sdf.setLenient(true);/* w  w  w  .ja  v a 2 s.  com*/
        binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
    }
}

From source file:com.benfante.minimark.controllers.AppBindingInitializer.java

public void initBinder(WebDataBinder binder, WebRequest request) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
    dateFormat.setLenient(false);//from   w w w. j av  a 2s .  c  o m
    binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    if (binder.getTarget() != null) {
        binder.setMessageCodesResolver(new ModelAwareMessageCodesResolver());
    }
}

From source file:cz.muni.fi.dndtroopsweb.controllers.HeroController.java

/**
 * Binds validator for Hero creation/*from   w  ww .  j av  a2 s.c  om*/
 */
@InitBinder
protected void initBinder(WebDataBinder binder) {
    if (binder.getTarget() instanceof HeroCreateDTO) {
        binder.addValidators(new HeroCreateDTOValidator());
    }
}

From source file:org.opentides.web.processor.FormBindMethodProcessor.java

/**
 * Resolve the argument from the model or if not found instantiate it with
 * its default. The model attribute is then populated with request values 
 * via data binding and validated. If no validation error, the model
 * is retrieved from database and merged with the submitted form.
 *   /*from w  w  w.  j  av a 2 s.  c o m*/
 * @throws BindException if data binding and validation result in an error
 * @throws Exception if WebDataBinder initialization fails.
 */
@SuppressWarnings("unchecked")
public final Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
        NativeWebRequest nativeRequest, WebDataBinderFactory binderFactory) throws Exception {

    FormBind annot = parameter.getParameterAnnotation(FormBind.class);
    Class<?> clazz = parameter.getDeclaringClass();
    String name = getName(annot, parameter);
    HttpServletRequest request = (HttpServletRequest) nativeRequest.getNativeRequest();

    Method addForm = CacheUtil.getNewFormBindMethod(clazz);
    Object controller = beanFactory.getBean(parameter.getContainingClass());
    Object target = (addForm != null) ? addForm.invoke(controller, request)
            : BeanUtils.instantiateClass(parameter.getParameterType());
    MutablePropertyValues mpvs = new MutablePropertyValues(nativeRequest.getParameterMap());
    WebDataBinder binder = binderFactory.createBinder(nativeRequest, target, name);
    if (binder.getTarget() != null) {
        binder.bind(mpvs);
        if (binder.getValidator() != null)
            binder.validate();
        if (binder.getBindingResult().hasErrors()) {
            throw new BindException(binder.getBindingResult());
        }
        String method = request.getMethod().toLowerCase();

        // id should be the last segment of the uri
        String uri = request.getRequestURI();
        String sid = uri.substring(uri.lastIndexOf("/") + 1);
        Long id = StringUtil.convertToLong(sid, 0);

        // if target extends BaseEntity and for update, link target to database record
        if (("put".equals(method) || "post".equals(method)) && id > 0
                && BaseEntity.class.isAssignableFrom(parameter.getParameterType())) {
            // now retrieve record from database for updating
            Method updateForm = CacheUtil.getUpdateFormBindMethod(clazz);

            BaseEntity record = null;
            if (updateForm == null) {
                // no annotation, invoke from service
                Method getService = controller.getClass().getMethod("getService");
                if (getService == null) {
                    String message = "Cannot find method with @FormBind with update mode. "
                            + "Also, unable to find service associated to controller."
                            + "Please specify one that retrieves record from database.";
                    throw new InvalidImplementationException(message);
                }
                BaseCrudService<? extends BaseEntity> service = (BaseCrudService<? extends BaseEntity>) getService
                        .invoke(controller);
                record = (BaseEntity) service.load(sid);
            } else {
                // with annotation, invoke annotation
                record = (BaseEntity) updateForm.invoke(controller, sid, request);
            }

            if (record != null) {
                WebDataBinder updateBinder = binderFactory.createBinder(nativeRequest, record, name);
                updateBinder.bind(mpvs);
                mavContainer.addAllAttributes(updateBinder.getBindingResult().getModel());
                return updateBinder.getTarget();
            } else {
                String message = "Unable to find " + parameter.getParameterType().getSimpleName() + " with id="
                        + sid + " for update.";
                throw new DataAccessException(message);
            }
        } else if ("post".equals(method)) {
            mavContainer.addAllAttributes(binder.getBindingResult().getModel());
            return binder.getTarget();
        } else {
            throw new InvalidImplementationException(
                    "@FormBind argument annotation can only be used on POST or PUT methods.");
        }
    }
    mavContainer.addAllAttributes(binder.getBindingResult().getModel());
    return binder.getTarget();
}

From source file:edu.mayo.cts2.framework.webapp.rest.controller.MapEntryController.java

@InitBinder
public void initMapEntryQueryServiceRestrictionsBinder(WebDataBinder binder,
        @RequestParam(value = PARAM_TARGETENTITYID, required = false) List<String> targetEntities) {

    if (binder.getTarget() instanceof MapEntryQueryServiceRestrictions) {
        MapEntryQueryServiceRestrictions restrictions = (MapEntryQueryServiceRestrictions) binder.getTarget();

        Set<EntityNameOrURI> targetEntitiesSet = ControllerUtils.idsToEntityNameOrUriSet(targetEntities);

        restrictions.setTargetEntities(targetEntitiesSet);
    }//from  w ww .j  a va  2s  . co m
}

From source file:edu.mayo.cts2.framework.webapp.rest.controller.ConceptDomainBindingController.java

@InitBinder
public void initConceptDomainBindingRestrictionBinder(WebDataBinder binder,
        @RequestParam(value = PARAM_CONCEPTDOMAIN, required = false) String conceptdomain) {

    if (binder.getTarget() instanceof ConceptDomainBindingQueryServiceRestrictions) {
        ConceptDomainBindingQueryServiceRestrictions restrictions = (ConceptDomainBindingQueryServiceRestrictions) binder
                .getTarget();/*  w w  w . ja v  a2  s.  c  om*/

        if (StringUtils.isNotBlank(conceptdomain)) {
            restrictions.setConceptDomain(ModelUtils.nameOrUriFromEither(conceptdomain));
        }
    }
}

From source file:edu.mayo.cts2.framework.webapp.rest.controller.ChangeSetController.java

@InitBinder
public void initChangeSetRestrictionBinder(WebDataBinder binder,
        @RequestParam(value = PARAM_FROMDATE, required = false) Date fromDate,
        @RequestParam(value = PARAM_TODATE, required = false) Date toDate) {

    if (binder.getTarget() instanceof ChangeSetQueryExtensionRestrictions) {
        ChangeSetQueryExtensionRestrictions restrictions = (ChangeSetQueryExtensionRestrictions) binder
                .getTarget();//from  w  ww .  jav a2  s .  com

        restrictions.setFromDate(fromDate);
        restrictions.setToDate(toDate);
    }
}