本文整理汇总了Java中org.springframework.web.method.support.ModelAndViewContainer.addAttribute方法的典型用法代码示例。如果您正苦于以下问题:Java ModelAndViewContainer.addAttribute方法的具体用法?Java ModelAndViewContainer.addAttribute怎么用?Java ModelAndViewContainer.addAttribute使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.web.method.support.ModelAndViewContainer
的用法示例。
在下文中一共展示了ModelAndViewContainer.addAttribute方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: initModel
import org.springframework.web.method.support.ModelAndViewContainer; //导入方法依赖的package包/类
/**
* Populate the model in the following order:
* <ol>
* <li>Retrieve "known" session attributes -- i.e. attributes listed via
* {@link SessionAttributes @SessionAttributes} and previously stored in
* the in the model at least once
* <li>Invoke {@link ModelAttribute @ModelAttribute} methods
* <li>Find method arguments eligible as session attributes and retrieve
* them if they're not already present in the model
* </ol>
* @param request the current request
* @param mavContainer contains the model to be initialized
* @param handlerMethod the method for which the model is initialized
* @throws Exception may arise from {@code @ModelAttribute} methods
*/
public void initModel(NativeWebRequest request, ModelAndViewContainer mavContainer, HandlerMethod handlerMethod)
throws Exception {
Map<String, ?> attributesInSession = this.sessionAttributesHandler.retrieveAttributes(request);
mavContainer.mergeAttributes(attributesInSession);
invokeModelAttributeMethods(request, mavContainer);
for (String name : findSessionAttributeArguments(handlerMethod)) {
if (!mavContainer.containsAttribute(name)) {
Object value = this.sessionAttributesHandler.retrieveAttribute(request, name);
if (value == null) {
throw new HttpSessionRequiredException("Expected session attribute '" + name + "'");
}
mavContainer.addAttribute(name, value);
}
}
}
示例2: invokeModelAttributeMethods
import org.springframework.web.method.support.ModelAndViewContainer; //导入方法依赖的package包/类
/**
* Invoke model attribute methods to populate the model. Attributes are
* added only if not already present in the model.
*/
private void invokeModelAttributeMethods(NativeWebRequest request, ModelAndViewContainer mavContainer)
throws Exception {
for (InvocableHandlerMethod attrMethod : this.attributeMethods) {
String modelName = attrMethod.getMethodAnnotation(ModelAttribute.class).value();
if (mavContainer.containsAttribute(modelName)) {
continue;
}
Object returnValue = attrMethod.invokeForRequest(request, mavContainer);
if (!attrMethod.isVoid()){
String returnValueName = getNameForReturnValue(returnValue, attrMethod.getReturnType());
if (!mavContainer.containsAttribute(returnValueName)) {
mavContainer.addAttribute(returnValueName, returnValue);
}
}
}
}
示例3: resolveArgument
import org.springframework.web.method.support.ModelAndViewContainer; //导入方法依赖的package包/类
/**
* Throws MethodArgumentNotValidException if validation fails.
* @throws HttpMessageNotReadableException if {@link RequestBody#required()}
* is {@code true} and there is no body content or if there is no suitable
* converter to read the content with.
*/
@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
Object arg = readWithMessageConverters(webRequest, parameter, parameter.getGenericParameterType());
String name = Conventions.getVariableNameForParameter(parameter);
WebDataBinder binder = binderFactory.createBinder(webRequest, arg, name);
if (arg != null) {
validateIfApplicable(binder, parameter);
if (binder.getBindingResult().hasErrors() && isBindExceptionRequired(binder, parameter)) {
throw new MethodArgumentNotValidException(parameter, binder.getBindingResult());
}
}
mavContainer.addAttribute(BindingResult.MODEL_KEY_PREFIX + name, binder.getBindingResult());
return arg;
}
示例4: initModel
import org.springframework.web.method.support.ModelAndViewContainer; //导入方法依赖的package包/类
/**
* Populate the model in the following order:
* <ol>
* <li>Retrieve "known" session attributes listed as {@code @SessionAttributes}.
* <li>Invoke {@code @ModelAttribute} methods
* <li>Find {@code @ModelAttribute} method arguments also listed as
* {@code @SessionAttributes} and ensure they're present in the model raising
* an exception if necessary.
* </ol>
* @param request the current request
* @param mavContainer a container with the model to be initialized
* @param handlerMethod the method for which the model is initialized
* @throws Exception may arise from {@code @ModelAttribute} methods
*/
public void initModel(NativeWebRequest request, ModelAndViewContainer mavContainer, HandlerMethod handlerMethod)
throws Exception {
Map<String, ?> sessionAttributes = this.sessionAttributesHandler.retrieveAttributes(request);
mavContainer.mergeAttributes(sessionAttributes);
invokeModelAttributeMethods(request, mavContainer);
for (String name : findSessionAttributeArguments(handlerMethod)) {
if (!mavContainer.containsAttribute(name)) {
Object value = this.sessionAttributesHandler.retrieveAttribute(request, name);
if (value == null) {
throw new HttpSessionRequiredException("Expected session attribute '" + name + "'");
}
mavContainer.addAttribute(name, value);
}
}
}
示例5: invokeModelAttributeMethods
import org.springframework.web.method.support.ModelAndViewContainer; //导入方法依赖的package包/类
/**
* Invoke model attribute methods to populate the model.
* Attributes are added only if not already present in the model.
*/
private void invokeModelAttributeMethods(NativeWebRequest request, ModelAndViewContainer mavContainer)
throws Exception {
while (!this.modelMethods.isEmpty()) {
InvocableHandlerMethod attrMethod = getNextModelMethod(mavContainer).getHandlerMethod();
String modelName = attrMethod.getMethodAnnotation(ModelAttribute.class).value();
if (mavContainer.containsAttribute(modelName)) {
continue;
}
Object returnValue = attrMethod.invokeForRequest(request, mavContainer);
if (!attrMethod.isVoid()){
String returnValueName = getNameForReturnValue(returnValue, attrMethod.getReturnType());
if (!mavContainer.containsAttribute(returnValueName)) {
mavContainer.addAttribute(returnValueName, returnValue);
}
}
}
}
示例6: updateModelBindingResult
import org.springframework.web.method.support.ModelAndViewContainer; //导入方法依赖的package包/类
@Test
public void updateModelBindingResult() throws Exception {
String commandName = "attr1";
Object command = new Object();
ModelAndViewContainer container = new ModelAndViewContainer();
container.addAttribute(commandName, command);
WebDataBinder dataBinder = new WebDataBinder(command, commandName);
WebDataBinderFactory binderFactory = mock(WebDataBinderFactory.class);
given(binderFactory.createBinder(this.webRequest, command, commandName)).willReturn(dataBinder);
ModelFactory modelFactory = new ModelFactory(null, binderFactory, this.sessionAttrsHandler);
modelFactory.updateModel(this.webRequest, container);
assertEquals(command, container.getModel().get(commandName));
assertSame(dataBinder.getBindingResult(), container.getModel().get(bindingResultKey(commandName)));
assertEquals(2, container.getModel().size());
}
示例7: updateModelSessionAttributesSaved
import org.springframework.web.method.support.ModelAndViewContainer; //导入方法依赖的package包/类
@Test
public void updateModelSessionAttributesSaved() throws Exception {
String attributeName = "sessionAttr";
String attribute = "value";
ModelAndViewContainer container = new ModelAndViewContainer();
container.addAttribute(attributeName, attribute);
WebDataBinder dataBinder = new WebDataBinder(attribute, attributeName);
WebDataBinderFactory binderFactory = mock(WebDataBinderFactory.class);
given(binderFactory.createBinder(this.webRequest, attribute, attributeName)).willReturn(dataBinder);
ModelFactory modelFactory = new ModelFactory(null, binderFactory, this.sessionAttrsHandler);
modelFactory.updateModel(this.webRequest, container);
assertEquals(attribute, container.getModel().get(attributeName));
assertEquals(attribute, this.sessionAttributeStore.retrieveAttribute(this.webRequest, attributeName));
}
示例8: updateModelSessionAttributesRemoved
import org.springframework.web.method.support.ModelAndViewContainer; //导入方法依赖的package包/类
@Test
public void updateModelSessionAttributesRemoved() throws Exception {
String attributeName = "sessionAttr";
String attribute = "value";
ModelAndViewContainer container = new ModelAndViewContainer();
container.addAttribute(attributeName, attribute);
// Store and resolve once (to be "remembered")
this.sessionAttributeStore.storeAttribute(this.webRequest, attributeName, attribute);
this.sessionAttrsHandler.isHandlerSessionAttribute(attributeName, null);
WebDataBinder dataBinder = new WebDataBinder(attribute, attributeName);
WebDataBinderFactory binderFactory = mock(WebDataBinderFactory.class);
given(binderFactory.createBinder(this.webRequest, attribute, attributeName)).willReturn(dataBinder);
container.getSessionStatus().setComplete();
ModelFactory modelFactory = new ModelFactory(null, binderFactory, this.sessionAttrsHandler);
modelFactory.updateModel(this.webRequest, container);
assertEquals(attribute, container.getModel().get(attributeName));
assertNull(this.sessionAttributeStore.retrieveAttribute(this.webRequest, attributeName));
}
示例9: updateModelWhenRedirecting
import org.springframework.web.method.support.ModelAndViewContainer; //导入方法依赖的package包/类
@Test
public void updateModelWhenRedirecting() throws Exception {
String attributeName = "sessionAttr";
String attribute = "value";
ModelAndViewContainer container = new ModelAndViewContainer();
container.addAttribute(attributeName, attribute);
String queryParam = "123";
String queryParamName = "q";
container.setRedirectModel(new ModelMap(queryParamName, queryParam));
container.setRedirectModelScenario(true);
WebDataBinder dataBinder = new WebDataBinder(attribute, attributeName);
WebDataBinderFactory binderFactory = mock(WebDataBinderFactory.class);
given(binderFactory.createBinder(this.webRequest, attribute, attributeName)).willReturn(dataBinder);
ModelFactory modelFactory = new ModelFactory(null, binderFactory, this.sessionAttrsHandler);
modelFactory.updateModel(this.webRequest, container);
assertEquals(queryParam, container.getModel().get(queryParamName));
assertEquals(1, container.getModel().size());
assertEquals(attribute, this.sessionAttributeStore.retrieveAttribute(this.webRequest, attributeName));
}
示例10: handleReturnValue
import org.springframework.web.method.support.ModelAndViewContainer; //导入方法依赖的package包/类
/**
* Add non-null return values to the {@link ModelAndViewContainer}.
*/
@Override
public void handleReturnValue(
Object returnValue, MethodParameter returnType,
ModelAndViewContainer mavContainer, NativeWebRequest webRequest)
throws Exception {
if (returnValue != null) {
String name = ModelFactory.getNameForReturnValue(returnValue, returnType);
mavContainer.addAttribute(name, returnValue);
}
}
示例11: handleReturnValue
import org.springframework.web.method.support.ModelAndViewContainer; //导入方法依赖的package包/类
/**
* Add non-null return values to the {@link ModelAndViewContainer}.
*/
@Override
public void handleReturnValue(Object returnValue, MethodParameter returnType,
ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
if (returnValue != null) {
String name = ModelFactory.getNameForReturnValue(returnValue, returnType);
mavContainer.addAttribute(name, returnValue);
}
}
示例12: bindingResult
import org.springframework.web.method.support.ModelAndViewContainer; //导入方法依赖的package包/类
@Test
public void bindingResult() throws Exception {
ModelAndViewContainer mavContainer = new ModelAndViewContainer();
mavContainer.addAttribute("ignore1", "value1");
mavContainer.addAttribute("ignore2", "value2");
mavContainer.addAttribute("ignore3", "value3");
mavContainer.addAttribute("ignore4", "value4");
mavContainer.addAttribute("ignore5", "value5");
mavContainer.addAllAttributes(bindingResult.getModel());
Object actual = resolver.resolveArgument(paramErrors, mavContainer, webRequest, null);
assertSame(actual, bindingResult);
}
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:15,代码来源:ErrorsMethodHandlerArgumentResolverTests.java
示例13: bindingResultNotFound
import org.springframework.web.method.support.ModelAndViewContainer; //导入方法依赖的package包/类
@Test(expected=IllegalStateException.class)
public void bindingResultNotFound() throws Exception {
ModelAndViewContainer mavContainer = new ModelAndViewContainer();
mavContainer.addAllAttributes(bindingResult.getModel());
mavContainer.addAttribute("ignore1", "value1");
resolver.resolveArgument(paramErrors, mavContainer, webRequest, null);
}
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:9,代码来源:ErrorsMethodHandlerArgumentResolverTests.java
示例14: resolveArgument
import org.springframework.web.method.support.ModelAndViewContainer; //导入方法依赖的package包/类
/**
* Resolve the argument from the model or if not found instantiate it with
* its default if it is available. The model attribute is then populated
* with request values via data binding and optionally validated
* if {@code @java.validation.Valid} is present on the argument.
*
* @throws org.springframework.validation.BindException
* if data binding and validation result in an error
* and the next method parameter is not of type {@link org.springframework.validation.Errors}.
* @throws Exception if WebDataBinder initialization fails.
*/
public final Object resolveArgument(MethodParameter parameter,
ModelAndViewContainer mavContainer,
NativeWebRequest request,
WebDataBinderFactory binderFactory) throws Exception {
String name = parameter.getParameterAnnotation(FormModel.class).value();
Object target = (mavContainer.containsAttribute(name)) ?
mavContainer.getModel().get(name) : createAttribute(name, parameter, binderFactory, request);
WebDataBinder binder = binderFactory.createBinder(request, target, name);
target = binder.getTarget();
if (target != null) {
bindRequestParameters(mavContainer, binderFactory, binder, request, parameter);
validateIfApplicable(binder, parameter);
if (binder.getBindingResult().hasErrors()) {
if (isBindExceptionRequired(binder, parameter)) {
throw new BindException(binder.getBindingResult());
}
}
}
target = binder.convertIfNecessary(binder.getTarget(), parameter.getParameterType());
mavContainer.addAttribute(name, target);
return target;
}
示例15: resolveArgument
import org.springframework.web.method.support.ModelAndViewContainer; //导入方法依赖的package包/类
/**
* Resolve the argument from the model or if not found instantiate it with
* its default if it is available. The model attribute is then populated
* with request values via data binding and optionally validated
* if {@code @java.validation.Valid} is present on the argument.
* @throws BindException if data binding and validation result in an error
* and the next method parameter is not of type {@link Errors}.
* @throws Exception if WebDataBinder initialization fails.
*/
public final Object resolveArgument(MethodParameter parameter,
ModelAndViewContainer mavContainer,
NativeWebRequest request,
WebDataBinderFactory binderFactory) throws Exception {
String name = parameter.getParameterAnnotation(FormModel.class).value();
Object target = (mavContainer.containsAttribute(name)) ?
mavContainer.getModel().get(name) : createAttribute(name, parameter, binderFactory, request);
WebDataBinder binder = binderFactory.createBinder(request, target, name);
target = binder.getTarget();
if (target != null) {
bindRequestParameters(mavContainer, binderFactory, binder, request, parameter);
validateIfApplicable(binder, parameter);
if (binder.getBindingResult().hasErrors()) {
if (isBindExceptionRequired(binder, parameter)) {
throw new BindException(binder.getBindingResult());
}
}
}
target = binder.convertIfNecessary(binder.getTarget(), parameter.getParameterType());
mavContainer.addAttribute(name, target);
return target;
}