Spring mvc - form:checkboxes不带给我的东西

时间:2016-11-28 00:00:15

标签: spring forms model-view-controller checkboxlist

我正在使用表单:复选框并且不能正常工作。我可以检查html中的项目,当我把提交,debbuging,我看到我检查的项目不在我的表单列表中,列表是空的

表格

public class PresupuestoForm {

private long id;
private List<ItemVenta> elementos;


public long getId() {
    return id;
}
public void setId(long id) {
    this.id = id;
}
public List<ItemVenta> getElementos() {
    return elementos;
}
public void setElementos(List<ItemVenta> elementos) {
    this.elementos = elementos;
}

控制器

@RequestMapping(value =&#34; / manageStock&#34;,method = RequestMethod.GET)     public ModelAndView controlarStockParaPasarAVenta(@RequestParam(&#34; getItem&#34;)long id){

    try{

        Presupuesto p = this.presupuestoService.getPresupuesto(id);
        PresupuestoForm form = new PresupuestoForm();
        form.setId(p.getId());
        List<ItemVenta> elementos = new ArrayList<ItemVenta>();

        getItemsFromPresupuesto(p, elementos); // Method that fill the list elementos with some items

        ModelAndView m = new ModelAndView("manageStock");
        m.addObject("command",form);
        m.addObject("elementos",elementos);
        return m;

    }catch(Exception e){

        ...
    }
}




@RequestMapping(value = "/manageStockForm", method = RequestMethod.POST)
public Object manageStockPresupuestoForm(@ModelAttribute("manageStockForm") PresupuestoForm form, BindingResult result){

    Presupuesto p = null;

    try{

        p = this.presupuestoService.getPresupuesto(form.getId());

        for(ItemVenta i : form.getElementos()){


        }

        return "redirect:becomeVenta.html?getItem="+form.getId();

    }catch(BussinesException e){

        ...

    }catch(Exception e){

    ...

    }
}

HTML

<table>
    <tr style="display: none;">
                <td><form:input path="id"/></td> 
    </tr>
    <tr>
        <td><div class="texto">
            <form:checkboxes path="elementos" items="${elementos}" itemLabel="itemName" /> 
        </div></td>
    </tr>
    <tr>
            <td>
                    <input class="linkboton" type="submit" value="Submit"/>
                </td>
    </tr>

</table>

WEB.XML

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>SpringTiles</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>

查看解析器

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context3.0.xsd">


<context:component-scan base-package="controllers" />


<bean id="viewResolver"
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass">
        <value>
            org.springframework.web.servlet.view.tiles2.TilesView
        </value>
    </property>
</bean>
<bean id="tilesConfigurer"
    class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
    <property name="definitions">
        <list>
            <value>/WEB-INF/tiles.xml</value>
        </list>
    </property>
</bean>
</beans>

ItemVenta Mapping

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD3.0//EN"
                               "http://hibernate.sourceforge.n/hibernate-mapping-3.0.dtd">
<hibernate-mapping default-access="field" default-cascade="save-update">
<class name="ventas.ItemVenta">

    <id column="id" name="id">
        <generator class="native" />
    </id>

<property name="precio" />
<property name="nombre" />
<property name="cantidad" />
<property name="idItem" />
<property name="tipo" />

</class>
</hibernate-mapping>

1 个答案:

答案 0 :(得分:0)

我的表单:复选框必须遍历实体列表(ItemVenta)并将表单的元素保存在我检查的实体中。我用google搜索答案,我发现我需要将方法等于我的实体。它不起作用。因此,在表单列表中保存实体的内容,现在我正在保存这些实体的ID并且它起作用。我仍然会查看实体列表。