2

@ModelAttributeメソッドを特定のハンドラーメソッドに対してのみ呼び出し、1つのハンドラーメソッドの呼び出しに対してのみコマンドオブジェクトを提供することは可能ですか?特定のコントローラー内のハンドラーメソッドごとではありませんか?Spring Web-Portlet MVCを使用していますが、同じである必要があります...

これは、1つのコントローラー内のハンドラーメソッド呼び出しごとにこの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);
                }
            }
4

1 に答える 1

3

このレベルのきめ細かい制御が必要な場合は、コードをリファクタリングする必要があります。つまり、ハンドラーメソッドを独自のクラスに移動します。Spring MVCを使用すると、これが非常に簡単になります。この種のリファクタリングに制限はありません。

于 2010-11-13T11:04:58.717 に答える