Spring Framework DataBinder - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Spring Framework DataBinder class.

This code example shows how to use the following methods:setConversionService
	private FormattingConversionService conversionService;
 
	@Before 
	public void setup() { 
		this.conversionService = new DefaultFormattingConversionService();
		DataBinder dataBinder = new DataBinder(null);
		dataBinder.setConversionService(conversionService);
 
		this.redirectAttributes = new RedirectAttributesModelMap(dataBinder);
	} 
 
	@Test
	public void addAttributePrimitiveType() { 
		this.redirectAttributes.addAttribute("speed", 65);
		assertEquals("65", this.redirectAttributes.get("speed"));
	} 
 
	@Test
	public void addAttributeCustomType() { 
		String attrName = "person";
7
CodeRank
This code example shows how to use the following methods:getAllowedFields, getDisallowedFields, getObjectName, getRequiredFields, getTarget
        DataBinder binder = extractDataBinderArg(jp);
        if (binder == null) {
            return; 
        } 
 
        String objectName = binder.getObjectName();
        op.label("Init Binder " + objectName)
                .put(OBJECT_NAME, objectName);
 
        Object target = binder.getTarget();
        if (target == null) {
            // Target object may be null according to WebDataBinder docs 
            op.put(TARGET_TYPE, UNKNOWN_TARGET_TYPE);
        } else { 
            op.put(TARGET_TYPE, target.getClass().getName());
        } 
 
        fromArray(op.createList(ALLOWED_FIELDS_LIST), binder.getAllowedFields());
        fromArray(op.createList(DISALLOWED_FIELDS_LIST), binder.getDisallowedFields());
        fromArray(op.createList(REQUIRED_FIELDS_LIST), binder.getRequiredFields());

7
CodeRank
This code example shows how to use the following methods:registerCustomEditor, bind, getBindingResult, setRequiredFields
	 * the user will need to correct; on success, a redirect to the "reward/show" 
	 * controller to display confirmed reward details 
	 */ 
	public ModelAndView create(HttpServletRequest request, HttpServletResponse response) { 
		RewardForm rewardForm = new RewardForm(); 
		ServletRequestDataBinder binder = new ServletRequestDataBinder(rewardForm, "rewardForm"); 
		// enforces these fields are present in the form 
		binder.setRequiredFields(new String[] { "creditCardNumber", "amount", "merchantNumber" }); 
		binder.registerCustomEditor(MonetaryAmount.class, new MonetaryAmountEditor()); 
		// copies parameters in the request to the reward form 
		binder.bind(request); 
		if (binder.getBindingResult().hasErrors()) { 
			return new ModelAndView("reward/new", binder.getBindingResult().getModel()); 
		} else { 
			Dining dining = rewardForm.createDining(); 
			RewardConfirmation confirmation = rewardNetwork.rewardAccountFor(dining); 
			return new ModelAndView("redirect:show/" + confirmation.getConfirmationNumber()); 
		} 
	} 
 
6
CodeRank
This code example shows how to use the following methods:bind, getBindingResult, initDirectFieldAccess
		assertEquals("Oct 31, 2009", binder.getBindingResult().getFieldValue("children[0].localDateAnnotated"));
	} 
 
	@Test
	public void testBindLocalDateAnnotatedWithDirectFieldAccess() { 
		binder.initDirectFieldAccess();
		MutablePropertyValues propertyValues = new MutablePropertyValues();
		propertyValues.add("localDateAnnotated", "Oct 31, 2009");
		binder.bind(propertyValues);
		assertEquals(0, binder.getBindingResult().getErrorCount());
		assertEquals("Oct 31, 2009", binder.getBindingResult().getFieldValue("localDateAnnotated"));
	} 
 
	@Test
	public void testBindLocalDateAnnotatedWithDirectFieldAccessAndError() { 
		binder.initDirectFieldAccess();
		MutablePropertyValues propertyValues = new MutablePropertyValues();
		propertyValues.add("localDateAnnotated", "Oct -31, 2009");
		binder.bind(propertyValues);
		assertEquals(1, binder.getBindingResult().getFieldErrorCount("localDateAnnotated"));
6
CodeRank
This code example shows how to use the following methods:getBindingResult, bind, getBindingResult, getTarget, getValidator
     * @param request   The http servlet request object. 
     * @param formObject    A form object. 
     * @return  A binding result containing the outcome of binding and validation. 
     */ 
    protected BindingResult bindAndValidate(HttpServletRequest request, Object formObject) {
        WebDataBinder binder = new WebDataBinder(formObject);
        binder.setValidator(validator);
        binder.bind(new MutablePropertyValues(request.getParameterMap()));
        binder.getValidator().validate(binder.getTarget(), binder.getBindingResult());
        return binder.getBindingResult();
    } 
 
    /** 
     * Creates an expected redirect view path. 
     * @param path  The path to the requested view. 
     * @return  The expected redirect view path. 
     */ 
    protected String createExpectedRedirectViewPath(String path) {
        StringBuilder builder = new StringBuilder();
        builder.append(VIEW_REDIRECT_PREFIX);
6
CodeRank
This code example shows how to use the following methods:getBindingResult, bind, getBindingResult, getTarget, getValidator
        verify(messageSourceMock, times(1)).getMessage(eq(messageCode), any(Object[].class), any(Locale.class)); 
        verifyNoMoreInteractions(messageSourceMock); 
    } 
 
    private BindingResult bindAndValidate(HttpServletRequest request, Object formObject) { 
        WebDataBinder binder = new WebDataBinder(formObject); 
        binder.setValidator(validator); 
        binder.bind(new MutablePropertyValues(request.getParameterMap())); 
        binder.getValidator().validate(binder.getTarget(), binder.getBindingResult()); 
        return binder.getBindingResult(); 
    } 
 
    private void initMessageSourceForFeedbackMessage(String feedbackMessageCode) { 
        when(messageSourceMock.getMessage(eq(feedbackMessageCode), any(Object[].class), any(Locale.class))).thenReturn(FEEDBACK_MESSAGE); 
    } 
} 
6
CodeRank
This code example shows how to use the following methods:convertIfNecessary
 
	private String formatValue(Object value) {
		if (value == null) {
			return null; 
		} 
		return (dataBinder != null) ? dataBinder.convertIfNecessary(value, String.class) : value.toString();
	} 
 
	/** 
	 * {@inheritDoc} 
	 * <p>Formats the attribute value as a String before adding it. 
	 */ 
	@Override 
	public RedirectAttributesModelMap addAttribute(Object attributeValue) {
		super.addAttribute(attributeValue);
		return this;
	} 
 
	/** 
	 * {@inheritDoc} 
5
CodeRank
This code example shows how to use the following methods:setIgnoreUnknownFields
	 * @param target 
	 * @return a {@link DataBinder} that can be used to bind properties to the 
	 * target. 
	 */ 
	protected DataBinder createBinder(Object target) {
		DataBinder binder = new DataBinder(target);
		binder.setIgnoreUnknownFields(!this.strict);
		initBinder(binder);
		registerCustomEditors(binder);
		return binder;
	} 
 
	/** 
	 * Initialize a new binder instance. This hook allows customization of 
	 * binder settings such as the {@link DataBinder#initDirectFieldAccess() 
	 * direct field access}. Called by {@link #createBinder(Object)}. 
	 * <p> 
	 * Note that registration of custom property editors can be done in 
	 * {@link #registerCustomEditors(PropertyEditorRegistry)}. 
	 * </p> 
5
CodeRank
This code example shows how to use the following methods:convertIfNecessary
			arg = resolveDefaultValue(namedValueInfo.defaultValue);
		} 
 
		if (binderFactory != null) {
			WebDataBinder binder = binderFactory.createBinder(webRequest, null, namedValueInfo.name);
			arg = binder.convertIfNecessary(arg, paramType, parameter);
		} 
 
		handleResolvedValue(arg, namedValueInfo.name, parameter, mavContainer, webRequest);
 
		return arg;
	} 
 
	/** 
	 * Obtain the named value for the given method parameter. 
	 */ 
	private NamedValueInfo getNamedValueInfo(MethodParameter parameter) {
		NamedValueInfo namedValueInfo = this.namedValueInfoCache.get(parameter);
		if (namedValueInfo == null) {
			namedValueInfo = createNamedValueInfo(parameter);
5
CodeRank
This code example shows how to use the following methods:bind, setConversionService
        sourceMap.put("USERACTION", "INIT");
        sourceMap.put("ATTACHMENTS",
                ListUtils.lazyList(new ArrayList(), FactoryUtils.instantiateFactory(AttachmentInfo.class)));
        target.setResultsMap(sourceMap);
 
        DataBinder binder = new DataBinder(target);
 
        // set the conversion service to convert the date 
        DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService();
        conversionService.addFormatterForFieldAnnotation(new JodaDateTimeFormatAnnotationFormatterFactory());
        binder.setConversionService(conversionService);
 
        binder.bind(values);
 
        System.out.println(target);
 
        assertEquals("RELEASE", sourceMap.get("USERACTION"));
 
        List attachmentList = (List) sourceMap.get("ATTACHMENTS");
        assertNotNull(attachmentList);
5
CodeRank
See Code Examples for Spring Framework DataBinder Methods: