Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List<Foo> as form backing object using Spring 3 MVC, correct syntax?

Tags:

I want to do something like this, where Foo is a class with one String field name, and getter/setter:

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

And then submit the complete list of Foos with updated names?

My controller looks like this:

@RequestMapping(value = "/FOO", method = RequestMethod.POST) public String getSendEmail(List<Foo> foos, Model model) {     // ... } 
like image 460
NimChimpsky Avatar asked Mar 08 '12 11:03

NimChimpsky


People also ask

What is form backing object in Spring MVC?

Form Backing Object/Command Object This is a POJO that is used to collect all information on a form. It contains data only. It is also called a Command Object in some Spring tutorials. For example, an add a new car form page will have a Car form backing object with attribute data such as Year, Make and Model.

What is the use of spring form?

A springform pan is a round cake pan that features a removable bottom and sides. The sides are held together with an interlocking band that can be opened and removed once your baked good is out of the oven, leaving your cake on the base.


1 Answers

Maybe this answersyour question:

CONTROLLER :

@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) {          //...........     }  } 

FOO LIST WRAPPER :

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 CLASS :

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> 
like image 156
Faton Avatar answered Oct 06 '22 07:10

Faton