2013-06-28 40 views
0

我需要使用jsp jstl來渲染jsp中的所有java bean屬性。 我使用彈簧mvc。 下面是春季代碼的一部分。如何使用jstl填充jsp中的所有java bean屬性

@RequestMapping(method=RequestMethod.POST) 
public ModelAndView processForm(@ModelAttribute(value="FORM") UploadForm form,BindingResult result) throws Exception{ 
    String filePath = System.getProperty("java.io.tmpdir") + "/" + form.getFile().getOriginalFilename(); 
    ModelAndView model = new ModelAndView("view"); 
    List<Customer> customerList=null;//Customer is POJO file 
    if(!result.hasErrors()){ 
     ProcessUploadedFile processUploadedFile = new ProcessUploadedFile(form, filePath); 
     processUploadedFile.putUploadedFileToServer(form,filePath); 
     customerList= ProcessUploadedFile.readWithCsvBeanReader(filePath); 
    } 
    model.addObject("customerList", customerList);//add list of customers in object. all customer data need to be render in jsp 
    return model; 
} 

JSP JSTL代碼:

<c:forEach var="customer" items="${customerList}"> 

      <tr> 
      <td><c:out value="${customer.hit_time_gmt}"/></td> 
       <td><c:out value="${customer.service}"/></td> 
       <td><c:out value="${customer.accept_language}"/></td> 
       <td><c:out value="${customer.date_time}"/></td> 
       <td><c:out value="${customer.visid_high}"/></td> 
       <td><c:out value="${customer.visid_low}"/></td> 
. 
. 
. 
. 
</tr> 
</c:forEach> 

其實有角落找尋在POJO 300個屬性並手動寫屬性等是非常繁瑣的。

我想要一些循環的方式來獲取所有的屬性值是jsp使用jstl或可能是其他方式。 請分享你的提示!

感謝

回答

0

您可以使用Java反射爲每個客戶創建所有屬性的數組,並將其放入一些新的POJO中。

public CustomerProp { 
    private List<String> properties; 
} 

然後在jsp中使用一個迭代爲每個客戶顯示它們。

0

你應該寫自己的自定義標籤,並將其提供給社區用戶。至於我知道沒有可用的JSTL標籤,它試圖顯示一個對象的所有屬性。