Thymeleaf ModelAttribute List

时间:2014-08-05 16:21:50

标签: spring spring-mvc thymeleaf

我尝试使用Thymeleaf作为ModelAttribute之类的List,但是我没有让它工作。我读了这页http://www.bincsoft.com/blog/thymeleaf-and-lists-in-forms/,但我的代码失败了。

这是我的档案:

容器类

import java.util.ArrayList;
import java.util.List;

import javax.validation.Valid;


public class WagerateForm {

    @Valid
    private List<WagerateUi> wagerateuilist;

    public WagerateForm() {
        wagerateuilist = new ArrayList<WagerateUi>();
    }

    public WagerateForm(List<WagerateUi> wagerateuilist) {
        this.wagerateuilist = wagerateuilist;
    }

    //getter , setter omitted
}

我的DTO for ui

public class WagerateUi {

    //getter , setter omitted

    private boolean standard;
    private Long value;
}

控制器

@Named
@RequestMapping("/wagerate")
public class WagerateController {

@RequestMapping(value="")
public String wagerate(@ModelAttribute("wagerateform") WagerateForm wagerateForm,
        BindingResult bindingResult) {

    List<WagerateUi> wagerateUiList = wagerateForm.getWagerateuilist();

    System.out.println(wagerateUiList.size());

    //dummy items
    for (int i = 0; i < 10; i++) {
        WagerateUi temp = new WagerateUi();
        temp.setValue(100L);
        wagerateUiList.add(temp);
    }


    return "wagerate";
}

thymeleaf html

<form id="wagerateform" class="form-horizontal" role="form" action="#" th:object="${wagerateform}" th:action="@{/wagerate}" method="post">              

    <div th:each="wagerateui, iterStat  : ${wagerateform.wagerateuilist}">      
        <input th:field="*{wagerateform[__${iterStat.index}__].value}" class="form-control">
    </div>
    <button type="submit">Test</button>

</form>

错误消息

Caused by: org.springframework.beans.NotReadablePropertyException: Invalid property 'wagerateform[0]' of bean class [WagerateForm]: Bean property 'wagerateform[0]' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
    at org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:725)
    at org.springframework.beans.BeanWrapperImpl.getNestedBeanWrapper(BeanWrapperImpl.java:571)
    at org.springframework.beans.BeanWrapperImpl.getBeanWrapperForPropertyPath(BeanWrapperImpl.java:548)
    at org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:714)
    at org.springframework.validation.AbstractPropertyBindingResult.getActualFieldValue(AbstractPropertyBindingResult.java:99)
    at org.springframework.validation.AbstractBindingResult.getFieldValue(AbstractBindingResult.java:229)
    at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:120)
    at org.thymeleaf.spring4.util.FieldUtils.getBindStatusFromParsedExpression(FieldUtils.java:348)
    at org.thymeleaf.spring4.util.FieldUtils.getBindStatus(FieldUtils.java:288)
    at org.thymeleaf.spring4.util.FieldUtils.getBindStatus(FieldUtils.java:260)

控制器中列表的大小始终为0.我看不出什么错,我尝试了不同的方法,但我认为我的Thymeleaf / Spring EL是错误的。

2 个答案:

答案 0 :(得分:2)

到目前为止我的工作是:

<input th:value="${wagerateform.wagerateuilist[__${iterStat.index}__].value}" class="form-control">

答案 1 :(得分:0)

您的wagerateform不是列表,列表是wagerateuilist。使用th:each,您可以在循环浏览列表时使用元素填充wagerateui对象。

<div th:each="wagerateui : ${wagerateform.wagerateuilist}">      
        <input th:field="${wagerateui.value}" class="form-control">
</div>