2012-03-08 8 views
19

que quiero hacer algo como esto, donde foo es una clase con el nombre de campo de un String, y el captador/definidor:Lista <Foo> como objeto de forma de respaldo utilizando muel 3 mvc, sintaxis correcta?

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

y luego enviar la lista completa de Foos con los nombres actualizados? Mi sig controlador se ve así:

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

relacionada? http://stackoverflow.com/questions/8791181/series-of-repeated-forms-in-spring-portlet-mvc – adarshr

+1

Entonces, ¿cuál es su pregunta aquí? –

Respuesta

31

Tal vez esta pregunta answersyour:

CONTROLADOR:

@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 LISTA DE ENVOLTURA:

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); 
    } 
} 

clase Foo:

public class Foo { 
    private String name; 

    public Foo() { 
    } 

    public String getName() { 
     return name; 
    } 

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

VISTA JSP (nombre = 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> 
+8

Supongo que esta solución requiere tener una cantidad fija de campos de entrada, ¿es correcto? ¿Qué pasa si tienes un número dinámico de campos de entrada? Por ejemplo, tengo el requisito de permitir a mis usuarios generar dinámicamente nuevos campos de entrada. ¿Sabes cómo manejar esa situación? – littleK

+0

Faton, eres un salvavidas. Muchas gracias. – w3bshark

+0

Faton, ¿puede explicarme un poco por qué necesita escribir una clase contenedora? – newbie

0

Aunque las obras respuesta anterior, aquí es una alternativa que no requiere que usted para crear una clase contenedora/form clase.

modelo y el controlador

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 Ver

<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> 
+0

Hey. ¿Qué aspecto tiene '' 'renderizado? Necesito volver a crearlo con javascript :) – Nenotlep

+0

Ah, y por cierto, vea 'public String getFooList() {' y 'public void setFooList (String fooList) {' datatypes, se ven muy mal, ¿verdad? – Nenotlep

+0

@Nenotlep, no entiendo lo que querías decir con tu primer comentario. ¿Podrías ser más claro? Y gracias por señalar eso, la respuesta ha sido editada. Por cierto. si está preguntando cómo se ve cuando se ve en un navegador, es simplemente la forma en java (corríjame si estoy equivocado) de lo siguiente: . – sbs

Cuestiones relacionadas