복붙노트

[SPRING] 봄 3 mvc, 올바른 구문을 사용하여 양식지지 개체로 <Foo> 목록?

SPRING

봄 3 mvc, 올바른 구문을 사용하여 양식지지 개체로 목록?

나는 foo가 하나의 String 필드 이름을 가진 클래스이고 getter / setter와 같은 것을하고 싶다.

<form:form id="frmFoo" modelAttribute="foos">
   <c:forEach items="${foos}" var="foo">
     <form:input path="${foo.name}" type="text"/>

그리고 업데이트 된 이름으로 전체 식품 목록을 제출 하시겠습니까? 내 컨트롤러 시그 같은이 모양 :

@RequestMapping(value = "/FOO", method = RequestMethod.POST)
public String getSendEmail(List<Foo> foos, Model model)
{}

해결법

  1. ==============================

    1.어쩌면이 질문에 대한 답변 :

    어쩌면이 질문에 대한 답변 :

    컨트롤러 :

    @Controller("/")
    public class FooController{
    
        //returns the ModelAttribute fooListWrapper with the view fooForm
        @RequestMapping(value = "/FOO", method = RequestMethod.GET)
        public String getFooForm(Model model) {
            FooListWrapper fooListWrapper = new FooListWrapper();
            fooListWrapper.add(new Foo());
            fooListWrapper.add(new Foo());
    
            //add as many FOO you need
    
            model.addAttribute("fooListWrapper", fooListWrapper);
    
            return "fooForm";
        }
    
        @RequestMapping(value = "/FOO", method = RequestMethod.POST)
        public String postFooList(@ModelAttribute("fooListWrapper")FooListWrapper fooListWrapper, Model model) {
    
            //...........
        }
    
    }
    

    래퍼 작성자 :

    public class FooListWrapper {
        private List<Foo> fooList;
    
        public FooListWrapper() {
             this.fooList = new ArrayList<Foo>();
        }
    
        public List<Foo> getFooList() {
            return fooList;
        }
    
        public void setFooList(List<Foo> fooList) {
            this.fooList = fooList;
        }
    
        public void add(Foo foo) {
            this.fooList.add(foo);
        }
    }
    

    푸 (FOO) 등급 :

    public class Foo {
        private String name;
    
        public Foo() {
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    }
    

    JSP VIEW (name = fooForm) :

    <c:url var="fooUrl" value="/FOO"/>
    <form:form id="frmFoo" action="${fooUrl}" method="POST" modelAttribute="fooListWrapper">
    
    
        <c:forEach items="${fooListWrapper.fooList}" varStatus="i">
               <form:input path="fooList[${i.index}].name" type="text"/>
        </c:forEach>
    
    
        <button>submit</button>
    </form:form>
    
  2. ==============================

    2.위의 대답은 작동하지만 래퍼 클래스 / 폼 클래스를 만들 필요가없는 대체 방법은 다음과 같습니다. 모델 및 컨트롤러

    위의 대답은 작동하지만 래퍼 클래스 / 폼 클래스를 만들 필요가없는 대체 방법은 다음과 같습니다. 모델 및 컨트롤러

    public class Foo {
        private String name;
        private List<Foo> fooList; //**must create this list, also getter and setter**
        public Foo() {
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
        public List 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 getFooList(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>
    
  3. from https://stackoverflow.com/questions/9617207/listfoo-as-form-backing-object-using-spring-3-mvc-correct-syntax by cc-by-sa and MIT license