简体   繁体   English

在提交时将多个值从spring表单jsp传递给spring控制器

[英]Pass multiple values from spring form jsp to spring controller on submit

I've a table which has multiple columns as in my database table. 我有一个表,其中有多个列,就像我的数据库表一样。 Few of the columns are editable and the values entered by the user should be updated back in the database on click of submit. 几乎没有几列是可编辑的,并且在单击提交时,用户输入的值应在数据库中更新回来。 For this Ive a foreach loop with value as HashMap of list of bean. 为此,我创建了一个foreach循环,其值作为bean列表的HashMap。 Below is my foreach loop: 下面是我的foreach循环:

<c:forEach items="${feeType.value}" var="feeItem" varStatus="feeVar">                    
<td >[b]<form:checkbox disabled="true" class="editable${ifeeCount}" path="includeFeeValue" value="${feeVar.index}"/> [/b]</td>
<td >${feeItem.feetypeId} </td>
<td >${feeItem.feeValue} </td>
<td >[b]<form:input class="editable${ifeeCount}" disabled="true" path="${feeItem.overridenFee}" value="${feeItem.overridenFee}"/>[/b]</td>
    <td><form:errors path="overriddenFee" cssClass="error" /></td>
<td >${feeItem.lastUpdatedBy} </td>
<td >${feeItem.lastUpdatedDate} </td>

<td > ${feeItem.approvedBy}</td>
<td >${feeItem.approvalDate}</td>

<td align="left">[b]<form:input class="editable${ifeeCount}" disabled="true" path="feeComments[${feeVar.index}]"/>[/b]</td>
<td><form:errors path="feeComments" cssClass="error" /></td>

I'm iterating through a map called feeApprovalByFundId which is Map>. 我正在遍历一个名为FeeApprovalByFundId的地图,该地图为Map>。 How do I get the updated values of the columns like checkbox, input box? 如何获取复选框,输入框等列的更新值? I'm able to get in a String or String array but I won't know to which key was that mapped to. 我可以进入String或String数组,但是我不知道将其映射到哪个键。 (Eg. Fund1 will have 10 fees - I will have under one hashmap key). (例如,Fund1将收取10笔费用-我将使用一个hashmap键)。 I tried iterating like this : 我尝试过这样的迭代:

Collection<List<MyBaseBean>> newList = paswFeeMaintForm.getFeeApprovalByFundId().values();
for(List<MyBaseBean> myList: newList){
for(MyBaseBean myBean : myList){
    System.out.println("Overriden fee : "+myBean.getOverridenFee());
}
}

The path in the form control binding is a string that represents a property binding expression. 表单控件绑定中的path是一个表示属性绑定表达式的字符串。 When the form is rendered, Spring knows what feeItem is due to the JSTL context in which the form control is being rendered. 呈现表单时,Spring知道由于应在其中呈现表单控件的JSTL上下文而产生了feeItem But on POST, that context is lost, so feeItem doesn't make sense. 但是在POST上,该上下文丢失了,因此feeItem没有意义。

Instead, given a form bean like this: 相反,给定这样一个表单bean:

public class FormBean {
    private Map<Integer, ChildBean> children = new HashMap<>();

    public Map<Integer, ChildBean> getChildren() { return children; }   
}

public class ChildBean {
    private String name;
    private String age;
}

And a controller like this: 像这样的控制器:

@ModelAttribute("formBean")
public FormBean createFormBean() {
    FormBean bean = new FormBean();
    bean.add(new ChildBean("Joe", 5));
    bean.add(new Childbean("Sam", 10));
}

@RequestMapping(...)
public String post(@ModelAttribute("formBean") FormBean formBean) { ... }

@RequestMapping(...)
public String get(@ModelAttribute("formBean") FormBean formBean) { ... }

In the view you would do this: 在视图中,您可以这样做:

<form:form modelAttribute="formBean">
    <c:forEach items="${formBean.children}" varStatus="s">
        <form:input path="children[${s.index}].name" />
        <form:input path="children[${s.index}].age" />
    </c:forEach>
</form:form>

The rendered form elements would look something like: 呈现的表单元素如下所示:

<input type="text" name="children[0].name" />
<input type="text" name="children[0].age" />
<input type="text" name="children[1].name" />
<input type="text" name="children[1].age" />

Note that this is the standard indexed property syntax. 请注意,这是标准的索引属性语法。 Now, on the server side, there is enough context to determine which item in the collection should be modified. 现在,在服务器端,有足够的上下文来确定应修改集合中的哪个项目。

For a map, the following no longer applies: 对于地图,以下内容不再适用:

Also note that you don't have to return a list, but can specify the bean method to take an index and a single item, allowing you to define sparse collections (if you haven't constructed the collection ahead of time). 还要注意,您不必返回列表,但是可以指定bean方法来获取索引和单个项目,从而允许您定义稀疏集合(如果尚未提前构造该集合)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM