รูปแบบไดนามิกพร้อมการส่งวัตถุหลายรายการใน Spring?

ฉันมี Object CreateProjectFormModel ดังนี้ (ฉันใช้ Spring 4)

public class CreateProjectFormModel {
    private Project project;
    private List<DUser> users;

    public CreateProjectFormModel() {
        this.project = new Project();
        this.users = new ArrayList<DUser>();
    }

    public Project getProject() {
        return project;
    }

    public void setProject(Project project) {
        this.project = project;
    }

    public List<DUser> getUsers() {
        return users;
    }

    public void setUsers(List<DUser> users) {
        this.users = users;
    }
}

ฉันไม่สามารถทราบวิธีสร้างตัวควบคุมและแบบฟอร์มที่เกี่ยวข้องเพื่อให้สามารถส่ง DUser หลายรายการพร้อมกันได้ - จะทำได้ไหมหากวัตถุไม่มีคอลเลกชัน

อ่านสิ่งนี้ แต่ฉันไม่ ไม่ทราบว่าจะเพิ่มผู้ใช้ในโครงการล่วงหน้าได้อย่างไร จึงไม่สามารถกำหนดขนาดผู้ใช้ได้

ฉันอ่านบทช่วยสอน thymeleaf แล้ว แต่อยากทราบว่าสามารถทำได้โดยไม่ต้องใช้ ไธม์ลีฟ

ขอบคุณ.


person ykesh    schedule 20.12.2014    source แหล่งที่มา


คำตอบ (2)


ลิงก์ที่คุณโพสต์ในคำถาม List‹Foo › ในฐานะที่เป็นอ็อบเจ็กต์สำรองแบบฟอร์มโดยใช้ spring 3 mvc ไวยากรณ์ที่ถูกต้องหรือไม่ ควรให้วิธีแก้ปัญหาสำหรับคุณ สิ่งที่กล่าวถึงในความคิดเห็น

ฉันคิดว่าโซลูชันนี้จำเป็นต้องมีช่องป้อนข้อมูลจำนวนคงที่ ถูกต้องหรือไม่ จะเกิดอะไรขึ้นหากคุณมีจำนวนฟิลด์อินพุตแบบไดนามิก

ไม่เกี่ยวข้องกับจำนวนผู้ใช้ซึ่งไม่จำเป็นต้องได้รับการแก้ไข แต่จะเกี่ยวข้องกับความจริงที่ว่าคุณสมบัติของออบเจ็กต์นั้นแตกต่างกัน ซึ่งฉันไม่เชื่อว่าเป็นกรณีของคุณ ดังนั้น หาก DUser ของคุณมีคุณสมบัติ ชื่อผู้ใช้ และเช่น โครงการของคุณมีคุณสมบัติ ชื่อ วิธีการควบคุมของคุณอาจเป็นเพียง

@RequestMapping(value = "/test", method=RequestMethod.POST)
public String processSubmit(CreateProjectFormModel createProjectFormModel) {
       ...
}

และแบบฟอร์มของคุณ

<form:form action="/form/test" method="post">
    <div class="single">
        <input type="text" name="project.name"/>
        <input type="text" name="users[0].userName"/>
        <a href="/th#" onclick="addNewUserInputSection();return false">add another user</a>
        <input type="submit" value="Save">
    </div>
</form:form>

โดยที่คุณจะต้องพยายามสร้างฟังก์ชันจาวาสคริปต์ addNewUserInputSection ซึ่งจะเพิ่มช่องป้อนข้อมูลชุดใหม่สำหรับคุณสมบัติ ผู้ใช้ พร้อมด้วยดัชนีที่เพิ่มขึ้น เช่น

<form:form action="/form/test" method="post">
    <div class="single">
        <input type="text" name="project.name"/>
        <input type="text" name="users[0].userName"/>
        <input type="text" name="users[1].userName"/>
        <a href="/th#" onclick="addNewUserInputSection();return false">add another user</a>
        <input type="submit" value="Save">
    </div>
</form:form>

ตัวอย่างเป็นเพียงตัวอย่างพื้นฐาน แต่ควรจะเพียงพอที่จะให้คุณแก้ไขปัญหาได้

person Master Slave    schedule 21.12.2014

แม้ว่าคำตอบข้างต้นจะใช้งานได้ แต่นี่เป็นอีกทางเลือกหนึ่งที่ไม่ต้องการให้คุณสร้างคลาส wrapper/ คลาสแบบฟอร์ม

โมเดลและตัวควบคุม

public class Foo {
    private String name;
    private List<Foo> fooList;
    public Foo() {
    }

    public String getName() {
        return name;
    }

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

    public void setFooList(String fooList) {
        this.fooList = fooList;
    }

}

@Controller("/")
public class FooController{

    //returns the ModelAttribute fooListWrapper with the view fooForm
    @RequestMapping(value = "/FOO", method = RequestMethod.GET)
    public String getFooForm(Model model) {
        List<Foo> fooList = service.getFooList();

        model.addAttribute("fooList", fooList);

        return "list_foo"; //name of the view
    }

    @RequestMapping(value = "/FOO", method = RequestMethod.POST)
    public String postFooList(@ModelAttribute("foo")Foo foo, Model model) {
        List<Foo> list = foo.getFooList(); // **This is your desired object. 
        //If you debug this code, you can easily find this is the list of  
        //all the foo objects that you wanted, provided you pass them properly. 
        //Check the jsp file to see one of the ways of passing such a list of objects**
        //Rest of the code
    }

}

มุมมอง JSP

<form:form id="form" action="<paste-target-url-here>" method="POST" modelAttribute="fooList">


    <c:forEach items="${fooList}" varStatus="i">
           <form:input path="fooList[${i.index}].name" type="text"/>
           <!-- Here you are setting the data in the appropriate index which will be caught in the controller -->
    </c:forEach>


    <button>submit</button>
</form:form>
person sbsatter    schedule 27.02.2017