简体   繁体   English

将对象从JSP传递到Spring控制器

[英]Passing an object from JSP to Spring controller

I created a class named Person. 我创建了一个名为Person的类。 Then I pass an object of this class via Spring controller to JSP page, say abc.htm . 然后,我通过Spring控制器将此类的对象传递给JSP页面,例如abc.htm

Now I want it to transfer back from abc.htm to another controller. 现在,我希望它从abc.htm传输回另一个控制器。 How could I do that? 我该怎么办?

Also tell me if any other class object (say Address class object) uses that person object as parameter, then how would I pass that Address class object to the controller. 还告诉我是否还有其他类对象(例如, Address类对象)使用该人员对象作为参数,那么我将如何将该Address类对象传递给控制器​​。

I am very confused, please help me. 我很困惑,请帮助我。

After the page is rendered you are no longer in the "Java realm", so you don't have your objects. 呈现页面后,您将不再位于“ Java领域”,因此您没有对象。 You can rebuild them based on the parameters that are sent back in the next request. 您可以根据下一个请求中发回的参数重建它们。

This is called "binding". 这称为“绑定”。 In Spring MVC this is done automatically (more or less) if you are using the <form:x> tags . 在Spring MVC中,如果使用<form:x>标记,则会自动(或多或少)完成此操作。 Then in your controller your objects will be accessible as method attributes: 然后,在您的控制器中,您的对象将作为方法属性进行访问:

@RequestMapping(..)
public String foo(YourObject object) {..}

You might need a @ModelAttribute annotation if the name of your param and the one in the JSP are not the same. 如果您的参数名称与JSP中的参数名称不同,则可能需要@ModelAttribute批注。 The MVC docs write: MVC文档写道:

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. 命令或表单对象将参数绑定到:作为bean属性或字段,具有可自定义的类型转换,具体取决于@InitBinder方法和/或HandlerAdapter配置。 See the webBindingInitializer property on AnnotationMethodHandlerAdapter. 请参阅AnnotationMethodHandlerAdapter上的webBindingInitializer属性。 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". 例如,类型“ mypackage.OrderAddress”的“ orderAddress”。 Specify a parameter-level ModelAttribute annotation for declaring a specific model attribute name. 指定用于声明特定模型属性名称的参数级ModelAttribute批注。

I'd suggest you review the PetClinic Sample Application to see how this works in practice. 我建议您查看PetClinic示例应用程序以了解其实际工作原理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM