将两个不同的对象自动装配到一个spring bean

时间:2023-01-22 12:08:53

I am new to spring MVC and trying to add fields in existing form validate them. once the validation is complete, I need to map the data entered in form to a different object. The configuration of the spring bean is something like below.

我是Spring MVC的新手,并试图以现有形式添加字段来验证它们。验证完成后,我需要将表单中输入的数据映射到不同的对象。 spring bean的配置如下所示。

<bean id="SampleController"
    class="com.abcd.controller.SampleController"
    parent="AbstractxyzMlsController">
    <property name="formView"            value="abc/my_view" />
    <property name="highLightedTab"      value="DataRequirement" />
    <property name="commandName"         value="dataRequirement" />
    <property name="commandClass"        value="com.abcd.dataRequirementResponse" />
    <property name="validator"                            ref="DataAcceptanceValidator" />
</bean>

From the form of above spring bean configuration, I need to modify it accept few more fields and put them under a different object. I cannot use the same object as in the commandclass mentioned above. And i also need to validate the fields i add in the form. I see that the current validator accepts the object DataRequirement alone.

从上面的spring bean配置的形式,我需要修改它接受更多的字段并将它们放在不同的对象下。我不能使用与上面提到的命令类相同的对象。我还需要验证我在表单中添加的字段。我看到当前的验证器单独接受对象DataRequirement。

Do i need to come up with a different validator? Also How do i add the second object where I am going to put the fields in?

我需要提出一个不同的验证器吗?另外我如何添加第二个对象,我将把字段放在哪里?

Sorry if the question was dumb. Just trying to understand and implement.

对不起,如果问题是愚蠢的。只是想了解和实施。

2 个解决方案

#1


It is impossible to have multiple objects for same form.

对于相同的形式,不可能有多个对象。

But there is the way, you can use multiple objects like this :

但有方法,你可以使用这样的多个对象:

Backend :

public class FormObject() {
    Class1 class1Object;
    Class2 class2Object;
    //Getter and setter
}

Frontend :

<form:form
  action="../someAction"
  method="post" commandName="formObject">

  ...
  <form:input type="text" path="class1Object.property"/>
  ...
  <form:input type="text" path="class2Object.property"/>
  ...
</form:form>

#2


There is a concept called jsr303 and hibernate validator. with help of Binding results we can get all the error messages. Refer the following link.

有一个名为jsr303和hibernate验证器的概念。在Binding结果的帮助下,我们可以获得所有错误消息。请参阅以下链接。

http://www.codejava.net/frameworks/spring/spring-mvc-form-validation-example-with-bean-validation-api

#1


It is impossible to have multiple objects for same form.

对于相同的形式,不可能有多个对象。

But there is the way, you can use multiple objects like this :

但有方法,你可以使用这样的多个对象:

Backend :

public class FormObject() {
    Class1 class1Object;
    Class2 class2Object;
    //Getter and setter
}

Frontend :

<form:form
  action="../someAction"
  method="post" commandName="formObject">

  ...
  <form:input type="text" path="class1Object.property"/>
  ...
  <form:input type="text" path="class2Object.property"/>
  ...
</form:form>

#2


There is a concept called jsr303 and hibernate validator. with help of Binding results we can get all the error messages. Refer the following link.

有一个名为jsr303和hibernate验证器的概念。在Binding结果的帮助下,我们可以获得所有错误消息。请参阅以下链接。

http://www.codejava.net/frameworks/spring/spring-mvc-form-validation-example-with-bean-validation-api