我创建了一个名为 Person 的类。然后我通过 Spring Controller 将此类的一个对象传递给 JSP 页面,比如 abc.htm。
现在我想让它从 abc.htm 传回另一个 Controller 。 我怎么能那样做?
还请告诉我,如果任何其他类对象(例如 Address
类对象)使用该人对象作为参数,那么我将如何将该 Address
类对象传递给 Controller .
我很困惑,请帮帮我。
最佳答案
页面呈现后,您不再处于“Java 领域”,因此您没有对象。您可以根据在下一个请求中发回的参数重建它们。
这称为“绑定(bind)”。在 Spring MVC 中,如果您使用 <form:x>
tags,这是自动完成的(或多或少)。 .然后在您的 Controller 中,您的对象将作为方法属性访问:
@RequestMapping(..)
public String foo(YourObject object) {..}
您可能需要一个 @ModelAttribute
如果您的参数名称与 JSP 中的名称不同,则注释。 MVC docs写:
Command or form objects to bind parameters to: as bean properties or fields, with customizable type conversion, depending on @InitBinder methods and/or the HandlerAdapter configuration. See the webBindingInitializer property on AnnotationMethodHandlerAdapter. Such command objects along with their validation results will be exposed as model attributes by default, using the non-qualified command class name in property notation. For example, "orderAddress" for type "mypackage.OrderAddress". Specify a parameter-level ModelAttribute annotation for declaring a specific model attribute name.
我建议您查看 PetClinic Sample Application看看这在实践中是如何工作的。
关于java - 将对象从 JSP 传递到 Spring Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4423843/