如何在Spring MVC中迭代一个tpye的列表并从输入框(不在列表中)显示/获取数据 [英] How to iterate a List of one tpye and show/fetch data from input box(out of that list) in Spring MVC

查看:60
本文介绍了如何在Spring MVC中迭代一个tpye的列表并从输入框(不在列表中)显示/获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Class PaymentForm类,其中有一个类型为Issuer的变量.在Issuer中,有一个地址类型为var的地址列表,它具有三个变量addrId,addressLine1和付款.付款类型为Payment,最后在Payment中有一个var,类型为int的paymentAmt.

I have one Class PaymentForm, inside it I have one variable of type Issuer. Inside Issuer there is one var addressList of type Address, which have three vars addrId, addressLine1 and payment. The payment is of type Payment and at last there is one var in Payment, paymentAmt of type int.

所以我必须在jsp中迭代列表(addressList). jsp中有两个输入框和一个标签,它们映射到地址的addrId和addressLine以及Payment的付款.在列表的深处.

So I have to iterate the List (addressList) in jsp. There is two input boxes and one label in jsp which is mapped to addrId and addressLine of Address and payment of Payment. which is somewhere deep down in the List.

到目前为止我已经尝试过的

What I have tried so far

1)" payForm "是在表单级别声明的commandName, counter 是在scripplet页面顶部声明的var.

1) "payForm" is the commandName declared at the form level, counter is the var declared at the top of the page in scripplet.

<c:forEach var="pointer" varStatus="loopStatus" items="payForm.issuer.addressList">
    <DIV style="width: 100%; height: 100%; overflow: auto;">
        <tr>
            <td>
                <b><%=counter++%>)</b>
            </td>
            <td><form:input style="display:none;" path="issuer.addressList[${loopStatus.index}].addrID"/> </td>
            <td><c:out value="${pointer.addressLine}"/></td>                                                    
            <td><form:input path="issuer.addressList[${loopStatus.index}].payment.paymentAmt" size="17" maxlength="12"/></td>
        </tr>
    </DIV>
</c:forEach>

此错误:-

org.apache.jasper.JasperException:javax.el.PropertyNotFoundException:类'java.lang.String'没有属性'addressLine'.

2)

<td><c:out value="${issuer.addressList[${loopStatus.index}].addressLine}"/> </td>

此错误为:-

org.apache.jasper.JasperException:/WEB-INF/pages/invoice.jsp(162,17)"$ {issuer.addressList [$ {loopStatus.index}].addressLine}"包含无效的表达式(s):{1}

3)

<td><c:out value="${payForm.issuer.addressList[${loopStatus.index}].addressLine}"/></td>

与此有关的错误:-

org.apache.jasper.JasperException:/WEB-INF/pages/invoice.jsp(162,17)"$ {payForm.issuer.addressList [$ {loopStatus.index}].addressLine}"包含无效的表达式:{1}

第一和第三字段没有问题.这里只有中间一个人在制造问题.

No issue with first and third field. Only the middle one is creating problem here.

它开始使用以下代码

<td><c:out value="${payForm.issuer.addressList[0].addrID}"/></td>   

但是它的编码.如何使用c:if

But its harcode. How to get the value 0,1,2,3... using "loopStatus" var of c:if

推荐答案

从您的答案中有3种解决方案

From your answer there are 3 solutions

方法1:.

<td><c:out value="${payForm.issuer.addressList[loopStatus.index].addrID}"/></td>


方法2:

<td><c:out value="issuer.addressList[${loopStatus.index}].addrID"/></td>


方法3:由于转义字符,不建议使用.这将为 XSS攻击敞开大门.


Approach 3: Not advisable because of escape character. This will put doors open to XSS attacks.

<td>${payForm.issuer.addressList[loopStatus.index].addrID}</td>


但是我仍然找不到为什么下面的方法行不通的原因.因为它在我的页面中有效.


But still I can't find why below approach is not working. As it is working in my pages.

<td><c:out value="${pointer.addressLine}"/></td>

我不确定,但是这段代码是否能为您提供输出?

I am not sure, but does this code gives you the output?

<td><c:out value="${pointer.addrID}"/></td>

这篇关于如何在Spring MVC中迭代一个tpye的列表并从输入框(不在列表中)显示/获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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