2012-10-17 2 views
1

스프링 MVC에서 javascript 함수 바인딩에 대해 질문/문제가 있습니다. 우리의 요구 사항에 따라 사용자가 "추가"버튼을 클릭 할 때 테이블에 NEW ROW를 삽입해야합니다.봄 MVC 및 자바 스크립트 (addRows 함수) 바인딩

1 단계 : 제출 버튼을 사용자가 클릭, 내가해야 사용자가 클릭 그래서 때 테이블에서 새 행을 삽입 "추가"버튼을, 나는

2 단계 사용하여이 자바 스크립트를 처리하고 사용자가 입력 한 값을 내 컨트롤러 (Spring MVC 컨트롤러)로 보냅니다.

어떻게 컨트롤러에 값을 동적으로 바인딩 할 수 있습니까?

최대한 빨리 문제를 해결할 수 있도록 도와주세요.

+0

테이블 HTML, JavaScript 및 컨트롤러 코드 예제를 제공하면이 질문에 대답하는 것이 더 쉽습니다. – gutch

답변

0

나는 내가 프런트 엔드에서 오는 객체의 동적 목록에 바인딩 할 때 다음을 수행 : [{A :

{데이터를 다음과 같은 형식으로 즉 json array

포스트 데이터를 : 1, B : 2}, {A : 3, B : 오브젝트의지도와 목록을 결합 할 수있는 Controller

@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); 
} 
} 
0

봄 4}]}

당신은 appropr를 만들 줄 경우 iate 클래스를 사용하여 양식 데이터를 보유한 다음 컨트롤러 메소드에 @ModelAttribute 주석을 사용하십시오.

예를 들어, 자바 스크립트는이 같은 테이블 행 만드는 경우 :

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

그런 다음 당신은 다음과 같이 당신의 HTML 형식으로 각 반복 필드의 목록을 포함하는 모델 클래스를 만들 수 있습니다

public class AsapForm { 
    private List<String> bells; 
    private List<String> whistles; 

    // add getters and setters here 
} 

그리고 당신은 @ModelAttribute 주석과 매개 변수로 클래스를 사용하는 제어 방법을 만들 수 있습니다

public void postAsapForm(@ModelAttribute("contactForm") AsapForm asapForm, BindingResult result) { 
    ... 
} 

당신은 내가 LazyList를 사용하여이를 달성

+0

동적으로 추가 된 행을 바인드하지 않습니다. 특정 크기의 번호로 목록을 초기화해야합니다. –

0

asapForm.getBells()asapForm.getWhistles()를 사용하여 각 행의 값에 액세스 할 수 있습니다.

다음과 같이하십시오.

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

} 

OperationParameters.java

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




} 

Conroller 방법은 새로운 동작을 추가하는 POST 요청을 제공한다.

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

희망이 도움이 될 것입니다.

+0

나는 여전히 문제에 직면 해있다 ... – nidhi

+0

하나의 JSP 페이지에서 동적 행을 추가하는 방법은 무엇입니까? –