Pergunta

Is it possible to let @ModelAttribute method be invoked only for a specific handler method and supply the command object for only one handler method invocation ? Not for each handler method within a particular controller ? I'm using spring web-portlet MVC, but it should be the same...

Because this for loop is called for each handler method invocation within one controller and the implicitModel is supplied to each request going to the view

for (Method attributeMethod : this.methodResolver.getModelAttributeMethods()) {
                Method attributeMethodToInvoke = BridgeMethodResolver.findBridgedMethod(attributeMethod);
                Object[] args = resolveHandlerArguments(attributeMethodToInvoke, handler, webRequest, implicitModel);
                if (debug) {
                    logger.debug("Invoking model attribute method: " + attributeMethodToInvoke);
                }
                String attrName = AnnotationUtils.findAnnotation(attributeMethodToInvoke, ModelAttribute.class).value();
                if (!"".equals(attrName) && implicitModel.containsAttribute(attrName)) {
                    continue;
                }
                ReflectionUtils.makeAccessible(attributeMethodToInvoke);
                Object attrValue = attributeMethodToInvoke.invoke(handler, args);
                if ("".equals(attrName)) {
                    Class resolvedType = GenericTypeResolver.resolveReturnType(attributeMethodToInvoke, handler.getClass());
                    attrName = Conventions.getVariableNameForReturnType(attributeMethodToInvoke, resolvedType, attrValue);
                }
                if (!implicitModel.containsAttribute(attrName)) {
                    implicitModel.addAttribute(attrName, attrValue);
                }
            }
Foi útil?

Solução

If you need this level of fine-grained control, then you need to refactor your code, i.e. move the handler method into its own class. Spring MVC makes this very easy, there should be no restriction on this kind of refactoring.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top