001/* 002 * Copyright 2002-2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.web.reactive; 018 019import org.springframework.lang.Nullable; 020import org.springframework.ui.Model; 021import org.springframework.validation.support.BindingAwareConcurrentModel; 022import org.springframework.web.bind.support.WebBindingInitializer; 023import org.springframework.web.bind.support.WebExchangeDataBinder; 024import org.springframework.web.server.ServerErrorException; 025import org.springframework.web.server.ServerWebExchange; 026 027/** 028 * Context to assist with binding request data onto Objects and provide access 029 * to a shared {@link Model} with controller-specific attributes. 030 * 031 * <p>Provides methods to create a {@link WebExchangeDataBinder} for a specific 032 * target, command Object to apply data binding and validation to, or without a 033 * target Object for simple type conversion from request values. 034 * 035 * <p>Container for the default model for the request. 036 * 037 * @author Rossen Stoyanchev 038 * @since 5.0 039 */ 040public class BindingContext { 041 042 @Nullable 043 private final WebBindingInitializer initializer; 044 045 private final Model model = new BindingAwareConcurrentModel(); 046 047 048 /** 049 * Create a new {@code BindingContext}. 050 */ 051 public BindingContext() { 052 this(null); 053 } 054 055 /** 056 * Create a new {@code BindingContext} with the given initializer. 057 * @param initializer the binding initializer to apply (may be {@code null}) 058 */ 059 public BindingContext(@Nullable WebBindingInitializer initializer) { 060 this.initializer = initializer; 061 } 062 063 064 /** 065 * Return the default model. 066 */ 067 public Model getModel() { 068 return this.model; 069 } 070 071 072 /** 073 * Create a {@link WebExchangeDataBinder} to apply data binding and 074 * validation with on the target, command object. 075 * @param exchange the current exchange 076 * @param target the object to create a data binder for 077 * @param name the name of the target object 078 * @return the created data binder 079 * @throws ServerErrorException if {@code @InitBinder} method invocation fails 080 */ 081 public WebExchangeDataBinder createDataBinder(ServerWebExchange exchange, @Nullable Object target, String name) { 082 WebExchangeDataBinder dataBinder = new WebExchangeDataBinder(target, name); 083 if (this.initializer != null) { 084 this.initializer.initBinder(dataBinder); 085 } 086 return initDataBinder(dataBinder, exchange); 087 } 088 089 /** 090 * Initialize the data binder instance for the given exchange. 091 * @throws ServerErrorException if {@code @InitBinder} method invocation fails 092 */ 093 protected WebExchangeDataBinder initDataBinder(WebExchangeDataBinder binder, ServerWebExchange exchange) { 094 return binder; 095 } 096 097 /** 098 * Create a {@link WebExchangeDataBinder} without a target object for type 099 * conversion of request values to simple types. 100 * @param exchange the current exchange 101 * @param name the name of the target object 102 * @return the created data binder 103 * @throws ServerErrorException if {@code @InitBinder} method invocation fails 104 */ 105 public WebExchangeDataBinder createDataBinder(ServerWebExchange exchange, String name) { 106 return createDataBinder(exchange, null, name); 107 } 108 109}