2012-12-03 41 views
0

我有一個Spring MVC應用程序。 Jsp頁面包含提交的表單。 我控制器的方法是這樣的:Spring MVC Controller List <MyClass>作爲參數

@RequestMapping(value = "photos/fileUpload", method = RequestMethod.POST) 
    @ResponseBody 
    public String fileDetailsUpload(List<PhotoDTO> photoDTO, 
            BindingResult bindingResult, 
            HttpServletRequest request) { 
     // in the point **photoDTO** is null 
    } 

我的班級:

public class PhotoDTO { 
     private Boolean mainPhoto; 
     private String title; 
     private String description; 
     private Long realtyId; 

//getters/setters 

但是,如果我寫相同的方法,但參數是隻是我的對象:

@RequestMapping(value = "photos/fileUpload", method = RequestMethod.POST) 
     @ResponseBody 
     public String fileDetailsUpload(PhotoDTO photoDTO, 
             BindingResult bindingResult, 
             HttpServletRequest request) { 
// Everething is Ok **photoDTO** has all fields, which were fielded on the JSP 

什麼都要我在這種情況下呢?如果我在Jsp上有很多PhotoDTO對象(15)如何在服務器部件上收回所有這些對象?

回答

1

頁面應該將列表中每個PhotoDTO的參數以合適的格式傳遞迴服務器。在這種情況下,您需要使用status var使用name屬性指定列表中每個對象的索引。

<form> 
<c:forEach items="${photoDTO}" var="photo" varStatus="status"> 
     <input name="photoDTO[${status.index}].title" value="${photo.title}"/> 
     <input name="photoDTO[${status.index}].description" value="${photo.description}"/> 
</c:forEach> 
</form> 
+0

無論如何在純html使用客戶端腳本,而不是使用JSTL。我需要在客戶端動態添加列。我需要知道的是我應該如何將輸入對象的'name'屬性傳遞給控制器​​參數中的列表。 –

相關問題