0

Spring 3 MVC(3.0.1 リリース) を使用しています。以下に示すように、Bean クラス Customer と CustomerForm クラスがあります。

public class Contact {
private String firstname;
private String lastname;
private String email;
private String phone;
    //getters and setters
}

public class ContactForm {
private List<Contact> contacts;
    //getters and setters
}

以下に示すように、Spring Controller から連絡先を入力し、Model に設定しています。

private static List<Contact> contacts = new ArrayList<Contact>();

static {
    contacts.add(new Contact("Barack", "Obama", "barack.o@whitehouse.com", "147-852-965"));
    contacts.add(new Contact("George", "Bush", "george.b@whitehouse.com", "785-985-652"));
    contacts.add(new Contact("Bill", "Clinton", "bill.c@whitehouse.com", "236-587-412"));
    contacts.add(new Contact("Ronald", "Reagan", "ronald.r@whitehouse.com", "369-852-452"));
}

@RequestMapping(value = "/get", method = RequestMethod.GET)
public ModelAndView get() {

    ContactForm contactForm = new ContactForm();
    contactForm.setContacts(contacts);

    return new ModelAndView("add_contact" , "contactForm", contactForm);
}

JSPでは、以下のように編集可能な形式で表示していますが、

<form:form method="post" action="save.html" modelAttribute="contactForm">
<table>
<tr>
    <th>No.</th>
    <th>Name</th>
    <th>Lastname</th>
    <th>Email</th>
    <th>Phone</th>
</tr>
<c:forEach items="${contactForm.contacts}" var="contact" varStatus="status">
    <tr>
        <td align="center">${status.count}</td>
        <td><form:input path="contacts[${status.index}].firstname"/></td>
        <td><form:input path="contacts[${status.index}].lastname"/></td>
        <td><form:input path="contacts[${status.index}].email" /></td>
        <td><form:input path="contacts[${status.index}].phone"/></td>
    </tr>
</c:forEach>
    </table>    
    <br/>
    <input type="submit" value="Save" />
</form:form>

連絡先を変更して送信すると、値がモデルに設定されません。ただし、Spring JSTL を使用する代わりに、以下に示すようにプレーンな HTML 入力タグを使用すると、値がモデルに設定されます。

<c:forEach items="${contactForm.contacts}" var="contact" varStatus="status">
    <tr>
        <td align="center">${status.count}</td>
        <td><input name="contacts[${status.index}].firstname" value="${contact.firstname}"/></td>
        <td><input name="contacts[${status.index}].lastname" value="${contact.lastname}"/></td>
        <td><input name="contacts[${status.index}].email" value="${contact.email}"/></td>
        <td><input name="contacts[${status.index}].phone" value="${contact.phone}"/></td>
    </tr>
</c:forEach>

レンダリングされた JSP の HTML ソースをブラウザから確認したところ、以下のように入力コントロール名に違いが見つかりました。

//JSP
<form:input path="contacts[${status.index}].firstname"/>
//corresponding HTML not working
<input id="contacts0.firstname" name="contacts0.firstname" type="text" value="Barack"/>

//JSP
<input name="contacts[${status.index}].firstname" value="${contact.firstname}"/>
//corresponding HTML working!
<input name="contacts[0].firstname" value="Barack"/>

これは既知の問題ですか、それとも何か不足していますか?

よろしく、マケシュ。

4

1 に答える 1

1

以下のように form:input タグで name 属性を直接使用できます。

 <td><form:input name="contacts[${status.index}].firstname" path="contacts[${status.index}].firstname"/></td>
于 2013-03-06T06:18:21.890 に答える