Java Code Examples for

Following code examples demonstrate how to use spring-framework. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use various code implementation of this class.

	public Object bindAs(String className) {
		Object value = null;
		try {
			Class<?> clazz = Class.forName(className);
			Object object = clazz.newInstance();
			WebRequestDataBinder binder = new WebRequestDataBinder(object);
			value = binder.getTarget();
		} catch (Exception e) {
		return value;

    public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {
        AbstractReportForm form = conversionService.convert(nativeWebRequest.getParameter(FORM_TYPE), AbstractReportForm.class);
        WebRequestDataBinder binder = new WebRequestDataBinder(form);
        return form;

    protected Credential obtainCredentials() {
            final ExtUsernamePasswordCredential c = new ExtUsernamePasswordCredential();
            final WebRequestDataBinder binder = new WebRequestDataBinder(c);
            final RestletWebRequest webRequest = new RestletWebRequest(getRequest());


            return c;

	public BindingResult bind(Object obj, WebRequest req) {

		WebRequestDataBinder binder = new WebRequestDataBinder(obj,"entity");
		binder.addCustomFormatter(new DateFormatter());
		for(Validator v:this.validators)
		List<ColumnMeta> columns = EntityUtil.getColumnMeta(this.em.getMetamodel(), obj.getClass());
		for (ColumnMeta c : columns) {
			Object entityProperty = c.getValue(obj);
			if (entityProperty != null && entityProperty instanceof BaseEntity) {
				Integer id = ((BaseEntity) entityProperty).getId();
				if (id != null) {
					Object v = this.em.find(c.getJavaType(), id);
					c.setValue(obj, v);
				} else {
					c.setValue(obj, null);
		return binder.getBindingResult();

    public static void getBeanByAttr(WebRequest servletRequest, Object entity) {
        WebRequestDataBinder dataBinder = new WebRequestDataBinder(entity);

    public BindingResult validate(Object object) {
        String simpleName = object.getClass ().getSimpleName ();
        char first = simpleName.charAt (0);
        simpleName = Character.isLowerCase (first) ? simpleName : Character.toLowerCase (first) + simpleName.substring (1);
        WebRequestDataBinder dataBinder = new WebRequestDataBinder (object, simpleName);
        dataBinder.setValidator (validator);
        dataBinder.validate ();
        return dataBinder.getBindingResult ();

    public Object resolveArgument(MethodParameter methodParameter, NativeWebRequest request) throws Exception {
        if(methodParameter.getParameterType().getAnnotation(Entity.class) != null){
            Object bean;
                bean = em.get(methodParameter.getParameterType(), Long.valueOf(request.getParameter("id")));
                bean = BeanUtils.instantiateClass(methodParameter.getParameterType());
            WebRequestDataBinder binder = new WebRequestDataBinder(bean);
            if (this.bindingInitializer != null) {
                this.bindingInitializer.initBinder(binder, request);
            ServletWebRequest webRequest = new ServletWebRequest((HttpServletRequest) request.getNativeRequest());
            return bean;
        return WebArgumentResolver.UNRESOLVED;

    public BindingResult convertRequestToEntity(Class entityClass, HttpServletRequest request) {
        BaseEntity obj = null;
        HttpInputMessage msg = new ServletServerHttpRequest(request);
        List<HttpMessageConverter<?>> msgconverter = requestMappingHandlerAdapter.getMessageConverters();
        for (HttpMessageConverter<?> converter : msgconverter) {
            if (converter.canRead(entityClass, MediaType.APPLICATION_JSON_UTF8)) {
                try {
                    obj = (BaseEntity), msg);
                } catch (IOException e) {

        WebRequestDataBinder webDataBinder = new WebRequestDataBinder(obj);

        return webDataBinder.getBindingResult();

	public WebRequestDataBinder getDataBinder(WebRequest request, DomainEntity domainEntity){
		WebRequestDataBinder binder = new WebRequestDataBinder(domainEntity);
		return binder;

Special org.springframework.validation.DataBinder to perform data binding from web request parameter

s to JavaBeans, including support for multipart files.

See the DataBinder/WebDataBinder superclasses for customization options, which include specifying allowed/required fields, and registering custom property editors.

Can also used for manual data binding in custom web controllers or interceptors that build on Spring's org.springframework.web.context.request.WebRequest abstraction: e.g. in a org.springframework.web.context.request.WebRequestInterceptor implementation. Simply instantiate a WebRequestDataBinder for each binding process, and invoke bind with the current WebRequest as argument: MyBean myBean = new MyBean(); // apply binder to custom target object WebRequestDataBinder binder = new WebRequestDataBinder(myBean); // register custom editors, if desired binder.registerCustomEditor(...); // trigger actual binding of request parameters binder.bind(request); // optionally evaluate binding errors Errors errors = binder.getErrors(); ... @author Juergen Hoeller @author Brian Clozel @since 2.5.2 @see #bind(org.springframework.web.context.request.WebRequest) @see #registerCustomEditor @see #setAllowedFields @see #setRequiredFields @see #setFieldMarkerPrefix

Read More