如何使用jstl填充jsp中的所有java bean属性

时间:2013-06-28 07:30:41

标签: jsp spring-mvc jstl

我需要使用jsp jstl在jsp中呈现所有java bean属性。 我正在使用spring mvc。 以下是春季代码的一部分。

@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个属性,手动写属性非常繁琐。

我想要一些循环方式来获取所有属性值是使用jstl的jsp或者可能是其他方式。 请分享你的提示!

感谢

2 个答案:

答案 0 :(得分:0)

您应该编写自己的自定义标记,并将其提供给社区用户。至于我知道没有可用的JSTL标签试图显示一个对象的所有属性。

答案 1 :(得分:0)

您可以使用java反射为每个客户创建所有属性的数组,并将其放入一些新的POJO中。

public CustomerProp {
   private List<String> properties;
}

然后使用一个迭代为每个客户在jsp中显示它们。