如何验证服务器端动态生成的字段 [英] How to validate dynamically generated fields server side

查看:67
本文介绍了如何验证服务器端动态生成的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Spring 3.1开发了一个Web应用程序

I have developed a web application using Spring 3.1

在模块之一中,我需要保存一个具有许多OperationParameter对象的Operation对象. 因此,在视图中,我提供了添加按钮,供用户为特定操作创建OperationParameters.

In one of the module I need to save one Operation object having many OperationParameter objects. So in the view I have provided add button for user to create OperationParameters for the particular Operation.

两个模型都具有休眠映射,并且Operation和OperationParameter之间存在一对多的关系.在Operation模型中,当用户通过动态添加的OperationParameters创建新的Operation时,我将在数据库中插入OperationParameters列表.

Both the models have hibernate mapping and there is one to many relation between Operation and OperationParameter. And in Operation model I have List of OperationParameters that will be inserted in database when a new Operation will be created with dynamically added OperationParameters by the user.

当我不使用验证时,它可以正常工作.当我为Operation模型插入操作时,OperationParameters的列表也将插入到OperationParameter表中.

When I don't use validation it works fine. When I do insert operation for Operation model the list of OperationParameters will also be inserted in the OperationParameter table.

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

My question is that how can I do server side validation for OperationParameter fields? And if validation is done with errors then how can I show the error of particular OperationParameter field?

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;
    }

}

OperationParameters.java

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;
    }




}

用于发布请求以添加新操作的控制器方法.

/**
     * 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();
    }

在这方面任何好的建议或榜样都会对我有很大的帮助. :)

Any good suggestion or example in this regard will be a great help to me. :)

**

**

我还想知道是否可以验证字段数组并将错误显示回jsp文件中?

I would also like to know whether it is possible to validate an array of fields and show the error back in the jsp file?

请帮帮我.

推荐答案

经过一周的头脑风暴,我自己找到了解决方案. 我很高兴终于实现了这一目标. :)

After a week of brain storming I've found the solution myself. I am really happy that finally I have achieved this. :)

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

For the purpose to validate the dynamically generated fields you need to depend on custom validations on server side. Because if we use the Annotation based validation for dynamically generated fields then after validation it will not come in the controller function.

因此,每当您执行自定义验证时,都会从控制器函数中调用验证.而且您可以在列表中动态生成的字段上生成错误.

So whenever you do custom validations then the validation will be called from the controller function. And there you can generate error on dynamically generated fields in the list.

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

If validation is not successful then you need to get back to the jsp page and display errors on the form. You need to display the dynamically generated field values along with their errors using .

我希望这能帮助那些也希望同一件事起作用的人.

I hope this helps others who also want the same thing to work.

这篇关于如何验证服务器端动态生成的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆