Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling multiple <input>s with the same name in spring-mvc

Please take a look at codes below. Four text boxes are displayed.

If I input "1" and "2" to former text-boxes, these are binded as comma-separated "1,2" as I expected.

However, if I input "2001/01/01" and "2001/01/02" in rest of two-boxes are binded "2001/01/01". "2001/01/01" is only binded surprisingly. First parameter seems having a priority to bind.

I want to know where is defined the specifications(HTTP or SpringMVC or ...?) about that in order to understand deeply and accurately. Can someone help me?

Form
public class SampleForm {

    private String name;

    private Date date;

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

JSP
<form:form modelAttribute="form" method="post">
    <form:input path="name" />
    <form:input path="name" />
    <form:input path="date" />
    <form:input path="date" />
    <p>
        <input type="submit" name="register" value="register" />
    </p>
</form:form>
like image 669
zono Avatar asked Feb 09 '11 15:02

zono


2 Answers

It's logical. Multiple strings can be represented as one String by being comma-separated. Multiple Date objects can't be represented as one Date object.

You can try using String[] and Date[] instead.

like image 103
Bozho Avatar answered Oct 28 '22 15:10

Bozho


private List<Date> date= new ArrayList<Date>();

    public List<Date> getDate() {
        return date;
    }
    public void setDate(List<Date> date) {
        this.date= date;
    }

It will solve your problem.

like image 24
danny.lesnik Avatar answered Oct 28 '22 13:10

danny.lesnik