2
是否可以让@ModelAttribute方法仅针对特定处理程序方法进行调用,并仅为一个处理程序方法调用提供命令对象?不是针对特定控制器中的每个处理程序方法?我使用Spring的web portlet的MVC,但它应该是相同的...Spring @ModelAttribute仅适用于一种处理程序方法
因为这个for循环被每一个信息处理方法调用一个控制器内和内隐提供给每个请求将视图
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);
}
}
我错过了它的目的。我们可以简单地在控制器“MyBean initBean()”中创建一个方法,该方法最终进行初始化并返回我们可以添加到“模型”中的bean实例,并且效果是相同的 – lisak 2010-11-13 17:47:02