2013-10-24 53 views
0

我在春天的形式像下面春季形式从对象列表中选择显示

<form:form method="POST" commandName="productlist"> 
    <table> 
    <c:forEach var="product" items="${productlist}"> 
    <tr id=${product.id}><td>${product.name}</td> 
    </tr>  
    </c:forEach> 
    </table> 
    </form:form> 

现在,我想在每个产品前面的复选框,在JSP中,这样用户可以选择显示的对象列表任何产品和选定的产品列表都会发送给控制器进行进一步处理。提前致谢。

回答

0

您可以定义表单支持对象,因为这:

public class ProductForm { 
private List<String> productList;//checkbox values to be shown 
private String[] selProductList;//selected checkbox values available for processing 
//other properties 
... 
//getters and setters 
} 

现在,您可以定义checkbox就象这样:

<form:form action="/productAction" method="post" modelAttribute="productform"> 
<form:checkboxes items="${productform.productList}" path="selProductList" /> 
... 
</form> 

提交此form时,所选复选框值将通过selProductList阵列提供给您。

+0

感谢Debojit,我在列表ProductForm中尝试使用列表而不是列表。在该页面开始显示每个产品的toString内容与输出上的复选框选项。但在选择少量产品后提交页面时,它给出以下异常org.springframework.validation.BindException:org.springframework.validation.BeanPropertyBindingResult:1个错误 字段'selProductList'上的对象'productList'中的字段错误:被拒绝的值[productId:13name:dhanush] – Vikky

+0

也想知道是否正确的方法来创建每个表单的模型类,不知道,但可能是,我将不得不创建一些模型类然后 – Vikky

+0

我看不到任何需要使用'List '而不是'List '显示复选框值,因为要显示给用户的值仅为字符串。为你的“表单”使用一个表单支持对象并不是一个坏主意。 –

0

添加一个复选框内TD

<form:form method="POST" commandName="productlist"> 
    <table> 
    <c:forEach var="product" items="${productlist}"> 
    <tr id=${product.id}><td><form:checkbox path=""/>${product.name}</td> 
    </tr>  
    </c:forEach> 
    </table> 
    </form:form> 

看到here