spring @modelattribute 1つのハンドラーメソッドのみ
-
09-10-2019 - |
質問
@ModelAttributeメソッドを特定のハンドラーメソッドにのみ呼び出して、1つのハンドラーメソッドの呼び出しのみにコマンドオブジェクトを提供することは可能ですか?特定のコントローラー内の各ハンドラーメソッド用ではありませんか? Spring Web-PortletMVCを使用していますが、同じである必要があります...
これは、1つのコントローラー内の各ハンドラーメソッドの呼び出しに対して呼び出され、biewに誘導される各リクエストにbiseに付属されるため、このループが呼び出されるため
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はこれを非常に簡単にします。この種のリファクタリングに制限はないはずです。
所属していません StackOverflow