Java Code Examples for org.springframework.web.bind.ServletRequestDataBinder#bind()

The following examples show how to use org.springframework.web.bind.ServletRequestDataBinder#bind() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ItinerarySelectionCommandTest.java    From dddsample-core with MIT License 6 votes vote down vote up
@Test
public void testBind() {
    command = new RouteAssignmentCommand();
    request = new MockHttpServletRequest();

    request.addParameter("legs[0].voyageNumber", "CM01");
    request.addParameter("legs[0].fromUnLocode", "AAAAA");
    request.addParameter("legs[0].toUnLocode", "BBBBB");

    request.addParameter("legs[1].voyageNumber", "CM02");
    request.addParameter("legs[1].fromUnLocode", "CCCCC");
    request.addParameter("legs[1].toUnLocode", "DDDDD");

    request.addParameter("trackingId", "XYZ");

    ServletRequestDataBinder binder = new ServletRequestDataBinder(command);
    binder.bind(request);

    assertThat(command.getLegs()).hasSize(2).extracting("voyageNumber", "fromUnLocode", "toUnLocode")
            .containsAll(Arrays.asList(Tuple.tuple("CM01", "AAAAA", "BBBBB"), Tuple.tuple("CM02", "CCCCC", "DDDDD")));

    assertThat(command.getTrackingId()).isEqualTo("XYZ");
}
 
Example 2
Source File: ServletModelAttributeMethodProcessor.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * This implementation downcasts {@link WebDataBinder} to
 * {@link ServletRequestDataBinder} before binding.
 * @see ServletRequestDataBinderFactory
 */
@Override
protected void bindRequestParameters(WebDataBinder binder, NativeWebRequest request) {
	ServletRequest servletRequest = request.getNativeRequest(ServletRequest.class);
	Assert.state(servletRequest != null, "No ServletRequest");
	ServletRequestDataBinder servletBinder = (ServletRequestDataBinder) binder;
	servletBinder.bind(servletRequest);
}
 
Example 3
Source File: ServletModelAttributeMethodProcessor.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * This implementation downcasts {@link WebDataBinder} to
 * {@link ServletRequestDataBinder} before binding.
 * @see ServletRequestDataBinderFactory
 */
@Override
protected void bindRequestParameters(WebDataBinder binder, NativeWebRequest request) {
	ServletRequest servletRequest = request.getNativeRequest(ServletRequest.class);
	Assert.state(servletRequest != null, "No ServletRequest");
	ServletRequestDataBinder servletBinder = (ServletRequestDataBinder) binder;
	servletBinder.bind(servletRequest);
}
 
Example 4
Source File: MultiActionController.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Bind request parameters onto the given command bean
 * @param request request from which parameters will be bound
 * @param command command object, that must be a JavaBean
 * @throws Exception in case of invalid state or arguments
 */
protected void bind(HttpServletRequest request, Object command) throws Exception {
	logger.debug("Binding request parameters onto MultiActionController command");
	ServletRequestDataBinder binder = createBinder(request, command);
	binder.bind(request);
	if (this.validators != null) {
		for (Validator validator : this.validators) {
			if (validator.supports(command.getClass())) {
				ValidationUtils.invokeValidator(validator, command, binder.getBindingResult());
			}
		}
	}
	binder.closeNoCatch();
}
 
Example 5
Source File: ServletModelAttributeMethodProcessor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * This implementation downcasts {@link WebDataBinder} to
 * {@link ServletRequestDataBinder} before binding.
 * @see ServletRequestDataBinderFactory
 */
@Override
protected void bindRequestParameters(WebDataBinder binder, NativeWebRequest request) {
	ServletRequest servletRequest = request.getNativeRequest(ServletRequest.class);
	ServletRequestDataBinder servletBinder = (ServletRequestDataBinder) binder;
	servletBinder.bind(servletRequest);
}
 
Example 6
Source File: MultiActionController.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Bind request parameters onto the given command bean
 * @param request request from which parameters will be bound
 * @param command command object, that must be a JavaBean
 * @throws Exception in case of invalid state or arguments
 */
protected void bind(HttpServletRequest request, Object command) throws Exception {
	logger.debug("Binding request parameters onto MultiActionController command");
	ServletRequestDataBinder binder = createBinder(request, command);
	binder.bind(request);
	if (this.validators != null) {
		for (Validator validator : this.validators) {
			if (validator.supports(command.getClass())) {
				ValidationUtils.invokeValidator(validator, command, binder.getBindingResult());
			}
		}
	}
	binder.closeNoCatch();
}
 
Example 7
Source File: ServletModelAttributeMethodProcessor.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * This implementation downcasts {@link WebDataBinder} to
 * {@link ServletRequestDataBinder} before binding.
 * @see ServletRequestDataBinderFactory
 */
@Override
protected void bindRequestParameters(WebDataBinder binder, NativeWebRequest request) {
	ServletRequest servletRequest = request.getNativeRequest(ServletRequest.class);
	ServletRequestDataBinder servletBinder = (ServletRequestDataBinder) binder;
	servletBinder.bind(servletRequest);
}
 
Example 8
Source File: EntityModelAttributeMethodProcessor.java    From java-platform with Apache License 2.0 5 votes vote down vote up
/**
 * This implementation downcasts {@link WebDataBinder} to
 * {@link ServletRequestDataBinder} before binding.
 * 
 * @see ServletRequestDataBinderFactory
 */
@Override
protected void bindRequestParameters(WebDataBinder binder, NativeWebRequest request) {
	ServletRequest servletRequest = request.getNativeRequest(ServletRequest.class);
	ServletRequestDataBinder servletBinder = (ServletRequestDataBinder) binder;
	servletBinder.bind(servletRequest);
}
 
Example 9
Source File: UifDefaultFormMethodArgumentResolver.java    From rice with Educational Community License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
        NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
    String name = UifConstants.DEFAULT_MODEL_NAME;

    Object attribute = null;
    if (mavContainer.containsAttribute(name)) {
        attribute = mavContainer.getModel().get(name);
    }

    WebDataBinder binder = binderFactory.createBinder(webRequest, attribute, name);
    if (binder.getTarget() != null) {
        ServletRequest servletRequest = webRequest.getNativeRequest(ServletRequest.class);
        ServletRequestDataBinder servletBinder = (ServletRequestDataBinder) binder;
        servletBinder.bind(servletRequest);

        if (binder.getBindingResult().hasErrors()) {
            if (isBindExceptionRequired(parameter)) {
                throw new BindException(binder.getBindingResult());
            }
        }
    }

    Map<String, Object> bindingResultModel = binder.getBindingResult().getModel();
    mavContainer.removeAttributes(bindingResultModel);
    mavContainer.addAllAttributes(bindingResultModel);

    return binder.getTarget();
}
 
Example 10
Source File: AnnotationMethodHandlerAdapter.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected void doBind(WebDataBinder binder, NativeWebRequest webRequest) throws Exception {
	ServletRequestDataBinder servletBinder = (ServletRequestDataBinder) binder;
	servletBinder.bind(webRequest.getNativeRequest(ServletRequest.class));
}
 
Example 11
Source File: AnnotationMethodHandlerAdapter.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
protected void doBind(WebDataBinder binder, NativeWebRequest webRequest) throws Exception {
	ServletRequestDataBinder servletBinder = (ServletRequestDataBinder) binder;
	servletBinder.bind(webRequest.getNativeRequest(ServletRequest.class));
}