2013-10-24 55 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