Spring @ModelAttribute只有一个处理程序方法

是否可以仅为特定的处理程序方法调用@ModelAttribute方法,并仅为一个处理程序方法调用提供命令对象?不适用于特定控制器中的每个处理程序方法?我正在使用spring web-portlet MVC,但它应该是相同的...... 因为在一个控制器中为每个处理程序方法调用调用此for循环,并且为每个进入视图的请求提供implicitModel
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);
                }
            }
    
已邀请:
如果你需要这种级别的细粒度控制,那么你需要重构你的代码,即将处理程序方法移动到它自己的类中。 Spring MVC使这很容易,对这种重构应该没有限制。     

要回复问题请先登录注册