/** * Whether the given attribute requires a {@link BindingResult} in the model. */ private boolean isBindingCandidate(String attributeName, Object value) { if (attributeName.startsWith(BindingResult.MODEL_KEY_PREFIX)) { return false; } if (this.sessionAttributesHandler.isHandlerSessionAttribute(attributeName, value.getClass())) { return true; } return (!value.getClass().isArray() && !(value instanceof Collection) && !(value instanceof Map) && !BeanUtils.isSimpleValueType(value.getClass())); }
/** * Store a subset of the given attributes in the session. Attributes not * declared as session attributes via {@code @SessionAttributes} are ignored. * @param request the current request * @param attributes candidate attributes for session storage */ public void storeAttributes(WebRequest request, Map<String, ?> attributes) { attributes.forEach((name, value) -> { if (value != null && isHandlerSessionAttribute(name, value.getClass())) { this.sessionAttributeStore.storeAttribute(request, name, value); } }); }
/** * Store a subset of the given attributes in the session. Attributes not * declared as session attributes via {@code @SessionAttributes} are ignored. * @param request the current request * @param attributes candidate attributes for session storage */ public void storeAttributes(WebRequest request, Map<String, ?> attributes) { attributes.forEach((name, value) -> { if (value != null && isHandlerSessionAttribute(name, value.getClass())) { this.sessionAttributeStore.storeAttribute(request, name, value); } }); }
/** * Whether the given attribute requires a {@link BindingResult} in the model. */ private boolean isBindingCandidate(String attributeName, Object value) { if (attributeName.startsWith(BindingResult.MODEL_KEY_PREFIX)) { return false; } if (this.sessionAttributesHandler.isHandlerSessionAttribute(attributeName, value.getClass())) { return true; } return (!value.getClass().isArray() && !(value instanceof Collection) && !(value instanceof Map) && !BeanUtils.isSimpleValueType(value.getClass())); }
/** * Find {@code @ModelAttribute} arguments also listed as {@code @SessionAttributes}. */ private List<String> findSessionAttributeArguments(HandlerMethod handlerMethod) { List<String> result = new ArrayList<>(); for (MethodParameter parameter : handlerMethod.getMethodParameters()) { if (parameter.hasParameterAnnotation(ModelAttribute.class)) { String name = getNameForParameter(parameter); Class<?> paramType = parameter.getParameterType(); if (this.sessionAttributesHandler.isHandlerSessionAttribute(name, paramType)) { result.add(name); } } } return result; }
@Test public void isSessionAttribute() throws Exception { assertTrue(sessionAttributesHandler.isHandlerSessionAttribute("attr1", String.class)); assertTrue(sessionAttributesHandler.isHandlerSessionAttribute("attr2", String.class)); assertTrue(sessionAttributesHandler.isHandlerSessionAttribute("simple", TestBean.class)); assertFalse(sessionAttributesHandler.isHandlerSessionAttribute("simple", String.class)); }
/** * Find {@code @ModelAttribute} arguments also listed as {@code @SessionAttributes}. */ private List<String> findSessionAttributeArguments(HandlerMethod handlerMethod) { List<String> result = new ArrayList<>(); for (MethodParameter parameter : handlerMethod.getMethodParameters()) { if (parameter.hasParameterAnnotation(ModelAttribute.class)) { String name = getNameForParameter(parameter); Class<?> paramType = parameter.getParameterType(); if (this.sessionAttributesHandler.isHandlerSessionAttribute(name, paramType)) { result.add(name); } } } return result; }
@Test public void retrieveAttributes() throws Exception { sessionAttributeStore.storeAttribute(request, "attr1", "value1"); sessionAttributeStore.storeAttribute(request, "attr2", "value2"); sessionAttributeStore.storeAttribute(request, "attr3", new TestBean()); sessionAttributeStore.storeAttribute(request, "attr4", new TestBean()); assertEquals("Named attributes (attr1, attr2) should be 'known' right away", new HashSet<>(asList("attr1", "attr2")), sessionAttributesHandler.retrieveAttributes(request).keySet()); // Resolve 'attr3' by type sessionAttributesHandler.isHandlerSessionAttribute("attr3", TestBean.class); assertEquals("Named attributes (attr1, attr2) and resolved attribute (att3) should be 'known'", new HashSet<>(asList("attr1", "attr2", "attr3")), sessionAttributesHandler.retrieveAttributes(request).keySet()); }
@Test public void cleanupAttributes() throws Exception { sessionAttributeStore.storeAttribute(request, "attr1", "value1"); sessionAttributeStore.storeAttribute(request, "attr2", "value2"); sessionAttributeStore.storeAttribute(request, "attr3", new TestBean()); sessionAttributesHandler.cleanupAttributes(request); assertNull(sessionAttributeStore.retrieveAttribute(request, "attr1")); assertNull(sessionAttributeStore.retrieveAttribute(request, "attr2")); assertNotNull(sessionAttributeStore.retrieveAttribute(request, "attr3")); // Resolve 'attr3' by type sessionAttributesHandler.isHandlerSessionAttribute("attr3", TestBean.class); sessionAttributesHandler.cleanupAttributes(request); assertNull(sessionAttributeStore.retrieveAttribute(request, "attr3")); }
/** * Store a subset of the given attributes in the session. Attributes not * declared as session attributes via {@code @SessionAttributes} are ignored. * @param request the current request * @param attributes candidate attributes for session storage */ public void storeAttributes(WebRequest request, Map<String, ?> attributes) { attributes.forEach((name, value) -> { if (value != null && isHandlerSessionAttribute(name, value.getClass())) { this.sessionAttributeStore.storeAttribute(request, name, value); } }); }
/** * Store a subset of the given attributes in the session. Attributes not * declared as session attributes via {@code @SessionAttributes} are ignored. * @param request the current request * @param attributes candidate attributes for session storage */ public void storeAttributes(WebRequest request, Map<String, ?> attributes) { attributes.forEach((name, value) -> { if (value != null && isHandlerSessionAttribute(name, value.getClass())) { this.sessionAttributeStore.storeAttribute(request, name, value); } }); }
/** * Whether the given attribute requires a {@link BindingResult} in the model. */ private boolean isBindingCandidate(String attributeName, Object value) { if (attributeName.startsWith(BindingResult.MODEL_KEY_PREFIX)) { return false; } if (this.sessionAttributesHandler.isHandlerSessionAttribute(attributeName, value.getClass())) { return true; } return (!value.getClass().isArray() && !(value instanceof Collection) && !(value instanceof Map) && !BeanUtils.isSimpleValueType(value.getClass())); }
/** * Whether the given attribute requires a {@link BindingResult} in the model. */ private boolean isBindingCandidate(String attributeName, Object value) { if (attributeName.startsWith(BindingResult.MODEL_KEY_PREFIX)) { return false; } if (this.sessionAttributesHandler.isHandlerSessionAttribute(attributeName, value.getClass())) { return true; } return (!value.getClass().isArray() && !(value instanceof Collection) && !(value instanceof Map) && !BeanUtils.isSimpleValueType(value.getClass())); }
/** * Find {@code @ModelAttribute} arguments also listed as {@code @SessionAttributes}. */ private List<String> findSessionAttributeArguments(HandlerMethod handlerMethod) { List<String> result = new ArrayList<>(); for (MethodParameter parameter : handlerMethod.getMethodParameters()) { if (parameter.hasParameterAnnotation(ModelAttribute.class)) { String name = getNameForParameter(parameter); Class<?> paramType = parameter.getParameterType(); if (this.sessionAttributesHandler.isHandlerSessionAttribute(name, paramType)) { result.add(name); } } } return result; }
/** * Find {@code @ModelAttribute} arguments also listed as {@code @SessionAttributes}. */ private List<String> findSessionAttributeArguments(HandlerMethod handlerMethod) { List<String> result = new ArrayList<>(); for (MethodParameter parameter : handlerMethod.getMethodParameters()) { if (parameter.hasParameterAnnotation(ModelAttribute.class)) { String name = getNameForParameter(parameter); Class<?> paramType = parameter.getParameterType(); if (this.sessionAttributesHandler.isHandlerSessionAttribute(name, paramType)) { result.add(name); } } } return result; }