I developed a web application using Spring 3.1
In one of the modules I need to save one Operation object that has many OperationParameter objects. Thus, in the view, I provided an add button for the user to create OperationParameters for a specific operation.
Both models have a sleep mode display and there is one relationship between the operation and the operation parameter. And in the Operation model, I have a List of OperationParameters that will be inserted into the database when a new Operation is created with dynamically added Operation Parameters by the user.
When I do not use validation, it works fine. When I insert an operation for the operation model, the list of OperationParameters will also be inserted into the OperationParameter table.
My question is, how can I do server side validation for OperationParameter fields? And if the check fails, then how can I show the error of a specific field 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; } }
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; } }
The Conroller method to send a mail request to add a new operation.
@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 would be of great help to me. :)
**
Update
**
I would also like to know if it is possible to check an array of fields and show an error in the jsp file?
Please help me.