Hidden class creates commas in spring webflow

2019-09-17 11:12发布

问题:

I am displaying a list of items using spring webflow. Each item has an edit button; clicking the button opens up a modal dialog. If there are 12 items in the list, I see 12 commas generated in the form path. How can I avoid generation of commas?

<c:forEach var="note" items="${model.modelname}">
//create a click button for each item
//each click opens up a modal

    <div id="modal" class="hidden">
        <div class="modal-body-content">
            <form:textarea path="textPath" />
        </div>
    </div>  

</c:forEach>

回答1:

It appears that in a single form, you have multiple textarea fields with the exact same field name ("textPath") (and exact same DOM ID, which isn't good either).

If so, I'm not surprised that Spring combines all the values submitted under that single name, with commas separating them. What Java type is your model bean's textPath field? I think if it were an array or maybe List, you might get the values separated out. But that's just a guess.



回答2:

There are several issues at work. As noted by @dbreaux, all of your textarea elements are in the same form and have the same id; this is causing the commas when you post the form.

  1. The element id is supposed to be unique to each element. Your element id is "modal" is not unique. Make it unique. This is not causing the commas.
  2. The name of each textarea is textPath. Since you have multiple elements with one name, you get a comma separated list of values for that element name when you post the form. Each textarea must have a unique name. It appears that you have a unique name for each modal (the loop variable is the modalName). Use that in the name of the textarea.
  3. If you can't give each textarea a unique name, give each textarea its own form. Generate a new form for each modal div.

Here is an example of the solution I proposed in #2 above (Note: I don't have a system running that uses spring tags ATM, this example is untested):

<c:forEach var="note" items="${model.modelname}">
    <div id="modal" class="hidden">
        <div class="modal-body-content">
            <form:textarea path="${note}textPath" />
        </div>
    </div>  
</c:forEach>