Spring mvc Spring MVC和java脚本(addRows函数)绑定

Spring mvc Spring MVC和java脚本(addRows函数)绑定,spring-mvc,javascript-framework,Spring Mvc,Javascript Framework,我对SpringMVC中的javascript函数绑定有疑问/问题。根据我们的要求,当用户单击“添加”按钮时,我需要在表中插入新行 步骤1:因此,当用户单击“添加更多”按钮时,我会在表中插入一个新行,我正在使用javascript处理这个问题 步骤2:当用户单击submit按钮时,我需要将用户输入的值发送到我的控制器(SpringMVC控制器) 那么,如何将这些值动态绑定到控制器呢 请帮助我尽快解决此问题。当我需要绑定来自前端的对象的动态列表时,我会执行以下操作: 将数据发布为json数组,即以

我对SpringMVC中的javascript函数绑定有疑问/问题。根据我们的要求,当用户单击“添加”按钮时,我需要在表中插入新行

步骤1:因此,当用户单击“添加更多”按钮时,我会在表中插入一个新行,我正在使用javascript处理这个问题

步骤2:当用户单击submit按钮时,我需要将用户输入的值发送到我的控制器(SpringMVC控制器)

那么,如何将这些值动态绑定到控制器呢


请帮助我尽快解决此问题。

当我需要绑定来自前端的对象的动态列表时,我会执行以下操作:

将数据发布为
json数组
,即以下格式

{数据:[{a:1,b:2},{a:3,b:4}]}

控制器中

@RequestMapping(value="save", method=RequestMethod.POST)
public void save(JSONObject object)
{
 List<YourType> list = new ArrayList<YourType>();
 JSONArray array = object.getJSONArray("data")
 for(int i=0; i<array.length(); i++)
 {
   //getObjectFromJson is your method for converting json to an object of your type.
   list.add(JsonUtils.fromJson(array.getJSONObject(i).toString(), YourType.class);
 }
}
@RequestMapping(value=“save”,method=RequestMethod.POST)
公共作废保存(JSONObject对象)
{
列表=新的ArrayList();
JSONArray数组=object.getJSONArray(“数据”)

对于(int i=0;iSpring可以绑定对象的映射和列表,如果您指定创建一个适当的类来保存表单数据,那么就在控制器方法上使用
@modeldattribute
注释

例如,如果JavaScript创建如下表行:

<tr>
    <td><input name="bells[0]" /></td>
    <td><input name="whistles[0]" /></td>
</tr>
<tr>
    <td><input name="bells[1]" /></td>
    <td><input name="whistles[1]" /></td>
</tr>
public class AsapForm {
    private List<String> bells;
    private List<String> whistles;

    // add getters and setters here
}

然后,您可以使用
asapForm.getBells()
asapForm.getWhistles()
等来访问每一行的值。

我使用LazyList实现了这一点

您需要按照以下方式执行此操作

Operation.java

    package com.xxx.xxx.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.xxx.xxx.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;iIt),如果您提供表格HTML、JavaScript和控制器代码的示例,则回答此问题会更容易。这不会绑定动态添加的行。您必须使用特定数量的大小初始化列表。一个JSP页面如何添加动态行?
/**
     * 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();
    }