Spring 如何在服务器端验证动态生成的字段

Spring 如何在服务器端验证动态生成的字段,spring,spring-mvc,spring-3,hibernate-validator,Spring,Spring Mvc,Spring 3,Hibernate Validator,我使用Spring3.1开发了一个web应用程序 在其中一个模块中,我需要保存一个具有多个OperationParameter对象的操作对象。 因此,在视图中,我为用户提供了add按钮,用于为特定操作创建OperationParameters 这两种模型都有hibernate映射,并且操作和操作参数之间存在一对多关系。在操作模型中,我有一个OperationParameters列表,当用户使用动态添加的OperationParameters创建新操作时,该列表将插入数据库中 当我不使用验证时,它

我使用Spring3.1开发了一个web应用程序

在其中一个模块中,我需要保存一个具有多个OperationParameter对象的操作对象。 因此,在视图中,我为用户提供了add按钮,用于为特定操作创建OperationParameters

这两种模型都有hibernate映射,并且操作和操作参数之间存在一对多关系。在操作模型中,我有一个OperationParameters列表,当用户使用动态添加的OperationParameters创建新操作时,该列表将插入数据库中

当我不使用验证时,它可以正常工作。当我为操作模型插入操作时,操作参数列表也将插入到操作参数表中

我的问题是如何对OperationParameter字段进行服务器端验证? 如果验证有错误,那么如何显示特定OperationParameter字段的错误

Operation.java

    package com.abcprocure.servicerepo.model;
// Generated Feb 9, 2012 11:30:06 AM by Hibernate Tools 3.2.1.GA


import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.apache.commons.collections.FactoryUtils;
import org.apache.commons.collections.list.LazyList;

@Entity
@Table(name="Operations"
    ,schema="dbo"

)
public class Operations  implements java.io.Serializable {


     private int operationId;
     @Embedded
     private Services services;
     private String operationName;
     private String isHqlsql;
     private String isMultipleTables;
     private String listOfTablesAffected;
     private String hqlQuery;
     private String typeOfOperation;
     private String operationDetail;
     private String inputVariables;
     private String outputparamdatatype;
     private String isCountQuery;
     private String isDynamicWhereQry;
     private String isPaginationRequired;
     private String biInputParameters;
    private List<OperationParameters> operationParameterses = LazyList
            .decorate(new ArrayList<OperationParameters>(),
                    FactoryUtils.instantiateFactory(OperationParameters.class));

    public Operations() {
    }


    public Operations(int operationId, Services services, String operationName) {
        this.operationId = operationId;
        this.services = services;
        this.operationName = operationName;
    }
    public Operations(int operationId, Services services, String operationName, String isHqlsql, String isMultipleTables, String listOfTablesAffected, String hqlQuery, String typeOfOperation, String operationDetail, String inputVariables, String outputparamdatatype, String isCountQuery, List operationParameterses) {
       this.operationId = operationId;
       this.services = services;
       this.operationName = operationName;
       this.isHqlsql = isHqlsql;
       this.isMultipleTables = isMultipleTables;
       this.listOfTablesAffected = listOfTablesAffected;
       this.hqlQuery = hqlQuery;
       this.typeOfOperation = typeOfOperation;
       this.operationDetail = operationDetail;
       this.inputVariables = inputVariables;
       this.outputparamdatatype = outputparamdatatype;
       this.isCountQuery = isCountQuery;
       this.operationParameterses = operationParameterses;
    }

    @Id 
    @GeneratedValue
    @Column(name="operationId", unique=true, nullable=false)
    public int getOperationId() {
        return this.operationId;
    }

    public void setOperationId(int operationId) {
        this.operationId = operationId;
    }

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="serviceId", nullable=false)
    public Services getServices() {
        return this.services;
    }

    public void setServices(Services services) {
        this.services = services;
    }

    @Column(name="operationName", nullable=false, length=250)
    public String getOperationName() {
        return this.operationName;
    }

    public void setOperationName(String operationName) {
        this.operationName = operationName;
    }

    @Column(name="isHQLSQL", length=50)
    public String getIsHqlsql() {
        return this.isHqlsql;
    }

    public void setIsHqlsql(String isHqlsql) {
        this.isHqlsql = isHqlsql;
    }

    @Column(name="isMultipleTables", length=50)
    public String getIsMultipleTables() {
        return this.isMultipleTables;
    }

    public void setIsMultipleTables(String isMultipleTables) {
        this.isMultipleTables = isMultipleTables;
    }

    @Column(name="listOfTablesAffected", length=500)
    public String getListOfTablesAffected() {
        return this.listOfTablesAffected;
    }

    public void setListOfTablesAffected(String listOfTablesAffected) {
        this.listOfTablesAffected = listOfTablesAffected;
    }

    @Column(name="hqlQuery")
    public String getHqlQuery() {
        return this.hqlQuery;
    }

    public void setHqlQuery(String hqlQuery) {
        this.hqlQuery = hqlQuery;
    }

    @Column(name="typeOfOperation", length=50)
    public String getTypeOfOperation() {
        return this.typeOfOperation;
    }

    public void setTypeOfOperation(String typeOfOperation) {
        this.typeOfOperation = typeOfOperation;
    }

    @Column(name="operationDetail")
    public String getOperationDetail() {
        return this.operationDetail;
    }

    public void setOperationDetail(String operationDetail) {
        this.operationDetail = operationDetail;
    }

    @Column(name="inputVariables", length=5000)
    public String getInputVariables() {
        return this.inputVariables;
    }

    public void setInputVariables(String inputVariables) {
        this.inputVariables = inputVariables;
    }

    @Column(name="outputparamdatatype", length=50)
    public String getOutputparamdatatype() {
        return this.outputparamdatatype;
    }

    public void setOutputparamdatatype(String outputparamdatatype) {
        this.outputparamdatatype = outputparamdatatype;
    }

    @Column(name="isCountQuery", length=10)
    public String getIsCountQuery() {
        return this.isCountQuery;
    }

    public void setIsCountQuery(String isCountQuery) {
        this.isCountQuery = isCountQuery;
    }

    public String getIsDynamicWhereQry() {
        return isDynamicWhereQry;
    }


    public void setIsDynamicWhereQry(String isDynamicWhereQry) {
        this.isDynamicWhereQry = isDynamicWhereQry;
    }


    public String getIsPaginationRequired() {
        return isPaginationRequired;
    }


    public void setIsPaginationRequired(String isPaginationRequired) {
        this.isPaginationRequired = isPaginationRequired;
    }

    public String getBiInputParameters() {
        return biInputParameters;
    }


    public void setBiInputParameters(String biInputParameters) {
        this.biInputParameters = biInputParameters;
    }

    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="operations")
    public List<OperationParameters> getOperationParameterses() {
        return this.operationParameterses;
    }

    public void setOperationParameterses(List<OperationParameters> operationParameterses) {
        this.operationParameterses = operationParameterses;
    }

}
package com.abcprocure.servicerepo.model;

// Generated Feb 9, 2012 11:30:06 AM by Hibernate Tools 3.2.1.GA


import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name="OperationParameters"
    ,schema="dbo"

)
public class OperationParameters  implements java.io.Serializable {


     private int parameterId;
     private Operations operations;
     private String inputOutputParamName;
     private String inputOutputParamType;
     private String inputOutputParamDataType;

    public OperationParameters() {
    }

    public OperationParameters(int parameterId, Operations operations, String inputOutputParamName, String inputOutputParamType, String inputOutputParamDataType) {
       this.parameterId = parameterId;
       this.operations = operations;
       this.inputOutputParamName = inputOutputParamName;
       this.inputOutputParamType = inputOutputParamType;
       this.inputOutputParamDataType = inputOutputParamDataType;
    }

    @Id 
    @GeneratedValue
    @Column(name="parameterId", unique=true, nullable=false)
    public int getParameterId() {
        return this.parameterId;
    }

    public void setParameterId(int parameterId) {
        this.parameterId = parameterId;
    }
@ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="operationId", nullable=false)
    public Operations getOperations() {
        return this.operations;
    }

    public void setOperations(Operations operations) {
        this.operations = operations;
    }

    @Column(name="inputOutputParamName", nullable=false, length=250)
    public String getInputOutputParamName() {
        return this.inputOutputParamName;
    }

    public void setInputOutputParamName(String inputOutputParamName) {
        this.inputOutputParamName = inputOutputParamName;
    }

    @Column(name="inputOutputParamType", nullable=false, length=250)
    public String getInputOutputParamType() {
        return this.inputOutputParamType;
    }

    public void setInputOutputParamType(String inputOutputParamType) {
        this.inputOutputParamType = inputOutputParamType;
    }

    @Column(name="inputOutputParamDataType", nullable=false, length=250)
    public String getInputOutputParamDataType() {
        return this.inputOutputParamDataType;
    }

    public void setInputOutputParamDataType(String inputOutputParamDataType) {
        this.inputOutputParamDataType = inputOutputParamDataType;
    }




}
为添加新操作的post请求提供服务的Conroller方法。

/**
     * Method that will serve the post request to add the operation and operation parameters submitted by the user.
     * @param operations
     * @param map
     * @return {@link String} The view name that will redirect to the get request to display the previous page with newly entered operation in the list. 
     */
    @RequestMapping(value="/add", method=RequestMethod.POST)
    public String addOperations(@ModelAttribute Operations operations, ModelMap map) {
        operations.getOperationParameterses().removeAll(Collections.singleton(null));

        for(int i=0; i<operations.getOperationParameterses().size(); i++) {
            System.out.println("parameterName :: " + ((OperationParameters)operations.getOperationParameterses().get(i)).getInputOutputParamName());
            if(((OperationParameters)operations.getOperationParameterses().get(i)).getInputOutputParamName() == null || "".equalsIgnoreCase((((OperationParameters)operations.getOperationParameterses().get(i))).getInputOutputParamName())) {
                operations.getOperationParameterses().remove(i);
                System.out.println("empty parameter removed....");
            }
        }

        return "redirect:/operations/" + operations.getServices().getServiceId();
    }
/**
*方法,该方法将为post请求提供服务,以添加用户提交的操作和操作参数。
*@param操作
*@param-map
*@return{@link String}将重定向到get请求的视图名称,以显示列表中新输入操作的上一页。
*/
@RequestMapping(value=“/add”,method=RequestMethod.POST)
公共字符串addOperations(@modeldattribute Operations,ModelMap){
getOperationParameterses().removeAll(Collections.singleton(null));

对于(int i=0;i您可以创建一个帮助器类,该类根据其数据类型验证您的操作参数。对于字符串,请检查null或空字符串等。验证类还可以具有一些上下文验证,如有效电子邮件地址

在保存Operations对象之前,您需要在OperationParameters的Operations列表中进行迭代,检查数据类型和/或包含信息上下文的字段(例如电子邮件、工作日),并在helper类上调用适当的验证


如果您发现不一致,只需抛出一个描述不一致的自定义异常。处理不一致的代码部分可以将json返回到您的页面,并带有不一致的字段,这将反过来向用户提供反馈(即,字段下的红色文本包含错误消息).

经过一周的头脑风暴,我自己找到了解决办法。 我真的很高兴我终于做到了这一点。:)

为了验证动态生成的字段,您需要依赖服务器端的自定义验证。因为如果我们对动态生成的字段使用基于注释的验证,那么在验证之后,它将不会出现在控制器功能中

因此,每当您进行自定义验证时,验证将从控制器函数调用。在那里,您可以在列表中动态生成的字段上生成错误

如果验证未成功,则需要返回jsp页面并在表单上显示错误。需要使用显示动态生成的字段值及其错误


我希望这能帮助其他也希望使用相同功能的人。

感谢您的回复。您非常接近我想要的功能,但不是确切的功能。根据您的回答,我需要借助ajax验证动态生成的字段。但我不想这样做。我想在用户将表单提交回控制器时验证它(就像我们一般所做的那样)。但是当你发布这个答案的时候,我已经描述了确切的解决方案(多好的合作伙伴!!)。请检查我的答案。仍然+1供你参考。:)你为什么认为我在谈论ajax?我的意思是你要创建一个类,其中包含验证数据的方法(helper类),然后在方法addOperations for loop.All服务器端使用它。如果捕获到错误,可以重定向到错误页面或返回一些描述错误的json(ajax方式).Okies那么。我接受你的答案。你给出了与我类似的答案。无论如何,感谢你的努力。:)对不起。我现在没有这段代码。你可以按照上面的解释操作。它非常简单。你需要在内部手动验证pojo对象,而不是使用注释方法验证pojo对象如果验证失败,您可以设置错误并显示在JSP页面上。